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