2 * arch/arm/mach-ks8695/gpio.c
4 * Copyright (C) 2006 Andrew Victor
5 * Updated to GPIOLIB, Copyright 2008 Simtec Electronics
6 * Daniel Silverstone <dsilvers@simtec.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/module.h>
30 #include <mach/hardware.h>
31 #include <asm/mach/irq.h>
33 #include <mach/regs-gpio.h>
34 #include <mach/gpio.h>
37 * Configure a GPIO line for either GPIO function, or its internal
38 * function (Interrupt, Timer, etc).
40 static void ks8695_gpio_mode(unsigned int pin, short gpio)
42 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
43 unsigned long x, flags;
45 if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
48 local_irq_save(flags);
50 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
51 if (gpio) /* GPIO: set bit to 0 */
53 else /* Internal function: set bit to 1 */
55 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
57 local_irq_restore(flags);
61 static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
64 * Configure GPIO pin as external interrupt source.
66 int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
68 unsigned long x, flags;
70 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
73 local_irq_save(flags);
75 /* set pin as input */
76 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
78 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
80 local_irq_restore(flags);
82 /* Set IRQ triggering type */
83 set_irq_type(gpio_irq[pin], type);
85 /* enable interrupt mode */
86 ks8695_gpio_mode(pin, 0);
90 EXPORT_SYMBOL(ks8695_gpio_interrupt);
94 /* .... Generic GPIO interface .............................................. */
97 * Configure the GPIO line as an input.
99 static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
101 unsigned long x, flags;
103 if (pin > KS8695_GPIO_15)
106 /* set pin to GPIO mode */
107 ks8695_gpio_mode(pin, 1);
109 local_irq_save(flags);
111 /* set pin as input */
112 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
114 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
116 local_irq_restore(flags);
123 * Configure the GPIO line as an output, with default state.
125 static int ks8695_gpio_direction_output(struct gpio_chip *gc,
126 unsigned int pin, int state)
128 unsigned long x, flags;
130 if (pin > KS8695_GPIO_15)
133 /* set pin to GPIO mode */
134 ks8695_gpio_mode(pin, 1);
136 local_irq_save(flags);
139 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
144 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
146 /* set pin as output */
147 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
149 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
151 local_irq_restore(flags);
158 * Set the state of an output GPIO line.
160 static void ks8695_gpio_set_value(struct gpio_chip *gc,
161 unsigned int pin, int state)
163 unsigned long x, flags;
165 if (pin > KS8695_GPIO_15)
168 local_irq_save(flags);
170 /* set output line state */
171 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
176 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
178 local_irq_restore(flags);
183 * Read the state of a GPIO line.
185 static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
189 if (pin > KS8695_GPIO_15)
192 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
193 return (x & IOPD(pin)) != 0;
198 * Map GPIO line to IRQ number.
200 static int ks8695_gpio_to_irq(struct gpio_chip *gc, unsigned int pin)
202 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
205 return gpio_irq[pin];
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);
220 /* GPIOLIB interface */
222 static struct gpio_chip ks8695_gpio_chip = {
224 .direction_input = ks8695_gpio_direction_input,
225 .direction_output = ks8695_gpio_direction_output,
226 .get = ks8695_gpio_get_value,
227 .set = ks8695_gpio_set_value,
228 .to_irq = ks8695_gpio_to_irq,
234 /* Register the GPIOs */
235 void ks8695_register_gpios(void)
237 if (gpiochip_add(&ks8695_gpio_chip))
238 printk(KERN_ERR "Unable to register core GPIOs\n");
241 /* .... Debug interface ..................................................... */
243 #ifdef CONFIG_DEBUG_FS
245 static int ks8695_gpio_show(struct seq_file *s, void *unused)
247 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
248 unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
249 unsigned long mode, ctrl, data;
252 mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
253 ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
254 data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
256 seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
258 for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
259 seq_printf(s, "%i:\t", i);
261 seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
263 if (i <= KS8695_GPIO_3) {
264 if (ctrl & enable[i]) {
265 seq_printf(s, "EXT%i ", i);
267 switch ((ctrl & intmask[i]) >> (4 * i)) {
269 seq_printf(s, "(Low)"); break;
271 seq_printf(s, "(High)"); break;
273 seq_printf(s, "(Rising)"); break;
274 case IOPC_TM_FALLING:
275 seq_printf(s, "(Falling)"); break;
277 seq_printf(s, "(Edges)"); break;
281 seq_printf(s, "GPIO\t");
283 else if (i <= KS8695_GPIO_5) {
284 if (ctrl & enable[i])
285 seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
287 seq_printf(s, "GPIO\t");
290 seq_printf(s, "GPIO\t");
294 seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
299 static int ks8695_gpio_open(struct inode *inode, struct file *file)
301 return single_open(file, ks8695_gpio_show, NULL);
304 static const struct file_operations ks8695_gpio_operations = {
305 .open = ks8695_gpio_open,
308 .release = single_release,
311 static int __init ks8695_gpio_debugfs_init(void)
313 /* /sys/kernel/debug/ks8695_gpio */
314 (void) debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL, &ks8695_gpio_operations);
317 postcore_initcall(ks8695_gpio_debugfs_init);