MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / arm / mach-moxart / gpio.c
blob91af2a76ab79446f00606ce71434bb11f9d5bb2c
2 #include <linux/config.h>
3 #include <linux/module.h>
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/spinlock.h>
8 #include <asm/io.h>
9 #include <asm/arch/moxa.h>
10 #include <asm/arch/gpio.h>
12 #define VICTOR_USE_LOCK_IRQ
13 static mcpu_gpio_reg_t *gpio_reg=(mcpu_gpio_reg_t *)CPE_GPIO_VA_BASE;
14 static spinlock_t gpio_lock=SPIN_LOCK_UNLOCKED;
17 * To set the GPIO mode for INPUT or OUTPUT
19 void mcpu_gpio_inout(u32 gpio, int inout)
21 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
22 unsigned long flags;
23 #endif
25 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
26 spin_lock_irqsave(&gpio_lock, flags);
27 #else
28 spin_lock(&gpio_lock);
29 #endif
30 switch ( inout ) {
31 case MCPU_GPIO_INPUT :
32 writel(readl(&gpio_reg->pin_dir)&~gpio, &gpio_reg->pin_dir);
33 break;
34 case MCPU_GPIO_OUTPUT :
35 writel(readl(&gpio_reg->pin_dir)|gpio, &gpio_reg->pin_dir);
36 break;
38 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
39 spin_unlock_irqrestore(&gpio_lock, flags);
40 #else
41 spin_unlock(&gpio_lock);
42 #endif
44 EXPORT_SYMBOL(mcpu_gpio_inout);
47 * To get the GPIO which mode, INPUT or OUTPUT
49 u32 mcpu_gpio_get_inout(u32 gpio)
51 u32 ret;
52 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
53 unsigned long flags;
54 #endif
56 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
57 spin_lock_irqsave(&gpio_lock, flags);
58 #else
59 spin_lock(&gpio_lock);
60 #endif
61 if ( readl(&gpio_reg->pin_dir) & gpio )
62 ret = MCPU_GPIO_OUTPUT;
63 else
64 ret = MCPU_GPIO_INPUT;
65 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
66 spin_unlock_irqrestore(&gpio_lock, flags);
67 #else
68 spin_unlock(&gpio_lock);
69 #endif
70 return ret;
72 EXPORT_SYMBOL(mcpu_gpio_get_inout);
75 * To set the GPIO ouput signal to high or low
77 void mcpu_gpio_set(u32 gpio, int highlow)
79 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
80 unsigned long flags;
81 #endif
83 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
84 spin_lock_irqsave(&gpio_lock, flags);
85 #else
86 spin_lock(&gpio_lock);
87 #endif
88 switch ( highlow ) {
89 case MCPU_GPIO_HIGH :
90 writel(readl(&gpio_reg->data_out)|gpio, &gpio_reg->data_out);
91 break;
92 case MCPU_GPIO_LOW :
93 writel(readl(&gpio_reg->data_out)&~gpio, &gpio_reg->data_out);
94 break;
96 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
97 spin_unlock_irqrestore(&gpio_lock, flags);
98 #else
99 spin_unlock(&gpio_lock);
100 #endif
102 EXPORT_SYMBOL(mcpu_gpio_set);
105 * to get the GPIO input which level, high or low
107 u32 mcpu_gpio_get(u32 gpio)
109 u32 ret;
110 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
111 unsigned long flags;
112 #endif
114 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
115 spin_lock_irqsave(&gpio_lock, flags);
116 #else
117 spin_lock(&gpio_lock);
118 #endif
119 #if 0 // mask by Victor Yu. 04-20-2007
120 ret = readl(&gpio_reg->data_in) & gpio;
121 #else
122 ret = readl(&gpio_reg->pin_dir);
123 if ( ret & gpio ) // this is output GPIO
124 ret = readl(&gpio_reg->data_out) & gpio;
125 else
126 ret = readl(&gpio_reg->data_in) & gpio;
127 #endif
128 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
129 spin_unlock_irqrestore(&gpio_lock, flags);
130 #else
131 spin_unlock(&gpio_lock);
132 #endif
133 return ret;
135 EXPORT_SYMBOL(mcpu_gpio_get);
138 * To set thie GPIO to work GPIO mode
140 void mcpu_gpio_mp_set(u32 gpio)
142 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
143 unsigned long flags;
144 #endif
146 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
147 spin_lock_irqsave(&gpio_lock, flags);
148 #else
149 spin_lock(&gpio_lock);
150 #endif
151 *(volatile unsigned int *)(CPE_PMU_VA_BASE+0x100) |= gpio;
152 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
153 spin_unlock_irqrestore(&gpio_lock, flags);
154 #else
155 spin_unlock(&gpio_lock);
156 #endif
158 EXPORT_SYMBOL(mcpu_gpio_mp_set);
161 * To set the GPIO for component mode
163 void mcpu_gpio_mp_clear(u32 gpio)
165 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
166 unsigned long flags;
167 #endif
169 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
170 spin_lock_irqsave(&gpio_lock, flags);
171 #else
172 spin_lock(&gpio_lock);
173 #endif
174 *(volatile unsigned int *)(CPE_PMU_VA_BASE+0x100) &= ~gpio;
175 #ifdef VICTOR_USE_LOCK_IRQ // add by Victor Yu. 07-25-2007
176 spin_unlock_irqrestore(&gpio_lock, flags);
177 #else
178 spin_unlock(&gpio_lock);
179 #endif
181 EXPORT_SYMBOL(mcpu_gpio_mp_clear);