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