blob: b3fdfe1d262f4127222f4c786e8d4f3c87cedc61 [file] [log] [blame]
Andrew Victor8a87a992007-05-14 14:30:15 +01001/*
2 * arch/arm/mach-ks8695/gpio.c
3 *
4 * Copyright (C) 2006 Andrew Victor
Daniel Silverstone72880ad2008-12-13 20:44:12 +00005 * Updated to GPIOLIB, Copyright 2008 Simtec Electronics
6 * Daniel Silverstone <dsilvers@simtec.co.uk>
Andrew Victor8a87a992007-05-14 14:30:15 +01007 *
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.
11 *
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.
16 *
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
20 */
Russell King2f8163b2011-07-26 10:53:52 +010021#include <linux/gpio.h>
Andrew Victor8a87a992007-05-14 14:30:15 +010022#include <linux/kernel.h>
23#include <linux/mm.h>
24#include <linux/init.h>
Andrew Victor20118ff2007-10-15 14:27:41 +010025#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Andrew Victor8a87a992007-05-14 14:30:15 +010027#include <linux/module.h>
Russell Kingfced80c2008-09-06 12:10:45 +010028#include <linux/io.h>
Andrew Victor8a87a992007-05-14 14:30:15 +010029
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/hardware.h>
Andrew Victor8a87a992007-05-14 14:30:15 +010031#include <asm/mach/irq.h>
32
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/regs-gpio.h>
Andrew Victor8a87a992007-05-14 14:30:15 +010034
35/*
36 * Configure a GPIO line for either GPIO function, or its internal
37 * function (Interrupt, Timer, etc).
38 */
Daniel Silverstone72880ad2008-12-13 20:44:12 +000039static void ks8695_gpio_mode(unsigned int pin, short gpio)
Andrew Victor8a87a992007-05-14 14:30:15 +010040{
41 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
42 unsigned long x, flags;
43
44 if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
45 return;
46
47 local_irq_save(flags);
48
49 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
50 if (gpio) /* GPIO: set bit to 0 */
51 x &= ~enable[pin];
52 else /* Internal function: set bit to 1 */
53 x |= enable[pin];
54 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
55
56 local_irq_restore(flags);
57}
58
59
60static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
61
62/*
63 * Configure GPIO pin as external interrupt source.
64 */
Daniel Silverstone72880ad2008-12-13 20:44:12 +000065int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
Andrew Victor8a87a992007-05-14 14:30:15 +010066{
67 unsigned long x, flags;
68
69 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
70 return -EINVAL;
71
72 local_irq_save(flags);
73
74 /* set pin as input */
75 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
Andrew Victor8d163b32008-10-07 20:20:15 +010076 x &= ~IOPM(pin);
Andrew Victor8a87a992007-05-14 14:30:15 +010077 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
78
79 local_irq_restore(flags);
80
81 /* Set IRQ triggering type */
Thomas Gleixner6845664a2011-03-24 13:25:22 +010082 irq_set_irq_type(gpio_irq[pin], type);
Andrew Victor8a87a992007-05-14 14:30:15 +010083
84 /* enable interrupt mode */
85 ks8695_gpio_mode(pin, 0);
86
87 return 0;
88}
89EXPORT_SYMBOL(ks8695_gpio_interrupt);
90
91
92
93/* .... Generic GPIO interface .............................................. */
94
95/*
96 * Configure the GPIO line as an input.
97 */
Daniel Silverstone72880ad2008-12-13 20:44:12 +000098static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
Andrew Victor8a87a992007-05-14 14:30:15 +010099{
100 unsigned long x, flags;
101
102 if (pin > KS8695_GPIO_15)
103 return -EINVAL;
104
105 /* set pin to GPIO mode */
106 ks8695_gpio_mode(pin, 1);
107
108 local_irq_save(flags);
109
110 /* set pin as input */
111 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
Andrew Victor8d163b32008-10-07 20:20:15 +0100112 x &= ~IOPM(pin);
Andrew Victor8a87a992007-05-14 14:30:15 +0100113 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
114
115 local_irq_restore(flags);
116
117 return 0;
118}
Andrew Victor8a87a992007-05-14 14:30:15 +0100119
120
121/*
122 * Configure the GPIO line as an output, with default state.
123 */
Daniel Silverstone72880ad2008-12-13 20:44:12 +0000124static int ks8695_gpio_direction_output(struct gpio_chip *gc,
125 unsigned int pin, int state)
Andrew Victor8a87a992007-05-14 14:30:15 +0100126{
127 unsigned long x, flags;
128
129 if (pin > KS8695_GPIO_15)
130 return -EINVAL;
131
132 /* set pin to GPIO mode */
133 ks8695_gpio_mode(pin, 1);
134
135 local_irq_save(flags);
136
137 /* set line state */
138 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
139 if (state)
Andrew Victor8d163b32008-10-07 20:20:15 +0100140 x |= IOPD(pin);
Andrew Victor8a87a992007-05-14 14:30:15 +0100141 else
Andrew Victor8d163b32008-10-07 20:20:15 +0100142 x &= ~IOPD(pin);
Andrew Victor8a87a992007-05-14 14:30:15 +0100143 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
144
145 /* set pin as output */
146 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
Andrew Victor8d163b32008-10-07 20:20:15 +0100147 x |= IOPM(pin);
Andrew Victor8a87a992007-05-14 14:30:15 +0100148 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
149
150 local_irq_restore(flags);
151
152 return 0;
153}
Andrew Victor8a87a992007-05-14 14:30:15 +0100154
155
156/*
157 * Set the state of an output GPIO line.
158 */
Daniel Silverstone72880ad2008-12-13 20:44:12 +0000159static void ks8695_gpio_set_value(struct gpio_chip *gc,
160 unsigned int pin, int state)
Andrew Victor8a87a992007-05-14 14:30:15 +0100161{
162 unsigned long x, flags;
163
164 if (pin > KS8695_GPIO_15)
165 return;
166
167 local_irq_save(flags);
168
169 /* set output line state */
170 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
171 if (state)
Andrew Victor8d163b32008-10-07 20:20:15 +0100172 x |= IOPD(pin);
Andrew Victor8a87a992007-05-14 14:30:15 +0100173 else
Andrew Victor8d163b32008-10-07 20:20:15 +0100174 x &= ~IOPD(pin);
Andrew Victor8a87a992007-05-14 14:30:15 +0100175 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
176
177 local_irq_restore(flags);
178}
Andrew Victor8a87a992007-05-14 14:30:15 +0100179
180
181/*
182 * Read the state of a GPIO line.
183 */
Daniel Silverstone72880ad2008-12-13 20:44:12 +0000184static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
Andrew Victor8a87a992007-05-14 14:30:15 +0100185{
186 unsigned long x;
187
188 if (pin > KS8695_GPIO_15)
189 return -EINVAL;
190
191 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
Andrew Victor8d163b32008-10-07 20:20:15 +0100192 return (x & IOPD(pin)) != 0;
Andrew Victor8a87a992007-05-14 14:30:15 +0100193}
Andrew Victor8a87a992007-05-14 14:30:15 +0100194
195
196/*
197 * Map GPIO line to IRQ number.
198 */
Ben Dooks7ef71322008-12-13 20:44:13 +0000199static int ks8695_gpio_to_irq(struct gpio_chip *gc, unsigned int pin)
Andrew Victor8a87a992007-05-14 14:30:15 +0100200{
201 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
202 return -EINVAL;
203
204 return gpio_irq[pin];
205}
Andrew Victor8a87a992007-05-14 14:30:15 +0100206
207/*
208 * Map IRQ number to GPIO line.
209 */
210int irq_to_gpio(unsigned int irq)
211{
212 if ((irq < KS8695_IRQ_EXTERN0) || (irq > KS8695_IRQ_EXTERN3))
213 return -EINVAL;
214
215 return (irq - KS8695_IRQ_EXTERN0);
216}
217EXPORT_SYMBOL(irq_to_gpio);
Andrew Victor20118ff2007-10-15 14:27:41 +0100218
Daniel Silverstone72880ad2008-12-13 20:44:12 +0000219/* GPIOLIB interface */
220
221static struct gpio_chip ks8695_gpio_chip = {
222 .label = "KS8695",
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,
Ben Dooks7ef71322008-12-13 20:44:13 +0000227 .to_irq = ks8695_gpio_to_irq,
Daniel Silverstone72880ad2008-12-13 20:44:12 +0000228 .base = 0,
229 .ngpio = 16,
230 .can_sleep = 0,
231};
232
233/* Register the GPIOs */
234void ks8695_register_gpios(void)
235{
236 if (gpiochip_add(&ks8695_gpio_chip))
237 printk(KERN_ERR "Unable to register core GPIOs\n");
238}
Andrew Victor20118ff2007-10-15 14:27:41 +0100239
240/* .... Debug interface ..................................................... */
241
242#ifdef CONFIG_DEBUG_FS
243
244static int ks8695_gpio_show(struct seq_file *s, void *unused)
245{
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;
249 int i;
250
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);
254
255 seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
256
257 for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
258 seq_printf(s, "%i:\t", i);
259
Andrew Victor8d163b32008-10-07 20:20:15 +0100260 seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
Andrew Victor20118ff2007-10-15 14:27:41 +0100261
262 if (i <= KS8695_GPIO_3) {
263 if (ctrl & enable[i]) {
264 seq_printf(s, "EXT%i ", i);
265
266 switch ((ctrl & intmask[i]) >> (4 * i)) {
267 case IOPC_TM_LOW:
268 seq_printf(s, "(Low)"); break;
269 case IOPC_TM_HIGH:
270 seq_printf(s, "(High)"); break;
271 case IOPC_TM_RISING:
272 seq_printf(s, "(Rising)"); break;
273 case IOPC_TM_FALLING:
274 seq_printf(s, "(Falling)"); break;
275 case IOPC_TM_EDGE:
276 seq_printf(s, "(Edges)"); break;
277 }
278 }
279 else
280 seq_printf(s, "GPIO\t");
281 }
282 else if (i <= KS8695_GPIO_5) {
283 if (ctrl & enable[i])
284 seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
285 else
286 seq_printf(s, "GPIO\t");
287 }
288 else
289 seq_printf(s, "GPIO\t");
290
291 seq_printf(s, "\t");
292
Andrew Victor8d163b32008-10-07 20:20:15 +0100293 seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
Andrew Victor20118ff2007-10-15 14:27:41 +0100294 }
295 return 0;
296}
297
298static int ks8695_gpio_open(struct inode *inode, struct file *file)
299{
300 return single_open(file, ks8695_gpio_show, NULL);
301}
302
303static const struct file_operations ks8695_gpio_operations = {
304 .open = ks8695_gpio_open,
305 .read = seq_read,
306 .llseek = seq_lseek,
307 .release = single_release,
308};
309
310static int __init ks8695_gpio_debugfs_init(void)
311{
312 /* /sys/kernel/debug/ks8695_gpio */
313 (void) debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL, &ks8695_gpio_operations);
314 return 0;
315}
316postcore_initcall(ks8695_gpio_debugfs_init);
317
318#endif