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