Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-sa1100/clock.c |
| 3 | */ |
| 4 | #include <linux/module.h> |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/list.h> |
| 7 | #include <linux/errno.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/spinlock.h> |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/delay.h> |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 14 | |
| 15 | #include <asm/arch/pxa-regs.h> |
| 16 | #include <asm/hardware.h> |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 17 | |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 18 | #include "devices.h" |
| 19 | #include "generic.h" |
| 20 | #include "clock.h" |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 21 | |
| 22 | static LIST_HEAD(clocks); |
Russell King | f4b6a0a | 2007-05-15 16:49:02 +0100 | [diff] [blame] | 23 | static DEFINE_MUTEX(clocks_mutex); |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 24 | static DEFINE_SPINLOCK(clocks_lock); |
| 25 | |
Russell King | a0dd005 | 2008-02-17 10:35:15 +0000 | [diff] [blame^] | 26 | static struct clk *clk_lookup(struct device *dev, const char *id) |
| 27 | { |
| 28 | struct clk *p; |
| 29 | |
| 30 | list_for_each_entry(p, &clocks, node) |
| 31 | if (strcmp(id, p->name) == 0 && p->dev == dev) |
| 32 | return p; |
| 33 | |
| 34 | return NULL; |
| 35 | } |
| 36 | |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 37 | struct clk *clk_get(struct device *dev, const char *id) |
| 38 | { |
| 39 | struct clk *p, *clk = ERR_PTR(-ENOENT); |
| 40 | |
Russell King | f4b6a0a | 2007-05-15 16:49:02 +0100 | [diff] [blame] | 41 | mutex_lock(&clocks_mutex); |
Russell King | a0dd005 | 2008-02-17 10:35:15 +0000 | [diff] [blame^] | 42 | p = clk_lookup(dev, id); |
| 43 | if (!p) |
| 44 | p = clk_lookup(NULL, id); |
| 45 | if (p) |
| 46 | clk = p; |
Russell King | f4b6a0a | 2007-05-15 16:49:02 +0100 | [diff] [blame] | 47 | mutex_unlock(&clocks_mutex); |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 48 | |
| 49 | return clk; |
| 50 | } |
| 51 | EXPORT_SYMBOL(clk_get); |
| 52 | |
| 53 | void clk_put(struct clk *clk) |
| 54 | { |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 55 | } |
| 56 | EXPORT_SYMBOL(clk_put); |
| 57 | |
| 58 | int clk_enable(struct clk *clk) |
| 59 | { |
| 60 | unsigned long flags; |
| 61 | |
| 62 | spin_lock_irqsave(&clocks_lock, flags); |
| 63 | if (clk->enabled++ == 0) |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 64 | clk->ops->enable(clk); |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 65 | spin_unlock_irqrestore(&clocks_lock, flags); |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 66 | |
| 67 | if (clk->delay) |
| 68 | udelay(clk->delay); |
| 69 | |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | EXPORT_SYMBOL(clk_enable); |
| 73 | |
| 74 | void clk_disable(struct clk *clk) |
| 75 | { |
| 76 | unsigned long flags; |
| 77 | |
| 78 | WARN_ON(clk->enabled == 0); |
| 79 | |
| 80 | spin_lock_irqsave(&clocks_lock, flags); |
| 81 | if (--clk->enabled == 0) |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 82 | clk->ops->disable(clk); |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 83 | spin_unlock_irqrestore(&clocks_lock, flags); |
| 84 | } |
| 85 | EXPORT_SYMBOL(clk_disable); |
| 86 | |
| 87 | unsigned long clk_get_rate(struct clk *clk) |
| 88 | { |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 89 | unsigned long rate; |
| 90 | |
| 91 | rate = clk->rate; |
| 92 | if (clk->ops->getrate) |
| 93 | rate = clk->ops->getrate(clk); |
| 94 | |
| 95 | return rate; |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 96 | } |
| 97 | EXPORT_SYMBOL(clk_get_rate); |
| 98 | |
| 99 | |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 100 | static void clk_gpio27_enable(struct clk *clk) |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 101 | { |
| 102 | pxa_gpio_mode(GPIO11_3_6MHz_MD); |
| 103 | } |
| 104 | |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 105 | static void clk_gpio27_disable(struct clk *clk) |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 106 | { |
| 107 | } |
| 108 | |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 109 | static const struct clkops clk_gpio27_ops = { |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 110 | .enable = clk_gpio27_enable, |
| 111 | .disable = clk_gpio27_disable, |
| 112 | }; |
| 113 | |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 114 | |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 115 | void clk_cken_enable(struct clk *clk) |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 116 | { |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 117 | CKEN |= 1 << clk->cken; |
| 118 | } |
| 119 | |
| 120 | void clk_cken_disable(struct clk *clk) |
| 121 | { |
| 122 | CKEN &= ~(1 << clk->cken); |
| 123 | } |
| 124 | |
| 125 | const struct clkops clk_cken_ops = { |
| 126 | .enable = clk_cken_enable, |
| 127 | .disable = clk_cken_disable, |
| 128 | }; |
| 129 | |
| 130 | static struct clk common_clks[] = { |
| 131 | { |
| 132 | .name = "GPIO27_CLK", |
| 133 | .ops = &clk_gpio27_ops, |
| 134 | .rate = 3686400, |
| 135 | }, |
| 136 | }; |
| 137 | |
| 138 | void clks_register(struct clk *clks, size_t num) |
| 139 | { |
| 140 | int i; |
| 141 | |
Russell King | f4b6a0a | 2007-05-15 16:49:02 +0100 | [diff] [blame] | 142 | mutex_lock(&clocks_mutex); |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 143 | for (i = 0; i < num; i++) |
| 144 | list_add(&clks[i].node, &clocks); |
Russell King | f4b6a0a | 2007-05-15 16:49:02 +0100 | [diff] [blame] | 145 | mutex_unlock(&clocks_mutex); |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 146 | } |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 147 | |
| 148 | static int __init clk_init(void) |
| 149 | { |
Russell King | a6dba20 | 2007-08-20 10:18:02 +0100 | [diff] [blame] | 150 | clks_register(common_clks, ARRAY_SIZE(common_clks)); |
Russell King | 97d654f | 2006-03-15 15:54:37 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | arch_initcall(clk_init); |