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