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