Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 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 { |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 12 | struct clk clks[SANDBOX_CLK_TEST_NON_DEVM_COUNT]; |
| 13 | struct clk *clkps[SANDBOX_CLK_TEST_ID_COUNT]; |
Neil Armstrong | 65388d0 | 2018-04-03 11:44:19 +0200 | [diff] [blame] | 14 | struct clk_bulk bulk; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | static const char * const sandbox_clk_test_names[] = { |
| 18 | [SANDBOX_CLK_TEST_ID_FIXED] = "fixed", |
| 19 | [SANDBOX_CLK_TEST_ID_SPI] = "spi", |
| 20 | [SANDBOX_CLK_TEST_ID_I2C] = "i2c", |
| 21 | }; |
| 22 | |
| 23 | int sandbox_clk_test_get(struct udevice *dev) |
| 24 | { |
| 25 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 26 | int i, ret; |
| 27 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 28 | for (i = 0; i < SANDBOX_CLK_TEST_NON_DEVM_COUNT; i++) { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 29 | ret = clk_get_by_name(dev, sandbox_clk_test_names[i], |
| 30 | &sbct->clks[i]); |
| 31 | if (ret) |
| 32 | return ret; |
| 33 | } |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 38 | int sandbox_clk_test_devm_get(struct udevice *dev) |
| 39 | { |
| 40 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 41 | struct clk *clk; |
| 42 | |
| 43 | clk = devm_clk_get(dev, "no-an-existing-clock"); |
| 44 | if (!IS_ERR(clk)) { |
| 45 | dev_err(dev, "devm_clk_get() should have failed\n"); |
| 46 | return -EINVAL; |
| 47 | } |
| 48 | |
| 49 | clk = devm_clk_get(dev, "uart2"); |
| 50 | if (IS_ERR(clk)) |
| 51 | return PTR_ERR(clk); |
| 52 | sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM1] = clk; |
| 53 | |
| 54 | clk = devm_clk_get_optional(dev, "uart1"); |
| 55 | if (IS_ERR(clk)) |
| 56 | return PTR_ERR(clk); |
| 57 | sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM2] = clk; |
| 58 | |
| 59 | sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM_NULL] = NULL; |
| 60 | clk = devm_clk_get_optional(dev, "not_an_existing_clock"); |
| 61 | if (IS_ERR(clk)) |
| 62 | return PTR_ERR(clk); |
| 63 | if (clk) |
| 64 | return -EINVAL; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Neil Armstrong | 65388d0 | 2018-04-03 11:44:19 +0200 | [diff] [blame] | 69 | int sandbox_clk_test_get_bulk(struct udevice *dev) |
| 70 | { |
| 71 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 72 | |
| 73 | return clk_get_bulk(dev, &sbct->bulk); |
| 74 | } |
| 75 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 76 | ulong sandbox_clk_test_get_rate(struct udevice *dev, int id) |
| 77 | { |
| 78 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 79 | |
| 80 | if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT) |
| 81 | return -EINVAL; |
| 82 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 83 | return clk_get_rate(sbct->clkps[id]); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ulong sandbox_clk_test_set_rate(struct udevice *dev, int id, ulong rate) |
| 87 | { |
| 88 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 89 | |
| 90 | if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT) |
| 91 | return -EINVAL; |
| 92 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 93 | return clk_set_rate(sbct->clkps[id], rate); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | int sandbox_clk_test_enable(struct udevice *dev, int id) |
| 97 | { |
| 98 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 99 | |
| 100 | if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT) |
| 101 | return -EINVAL; |
| 102 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 103 | return clk_enable(sbct->clkps[id]); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 104 | } |
| 105 | |
Neil Armstrong | 65388d0 | 2018-04-03 11:44:19 +0200 | [diff] [blame] | 106 | int sandbox_clk_test_enable_bulk(struct udevice *dev) |
| 107 | { |
| 108 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 109 | |
| 110 | return clk_enable_bulk(&sbct->bulk); |
| 111 | } |
| 112 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 113 | int sandbox_clk_test_disable(struct udevice *dev, int id) |
| 114 | { |
| 115 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 116 | |
| 117 | if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT) |
| 118 | return -EINVAL; |
| 119 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 120 | return clk_disable(sbct->clkps[id]); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 121 | } |
| 122 | |
Neil Armstrong | 65388d0 | 2018-04-03 11:44:19 +0200 | [diff] [blame] | 123 | int sandbox_clk_test_disable_bulk(struct udevice *dev) |
| 124 | { |
| 125 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 126 | |
| 127 | return clk_disable_bulk(&sbct->bulk); |
| 128 | } |
| 129 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 130 | int sandbox_clk_test_free(struct udevice *dev) |
| 131 | { |
| 132 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 133 | int i, ret; |
| 134 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 135 | devm_clk_put(dev, sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM1]); |
| 136 | for (i = 0; i < SANDBOX_CLK_TEST_NON_DEVM_COUNT; i++) { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 137 | ret = clk_free(&sbct->clks[i]); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Neil Armstrong | 65388d0 | 2018-04-03 11:44:19 +0200 | [diff] [blame] | 145 | int sandbox_clk_test_release_bulk(struct udevice *dev) |
| 146 | { |
| 147 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 148 | |
| 149 | return clk_release_bulk(&sbct->bulk); |
| 150 | } |
| 151 | |
Fabrice Gasnier | 1fe243a | 2018-07-24 16:31:28 +0200 | [diff] [blame] | 152 | int sandbox_clk_test_valid(struct udevice *dev) |
| 153 | { |
| 154 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 155 | int i; |
| 156 | |
| 157 | for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) { |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 158 | if (!clk_valid(sbct->clkps[i])) |
| 159 | if (i != SANDBOX_CLK_TEST_ID_DEVM_NULL) |
| 160 | return -EINVAL; |
Fabrice Gasnier | 1fe243a | 2018-07-24 16:31:28 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 166 | static int sandbox_clk_test_probe(struct udevice *dev) |
| 167 | { |
| 168 | struct sandbox_clk_test *sbct = dev_get_priv(dev); |
| 169 | int i; |
| 170 | |
| 171 | for (i = 0; i < SANDBOX_CLK_TEST_ID_DEVM1; i++) |
| 172 | sbct->clkps[i] = &sbct->clks[i]; |
| 173 | for (i = SANDBOX_CLK_TEST_ID_DEVM1; i < SANDBOX_CLK_TEST_ID_COUNT; i++) |
| 174 | sbct->clkps[i] = NULL; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 179 | static const struct udevice_id sandbox_clk_test_ids[] = { |
| 180 | { .compatible = "sandbox,clk-test" }, |
| 181 | { } |
| 182 | }; |
| 183 | |
| 184 | U_BOOT_DRIVER(sandbox_clk_test) = { |
| 185 | .name = "sandbox_clk_test", |
| 186 | .id = UCLASS_MISC, |
| 187 | .of_match = sandbox_clk_test_ids, |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 188 | .probe = sandbox_clk_test_probe, |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 189 | .priv_auto_alloc_size = sizeof(struct sandbox_clk_test), |
| 190 | }; |