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