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