Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / arm / mach-ks8695 / gpio.c
blob5e46191c0af967a8cc9a36b30c49c594af8ebc0a
1 /*
2 * arch/arm/mach-ks8695/gpio.c
4 * Copyright (C) 2006 Andrew Victor
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/init.h>
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25 #include <linux/module.h>
27 #include <asm/io.h>
28 #include <asm/hardware.h>
29 #include <asm/mach/irq.h>
31 #include <asm/arch/regs-gpio.h>
32 #include <asm/arch/gpio.h>
35 * Configure a GPIO line for either GPIO function, or its internal
36 * function (Interrupt, Timer, etc).
38 static void __init_or_module ks8695_gpio_mode(unsigned int pin, short gpio)
40 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
41 unsigned long x, flags;
43 if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
44 return;
46 local_irq_save(flags);
48 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
49 if (gpio) /* GPIO: set bit to 0 */
50 x &= ~enable[pin];
51 else /* Internal function: set bit to 1 */
52 x |= enable[pin];
53 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
55 local_irq_restore(flags);
59 static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
62 * Configure GPIO pin as external interrupt source.
64 int __init_or_module ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
66 unsigned long x, flags;
68 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
69 return -EINVAL;
71 local_irq_save(flags);
73 /* set pin as input */
74 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
75 x &= ~IOPM_(pin);
76 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
78 local_irq_restore(flags);
80 /* Set IRQ triggering type */
81 set_irq_type(gpio_irq[pin], type);
83 /* enable interrupt mode */
84 ks8695_gpio_mode(pin, 0);
86 return 0;
88 EXPORT_SYMBOL(ks8695_gpio_interrupt);
92 /* .... Generic GPIO interface .............................................. */
95 * Configure the GPIO line as an input.
97 int __init_or_module gpio_direction_input(unsigned int pin)
99 unsigned long x, flags;
101 if (pin > KS8695_GPIO_15)
102 return -EINVAL;
104 /* set pin to GPIO mode */
105 ks8695_gpio_mode(pin, 1);
107 local_irq_save(flags);
109 /* set pin as input */
110 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
111 x &= ~IOPM_(pin);
112 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
114 local_irq_restore(flags);
116 return 0;
118 EXPORT_SYMBOL(gpio_direction_input);
122 * Configure the GPIO line as an output, with default state.
124 int __init_or_module gpio_direction_output(unsigned int pin, unsigned int state)
126 unsigned long x, flags;
128 if (pin > KS8695_GPIO_15)
129 return -EINVAL;
131 /* set pin to GPIO mode */
132 ks8695_gpio_mode(pin, 1);
134 local_irq_save(flags);
136 /* set line state */
137 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
138 if (state)
139 x |= (1 << pin);
140 else
141 x &= ~(1 << pin);
142 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
144 /* set pin as output */
145 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
146 x |= IOPM_(pin);
147 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
149 local_irq_restore(flags);
151 return 0;
153 EXPORT_SYMBOL(gpio_direction_output);
157 * Set the state of an output GPIO line.
159 void gpio_set_value(unsigned int pin, unsigned int state)
161 unsigned long x, flags;
163 if (pin > KS8695_GPIO_15)
164 return;
166 local_irq_save(flags);
168 /* set output line state */
169 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
170 if (state)
171 x |= (1 << pin);
172 else
173 x &= ~(1 << pin);
174 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
176 local_irq_restore(flags);
178 EXPORT_SYMBOL(gpio_set_value);
182 * Read the state of a GPIO line.
184 int gpio_get_value(unsigned int pin)
186 unsigned long x;
188 if (pin > KS8695_GPIO_15)
189 return -EINVAL;
191 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
192 return (x & (1 << pin)) != 0;
194 EXPORT_SYMBOL(gpio_get_value);
198 * Map GPIO line to IRQ number.
200 int gpio_to_irq(unsigned int pin)
202 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
203 return -EINVAL;
205 return gpio_irq[pin];
207 EXPORT_SYMBOL(gpio_to_irq);
211 * Map IRQ number to GPIO line.
213 int irq_to_gpio(unsigned int irq)
215 if ((irq < KS8695_IRQ_EXTERN0) || (irq > KS8695_IRQ_EXTERN3))
216 return -EINVAL;
218 return (irq - KS8695_IRQ_EXTERN0);
220 EXPORT_SYMBOL(irq_to_gpio);
223 /* .... Debug interface ..................................................... */
225 #ifdef CONFIG_DEBUG_FS
227 static int ks8695_gpio_show(struct seq_file *s, void *unused)
229 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
230 unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
231 unsigned long mode, ctrl, data;
232 int i;
234 mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
235 ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
236 data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
238 seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
240 for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
241 seq_printf(s, "%i:\t", i);
243 seq_printf(s, "%s\t", (mode & IOPM_(i)) ? "Output" : "Input");
245 if (i <= KS8695_GPIO_3) {
246 if (ctrl & enable[i]) {
247 seq_printf(s, "EXT%i ", i);
249 switch ((ctrl & intmask[i]) >> (4 * i)) {
250 case IOPC_TM_LOW:
251 seq_printf(s, "(Low)"); break;
252 case IOPC_TM_HIGH:
253 seq_printf(s, "(High)"); break;
254 case IOPC_TM_RISING:
255 seq_printf(s, "(Rising)"); break;
256 case IOPC_TM_FALLING:
257 seq_printf(s, "(Falling)"); break;
258 case IOPC_TM_EDGE:
259 seq_printf(s, "(Edges)"); break;
262 else
263 seq_printf(s, "GPIO\t");
265 else if (i <= KS8695_GPIO_5) {
266 if (ctrl & enable[i])
267 seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
268 else
269 seq_printf(s, "GPIO\t");
271 else
272 seq_printf(s, "GPIO\t");
274 seq_printf(s, "\t");
276 seq_printf(s, "%i\n", (data & IOPD_(i)) ? 1 : 0);
278 return 0;
281 static int ks8695_gpio_open(struct inode *inode, struct file *file)
283 return single_open(file, ks8695_gpio_show, NULL);
286 static const struct file_operations ks8695_gpio_operations = {
287 .open = ks8695_gpio_open,
288 .read = seq_read,
289 .llseek = seq_lseek,
290 .release = single_release,
293 static int __init ks8695_gpio_debugfs_init(void)
295 /* /sys/kernel/debug/ks8695_gpio */
296 (void) debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL, &ks8695_gpio_operations);
297 return 0;
299 postcore_initcall(ks8695_gpio_debugfs_init);
301 #endif