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