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 | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 12 | |
| 13 | struct sandbox_clk_priv { |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 14 | bool probed; |
Stephen Warren | a962243 | 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 | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 17 | bool requested[SANDBOX_CLK_ID_COUNT]; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 18 | }; |
| 19 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 20 | static ulong sandbox_clk_get_rate(struct clk *clk) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 21 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 22 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 23 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 24 | if (!priv->probed) |
| 25 | return -ENODEV; |
| 26 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 27 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 28 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 29 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 30 | return priv->rate[clk->id]; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 31 | } |
| 32 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 33 | static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 34 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 35 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 36 | ulong old_rate; |
| 37 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 38 | if (!priv->probed) |
| 39 | return -ENODEV; |
| 40 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 41 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 42 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 43 | |
| 44 | if (!rate) |
| 45 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 46 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 47 | old_rate = priv->rate[clk->id]; |
| 48 | priv->rate[clk->id] = rate; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 49 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 50 | return old_rate; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 51 | } |
| 52 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 53 | static int sandbox_clk_enable(struct clk *clk) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 54 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 55 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 56 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 57 | if (!priv->probed) |
| 58 | return -ENODEV; |
| 59 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 60 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 61 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 62 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 63 | priv->enabled[clk->id] = true; |
| 64 | |
| 65 | return 0; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 68 | static int sandbox_clk_disable(struct clk *clk) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 69 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 70 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 71 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 72 | if (!priv->probed) |
| 73 | return -ENODEV; |
| 74 | |
Stephen Warren | daec47b | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 75 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 76 | return -EINVAL; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 77 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 78 | priv->enabled[clk->id] = false; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 83 | static int sandbox_clk_request(struct clk *clk) |
| 84 | { |
| 85 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 86 | |
| 87 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | priv->requested[clk->id] = true; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int sandbox_clk_free(struct clk *clk) |
| 95 | { |
| 96 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 97 | |
| 98 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | priv->requested[clk->id] = false; |
| 102 | return 0; |
| 103 | } |
| 104 | |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 105 | static struct clk_ops sandbox_clk_ops = { |
| 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 | 2cdd3f4 | 2020-02-03 07:35:54 -0700 | [diff] [blame] | 111 | .rfree = sandbox_clk_free, |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 112 | }; |
| 113 | |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 114 | static int sandbox_clk_probe(struct udevice *dev) |
| 115 | { |
| 116 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 117 | |
| 118 | priv->probed = true; |
| 119 | return 0; |
| 120 | } |
| 121 | |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 122 | static const struct udevice_id sandbox_clk_ids[] = { |
| 123 | { .compatible = "sandbox,clk" }, |
| 124 | { } |
| 125 | }; |
| 126 | |
| 127 | U_BOOT_DRIVER(clk_sandbox) = { |
| 128 | .name = "clk_sandbox", |
| 129 | .id = UCLASS_CLK, |
| 130 | .of_match = sandbox_clk_ids, |
| 131 | .ops = &sandbox_clk_ops, |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 132 | .probe = sandbox_clk_probe, |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 133 | .priv_auto_alloc_size = sizeof(struct sandbox_clk_priv), |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 134 | }; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 135 | |
| 136 | ulong sandbox_clk_query_rate(struct udevice *dev, int id) |
| 137 | { |
| 138 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 139 | |
| 140 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | return priv->rate[id]; |
| 144 | } |
| 145 | |
| 146 | int sandbox_clk_query_enable(struct udevice *dev, int id) |
| 147 | { |
| 148 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 149 | |
| 150 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | return priv->enabled[id]; |
| 154 | } |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 155 | |
| 156 | int sandbox_clk_query_requested(struct udevice *dev, int id) |
| 157 | { |
| 158 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 159 | |
| 160 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 161 | return -EINVAL; |
| 162 | return priv->requested[id]; |
| 163 | } |