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
21 #include <linux/gpio.h>
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>
36 * Configure a GPIO line for either GPIO function, or its internal
37 * function (Interrupt, Timer, etc).
39 static void ks8695_gpio_mode(unsigned int pin, short gpio)
41 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
42 unsigned long x, flags;
44 if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
47 local_irq_save(flags);
49 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
50 if (gpio) /* GPIO: set bit to 0 */
52 else /* Internal function: set bit to 1 */
54 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
56 local_irq_restore(flags);
60 static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
63 * Configure GPIO pin as external interrupt source.
65 int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
67 unsigned long x, flags;
69 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
72 local_irq_save(flags);
74 /* set pin as input */
75 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
77 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
79 local_irq_restore(flags);
81 /* Set IRQ triggering type */
82 irq_set_irq_type(gpio_irq[pin], type);
84 /* enable interrupt mode */
85 ks8695_gpio_mode(pin, 0);
89 EXPORT_SYMBOL(ks8695_gpio_interrupt);
93 /* .... Generic GPIO interface .............................................. */
96 * Configure the GPIO line as an input.
98 static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
100 unsigned long x, flags;
102 if (pin > KS8695_GPIO_15)
105 /* set pin to GPIO mode */
106 ks8695_gpio_mode(pin, 1);
108 local_irq_save(flags);
110 /* set pin as input */
111 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
113 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
115 local_irq_restore(flags);
122 * Configure the GPIO line as an output, with default state.
124 static int ks8695_gpio_direction_output(struct gpio_chip *gc,
125 unsigned int pin, int state)
127 unsigned long x, flags;
129 if (pin > KS8695_GPIO_15)
132 /* set pin to GPIO mode */
133 ks8695_gpio_mode(pin, 1);
135 local_irq_save(flags);
138 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
143 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
145 /* set pin as output */
146 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
148 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
150 local_irq_restore(flags);
157 * Set the state of an output GPIO line.
159 static void ks8695_gpio_set_value(struct gpio_chip *gc,
160 unsigned int pin, int state)
162 unsigned long x, flags;
164 if (pin > KS8695_GPIO_15)
167 local_irq_save(flags);
169 /* set output line state */
170 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
175 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
177 local_irq_restore(flags);
182 * Read the state of a GPIO line.
184 static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
188 if (pin > KS8695_GPIO_15)
191 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
192 return (x & IOPD(pin)) != 0;
197 * Map GPIO line to IRQ number.
199 static int ks8695_gpio_to_irq(struct gpio_chip *gc, unsigned int pin)
201 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
204 return gpio_irq[pin];
208 * Map IRQ number to GPIO line.
210 int irq_to_gpio(unsigned int irq)
212 if ((irq < KS8695_IRQ_EXTERN0) || (irq > KS8695_IRQ_EXTERN3))
215 return (irq - KS8695_IRQ_EXTERN0);
217 EXPORT_SYMBOL(irq_to_gpio);
219 /* GPIOLIB interface */
221 static struct gpio_chip ks8695_gpio_chip = {
223 .direction_input = ks8695_gpio_direction_input,
224 .direction_output = ks8695_gpio_direction_output,
225 .get = ks8695_gpio_get_value,
226 .set = ks8695_gpio_set_value,
227 .to_irq = ks8695_gpio_to_irq,
233 /* Register the GPIOs */
234 void ks8695_register_gpios(void)
236 if (gpiochip_add(&ks8695_gpio_chip))
237 printk(KERN_ERR "Unable to register core GPIOs\n");
240 /* .... Debug interface ..................................................... */
242 #ifdef CONFIG_DEBUG_FS
244 static int ks8695_gpio_show(struct seq_file *s, void *unused)
246 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
247 unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
248 unsigned long mode, ctrl, data;
251 mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
252 ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
253 data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
255 seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
257 for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
258 seq_printf(s, "%i:\t", i);
260 seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
262 if (i <= KS8695_GPIO_3) {
263 if (ctrl & enable[i]) {
264 seq_printf(s, "EXT%i ", i);
266 switch ((ctrl & intmask[i]) >> (4 * i)) {
268 seq_printf(s, "(Low)"); break;
270 seq_printf(s, "(High)"); break;
272 seq_printf(s, "(Rising)"); break;
273 case IOPC_TM_FALLING:
274 seq_printf(s, "(Falling)"); break;
276 seq_printf(s, "(Edges)"); break;
280 seq_printf(s, "GPIO\t");
282 else if (i <= KS8695_GPIO_5) {
283 if (ctrl & enable[i])
284 seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
286 seq_printf(s, "GPIO\t");
289 seq_printf(s, "GPIO\t");
293 seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
298 static int ks8695_gpio_open(struct inode *inode, struct file *file)
300 return single_open(file, ks8695_gpio_show, NULL);
303 static const struct file_operations ks8695_gpio_operations = {
304 .open = ks8695_gpio_open,
307 .release = single_release,
310 static int __init ks8695_gpio_debugfs_init(void)
312 /* /sys/kernel/debug/ks8695_gpio */
313 (void) debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL, &ks8695_gpio_operations);
316 postcore_initcall(ks8695_gpio_debugfs_init);