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/module.h>
26 #include <asm/hardware.h>
27 #include <asm/mach/irq.h>
29 #include <asm/arch/regs-gpio.h>
30 #include <asm/arch/gpio.h>
33 * Configure a GPIO line for either GPIO function, or its internal
34 * function (Interrupt, Timer, etc).
36 static void __init_or_module
ks8695_gpio_mode(unsigned int pin
, short gpio
)
38 unsigned int enable
[] = { IOPC_IOEINT0EN
, IOPC_IOEINT1EN
, IOPC_IOEINT2EN
, IOPC_IOEINT3EN
, IOPC_IOTIM0EN
, IOPC_IOTIM1EN
};
39 unsigned long x
, flags
;
41 if (pin
> KS8695_GPIO_5
) /* only GPIO 0..5 have internal functions */
44 local_irq_save(flags
);
46 x
= __raw_readl(KS8695_GPIO_VA
+ KS8695_IOPC
);
47 if (gpio
) /* GPIO: set bit to 0 */
49 else /* Internal function: set bit to 1 */
51 __raw_writel(x
, KS8695_GPIO_VA
+ KS8695_IOPC
);
53 local_irq_restore(flags
);
57 static unsigned short gpio_irq
[] = { KS8695_IRQ_EXTERN0
, KS8695_IRQ_EXTERN1
, KS8695_IRQ_EXTERN2
, KS8695_IRQ_EXTERN3
};
60 * Configure GPIO pin as external interrupt source.
62 int __init_or_module
ks8695_gpio_interrupt(unsigned int pin
, unsigned int type
)
64 unsigned long x
, flags
;
66 if (pin
> KS8695_GPIO_3
) /* only GPIO 0..3 can generate IRQ */
69 local_irq_save(flags
);
71 /* set pin as input */
72 x
= __raw_readl(KS8695_GPIO_VA
+ KS8695_IOPM
);
74 __raw_writel(x
, KS8695_GPIO_VA
+ KS8695_IOPM
);
76 local_irq_restore(flags
);
78 /* Set IRQ triggering type */
79 set_irq_type(gpio_irq
[pin
], type
);
81 /* enable interrupt mode */
82 ks8695_gpio_mode(pin
, 0);
86 EXPORT_SYMBOL(ks8695_gpio_interrupt
);
90 /* .... Generic GPIO interface .............................................. */
93 * Configure the GPIO line as an input.
95 int __init_or_module
gpio_direction_input(unsigned int pin
)
97 unsigned long x
, flags
;
99 if (pin
> KS8695_GPIO_15
)
102 /* set pin to GPIO mode */
103 ks8695_gpio_mode(pin
, 1);
105 local_irq_save(flags
);
107 /* set pin as input */
108 x
= __raw_readl(KS8695_GPIO_VA
+ KS8695_IOPM
);
110 __raw_writel(x
, KS8695_GPIO_VA
+ KS8695_IOPM
);
112 local_irq_restore(flags
);
116 EXPORT_SYMBOL(gpio_direction_input
);
120 * Configure the GPIO line as an output, with default state.
122 int __init_or_module
gpio_direction_output(unsigned int pin
, unsigned int state
)
124 unsigned long x
, flags
;
126 if (pin
> KS8695_GPIO_15
)
129 /* set pin to GPIO mode */
130 ks8695_gpio_mode(pin
, 1);
132 local_irq_save(flags
);
135 x
= __raw_readl(KS8695_GPIO_VA
+ KS8695_IOPD
);
140 __raw_writel(x
, KS8695_GPIO_VA
+ KS8695_IOPD
);
142 /* set pin as output */
143 x
= __raw_readl(KS8695_GPIO_VA
+ KS8695_IOPM
);
145 __raw_writel(x
, KS8695_GPIO_VA
+ KS8695_IOPM
);
147 local_irq_restore(flags
);
151 EXPORT_SYMBOL(gpio_direction_output
);
155 * Set the state of an output GPIO line.
157 void gpio_set_value(unsigned int pin
, unsigned int state
)
159 unsigned long x
, flags
;
161 if (pin
> KS8695_GPIO_15
)
164 local_irq_save(flags
);
166 /* set output line state */
167 x
= __raw_readl(KS8695_GPIO_VA
+ KS8695_IOPD
);
172 __raw_writel(x
, KS8695_GPIO_VA
+ KS8695_IOPD
);
174 local_irq_restore(flags
);
176 EXPORT_SYMBOL(gpio_set_value
);
180 * Read the state of a GPIO line.
182 int gpio_get_value(unsigned int pin
)
186 if (pin
> KS8695_GPIO_15
)
189 x
= __raw_readl(KS8695_GPIO_VA
+ KS8695_IOPD
);
190 return (x
& (1 << pin
)) != 0;
192 EXPORT_SYMBOL(gpio_get_value
);
196 * Map GPIO line to IRQ number.
198 int gpio_to_irq(unsigned int pin
)
200 if (pin
> KS8695_GPIO_3
) /* only GPIO 0..3 can generate IRQ */
203 return gpio_irq
[pin
];
205 EXPORT_SYMBOL(gpio_to_irq
);
209 * Map IRQ number to GPIO line.
211 int irq_to_gpio(unsigned int irq
)
213 if ((irq
< KS8695_IRQ_EXTERN0
) || (irq
> KS8695_IRQ_EXTERN3
))
216 return (irq
- KS8695_IRQ_EXTERN0
);
218 EXPORT_SYMBOL(irq_to_gpio
);