Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 7 | #include <clk-uclass.h> |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 11 | #include <asm/clk.h> |
Simon Glass | 9bb88fb | 2021-03-15 17:25:24 +1300 | [diff] [blame] | 12 | #include <linux/clk-provider.h> |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 13 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 14 | static ulong sandbox_clk_get_rate(struct clk *clk) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 15 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 16 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 17 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 18 | if (!priv->probed) |
| 19 | return -ENODEV; |
| 20 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 21 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 22 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 23 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 24 | return priv->rate[clk->id]; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 25 | } |
| 26 | |
Dario Binacchi | b7f8589 | 2020-12-30 00:06:31 +0100 | [diff] [blame] | 27 | static ulong sandbox_clk_round_rate(struct clk *clk, ulong rate) |
| 28 | { |
| 29 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 30 | |
| 31 | if (!priv->probed) |
| 32 | return -ENODEV; |
| 33 | |
| 34 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
| 35 | return -EINVAL; |
| 36 | |
| 37 | if (!rate) |
| 38 | return -EINVAL; |
| 39 | |
| 40 | return rate; |
| 41 | } |
| 42 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 43 | static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 44 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 45 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 46 | ulong old_rate; |
| 47 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 48 | if (!priv->probed) |
| 49 | return -ENODEV; |
| 50 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 51 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 52 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 53 | |
| 54 | if (!rate) |
| 55 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 56 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 57 | old_rate = priv->rate[clk->id]; |
| 58 | priv->rate[clk->id] = rate; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 59 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 60 | return old_rate; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 61 | } |
| 62 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 63 | static int sandbox_clk_enable(struct clk *clk) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 64 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 65 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 66 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 67 | if (!priv->probed) |
| 68 | return -ENODEV; |
| 69 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 70 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 71 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 72 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 73 | priv->enabled[clk->id] = true; |
| 74 | |
| 75 | return 0; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 76 | } |
| 77 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 78 | static int sandbox_clk_disable(struct clk *clk) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 79 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 80 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 81 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 82 | if (!priv->probed) |
| 83 | return -ENODEV; |
| 84 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 85 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 86 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 87 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 88 | priv->enabled[clk->id] = false; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 93 | static int sandbox_clk_request(struct clk *clk) |
| 94 | { |
| 95 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 96 | |
| 97 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | priv->requested[clk->id] = true; |
| 101 | return 0; |
| 102 | } |
| 103 | |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 104 | static struct clk_ops sandbox_clk_ops = { |
Dario Binacchi | b7f8589 | 2020-12-30 00:06:31 +0100 | [diff] [blame] | 105 | .round_rate = sandbox_clk_round_rate, |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 106 | .get_rate = sandbox_clk_get_rate, |
| 107 | .set_rate = sandbox_clk_set_rate, |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 108 | .enable = sandbox_clk_enable, |
| 109 | .disable = sandbox_clk_disable, |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 110 | .request = sandbox_clk_request, |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 111 | }; |
| 112 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 113 | static int sandbox_clk_probe(struct udevice *dev) |
| 114 | { |
| 115 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 116 | |
| 117 | priv->probed = true; |
| 118 | return 0; |
| 119 | } |
| 120 | |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 121 | static const struct udevice_id sandbox_clk_ids[] = { |
| 122 | { .compatible = "sandbox,clk" }, |
| 123 | { } |
| 124 | }; |
| 125 | |
Simon Glass | 6b927b1 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 126 | U_BOOT_DRIVER(sandbox_clk) = { |
| 127 | .name = "sandbox_clk", |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 128 | .id = UCLASS_CLK, |
| 129 | .of_match = sandbox_clk_ids, |
| 130 | .ops = &sandbox_clk_ops, |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 131 | .probe = sandbox_clk_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 132 | .priv_auto = sizeof(struct sandbox_clk_priv), |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 133 | }; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 134 | |
| 135 | ulong sandbox_clk_query_rate(struct udevice *dev, int id) |
| 136 | { |
| 137 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 138 | |
| 139 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | return priv->rate[id]; |
| 143 | } |
| 144 | |
| 145 | int sandbox_clk_query_enable(struct udevice *dev, int id) |
| 146 | { |
| 147 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 148 | |
| 149 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | return priv->enabled[id]; |
| 153 | } |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 154 | |
| 155 | int sandbox_clk_query_requested(struct udevice *dev, int id) |
| 156 | { |
| 157 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 158 | |
| 159 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 160 | return -EINVAL; |
| 161 | return priv->requested[id]; |
| 162 | } |
Simon Glass | 9bb88fb | 2021-03-15 17:25:24 +1300 | [diff] [blame] | 163 | |
| 164 | int clk_fixed_rate_of_to_plat(struct udevice *dev) |
| 165 | { |
| 166 | struct clk_fixed_rate *cplat; |
| 167 | |
| 168 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 169 | struct sandbox_clk_fixed_rate_plat *plat = dev_get_plat(dev); |
| 170 | |
| 171 | cplat = &plat->fixed; |
| 172 | cplat->fixed_rate = plat->dtplat.clock_frequency; |
| 173 | #else |
| 174 | cplat = to_clk_fixed_rate(dev); |
| 175 | #endif |
| 176 | clk_fixed_rate_ofdata_to_plat_(dev, cplat); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static const struct udevice_id sandbox_clk_fixed_rate_match[] = { |
| 182 | { .compatible = "sandbox,fixed-clock" }, |
| 183 | { /* sentinel */ } |
| 184 | }; |
| 185 | |
| 186 | U_BOOT_DRIVER(sandbox_fixed_clock) = { |
| 187 | .name = "sandbox_fixed_clock", |
| 188 | .id = UCLASS_CLK, |
| 189 | .of_match = sandbox_clk_fixed_rate_match, |
| 190 | .of_to_plat = clk_fixed_rate_of_to_plat, |
| 191 | .plat_auto = sizeof(struct sandbox_clk_fixed_rate_plat), |
| 192 | .ops = &clk_fixed_rate_ops, |
| 193 | .flags = DM_FLAG_PRE_RELOC, |
| 194 | }; |