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>
22 #include <linux/init.h>
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25 #include <linux/module.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 */
46 local_irq_save(flags);
48 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
49 if (gpio) /* GPIO: set bit to 0 */
51 else /* Internal function: set bit to 1 */
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 */
71 local_irq_save(flags);
73 /* set pin as input */
74 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
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);
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)
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);
112 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
114 local_irq_restore(flags);
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)
131 /* set pin to GPIO mode */
132 ks8695_gpio_mode(pin, 1);
134 local_irq_save(flags);
137 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
142 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
144 /* set pin as output */
145 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
147 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
149 local_irq_restore(flags);
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)
166 local_irq_save(flags);
168 /* set output line state */
169 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
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)
188 if (pin > KS8695_GPIO_15)
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 */
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))
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;
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)) {
251 seq_printf(s, "(Low)"); break;
253 seq_printf(s, "(High)"); break;
255 seq_printf(s, "(Rising)"); break;
256 case IOPC_TM_FALLING:
257 seq_printf(s, "(Falling)"); break;
259 seq_printf(s, "(Edges)"); break;
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);
269 seq_printf(s, "GPIO\t");
272 seq_printf(s, "GPIO\t");
276 seq_printf(s, "%i\n", (data & IOPD_(i)) ? 1 : 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,
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);
299 postcore_initcall(ks8695_gpio_debugfs_init);