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 { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 13 | ulong rate[SANDBOX_CLK_ID_COUNT]; |
| 14 | bool enabled[SANDBOX_CLK_ID_COUNT]; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 15 | }; |
| 16 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 17 | static ulong sandbox_clk_get_rate(struct clk *clk) |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 18 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 19 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 20 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 21 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 22 | return -EINVAL; |
| 23 | |
| 24 | return priv->rate[clk->id]; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 25 | } |
| 26 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 27 | static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate) |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 28 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 29 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 30 | ulong old_rate; |
| 31 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 32 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 33 | return -EINVAL; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 34 | |
| 35 | if (!rate) |
| 36 | return -EINVAL; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 37 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 38 | old_rate = priv->rate[clk->id]; |
| 39 | priv->rate[clk->id] = rate; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 40 | |
| 41 | return old_rate; |
| 42 | } |
| 43 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 44 | static int sandbox_clk_enable(struct clk *clk) |
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 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 47 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 48 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 49 | return -EINVAL; |
| 50 | |
| 51 | priv->enabled[clk->id] = true; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int sandbox_clk_disable(struct clk *clk) |
| 57 | { |
| 58 | struct sandbox_clk_priv *priv = dev_get_priv(clk->dev); |
| 59 | |
Stephen Warren | df8b0a0 | 2016-06-21 13:32:07 -0600 | [diff] [blame] | 60 | if (clk->id >= SANDBOX_CLK_ID_COUNT) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 61 | return -EINVAL; |
| 62 | |
| 63 | priv->enabled[clk->id] = false; |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static struct clk_ops sandbox_clk_ops = { |
| 69 | .get_rate = sandbox_clk_get_rate, |
| 70 | .set_rate = sandbox_clk_set_rate, |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 71 | .enable = sandbox_clk_enable, |
| 72 | .disable = sandbox_clk_disable, |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static const struct udevice_id sandbox_clk_ids[] = { |
| 76 | { .compatible = "sandbox,clk" }, |
| 77 | { } |
| 78 | }; |
| 79 | |
| 80 | U_BOOT_DRIVER(clk_sandbox) = { |
| 81 | .name = "clk_sandbox", |
| 82 | .id = UCLASS_CLK, |
| 83 | .of_match = sandbox_clk_ids, |
| 84 | .ops = &sandbox_clk_ops, |
| 85 | .priv_auto_alloc_size = sizeof(struct sandbox_clk_priv), |
Simon Glass | 6a1c7ce | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 86 | }; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 87 | |
| 88 | ulong sandbox_clk_query_rate(struct udevice *dev, int id) |
| 89 | { |
| 90 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 91 | |
| 92 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | return priv->rate[id]; |
| 96 | } |
| 97 | |
| 98 | int sandbox_clk_query_enable(struct udevice *dev, int id) |
| 99 | { |
| 100 | struct sandbox_clk_priv *priv = dev_get_priv(dev); |
| 101 | |
| 102 | if (id < 0 || id >= SANDBOX_CLK_ID_COUNT) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | return priv->enabled[id]; |
| 106 | } |