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> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 10 | #include <asm/clk.h> |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 11 | |
| 12 | struct sandbox_clk_priv { |
Jean-Jacques Hiblot | 9a52be1 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 13 | bool probed; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 14 | ulong rate[SANDBOX_CLK_ID_COUNT]; |
| 15 | bool enabled[SANDBOX_CLK_ID_COUNT]; |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 16 | bool requested[SANDBOX_CLK_ID_COUNT]; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 17 | }; |
| 18 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 19 | static ulong sandbox_clk_get_rate(struct clk *clk) |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 20 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 21 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 22 | |
Jean-Jacques Hiblot | 9a52be1 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 23 | if (!priv->probed) |
| 24 | return -ENODEV; |
| 25 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 26 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 27 | return -EINVAL; |
| 28 | |
| 29 | return priv->rate[clk->id]; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 30 | } |
| 31 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 32 | static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate) |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 33 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 34 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 35 | ulong old_rate; |
| 36 | |
Jean-Jacques Hiblot | 9a52be1 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 37 | if (!priv->probed) |
| 38 | return -ENODEV; |
| 39 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 40 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 41 | return -EINVAL; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 42 | |
| 43 | if (!rate) |
| 44 | return -EINVAL; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 45 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 46 | old_rate = priv->rate[clk->id]; |
| 47 | priv->rate[clk->id] = rate; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 48 | |
| 49 | return old_rate; |
| 50 | } |
| 51 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 52 | static int sandbox_clk_enable(struct clk *clk) |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 53 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 54 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 55 | |
Jean-Jacques Hiblot | 9a52be1 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 56 | if (!priv->probed) |
| 57 | return -ENODEV; |
| 58 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 59 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 60 | return -EINVAL; |
| 61 | |
| 62 | priv->enabled[clk->id] = true; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int sandbox_clk_disable(struct clk *clk) |
| 68 | { |
| 69 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 70 | |
Jean-Jacques Hiblot | 9a52be1 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 71 | if (!priv->probed) |
| 72 | return -ENODEV; |
| 73 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 74 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 75 | return -EINVAL; |
| 76 | |
| 77 | priv->enabled[clk->id] = false; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Jean-Jacques Hiblot | dd2e0ce | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 82 | static int sandbox_clk_request(struct clk *clk) |
| 83 | { |
| 84 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 85 | |
| 86 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | priv->requested[clk->id] = true; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int sandbox_clk_free(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] = false; |
| 101 | return 0; |
| 102 | } |
| 103 | |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 104 | static struct clk_ops sandbox_clk_ops = { |
| 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 | fb8c0d5 | 2020-02-03 07:35:54 -0700 | [diff] [blame^] | 110 | .rfree = sandbox_clk_free, |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 111 | }; |
| 112 | |
Jean-Jacques Hiblot | 9a52be1 | 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 | 6a1c7ce | 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 | |
| 126 | U_BOOT_DRIVER(clk_sandbox) = { |
| 127 | .name = "clk_sandbox", |
| 128 | .id = UCLASS_CLK, |
| 129 | .of_match = sandbox_clk_ids, |
| 130 | .ops = &sandbox_clk_ops, |
Jean-Jacques Hiblot | 9a52be1 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 131 | .probe = sandbox_clk_probe, |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 132 | .priv_auto_alloc_size = sizeof(struct sandbox_clk_priv), |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 133 | }; |
Stephen Warren | 135aa95 | 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 | dd2e0ce | 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 | } |