1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/mach-ks8695/gpio.c
5 * Copyright (C) 2006 Andrew Victor
6 * Updated to GPIOLIB, Copyright 2008 Simtec Electronics
9 #include <linux/gpio/driver.h>
10 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/debugfs.h>
14 #include <linux/seq_file.h>
15 #include <linux/module.h>
18 #include <mach/hardware.h>
19 #include <asm/mach/irq.h>
21 #include <mach/regs-gpio.h>
22 #include <mach/gpio-ks8695.h>
25 * Configure a GPIO line for either GPIO function, or its internal
26 * function (Interrupt, Timer, etc).
28 static void ks8695_gpio_mode(unsigned int pin, short gpio)
30 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
31 unsigned long x, flags;
33 if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
36 local_irq_save(flags);
38 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
39 if (gpio) /* GPIO: set bit to 0 */
41 else /* Internal function: set bit to 1 */
43 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
45 local_irq_restore(flags);
49 static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
52 * Configure GPIO pin as external interrupt source.
54 int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
56 unsigned long x, flags;
58 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
61 local_irq_save(flags);
63 /* set pin as input */
64 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
66 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
68 local_irq_restore(flags);
70 /* Set IRQ triggering type */
71 irq_set_irq_type(gpio_irq[pin], type);
73 /* enable interrupt mode */
74 ks8695_gpio_mode(pin, 0);
78 EXPORT_SYMBOL(ks8695_gpio_interrupt);
82 /* .... Generic GPIO interface .............................................. */
85 * Configure the GPIO line as an input.
87 static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
89 unsigned long x, flags;
91 if (pin > KS8695_GPIO_15)
94 /* set pin to GPIO mode */
95 ks8695_gpio_mode(pin, 1);
97 local_irq_save(flags);
99 /* set pin as input */
100 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
102 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
104 local_irq_restore(flags);
111 * Configure the GPIO line as an output, with default state.
113 static int ks8695_gpio_direction_output(struct gpio_chip *gc,
114 unsigned int pin, int state)
116 unsigned long x, flags;
118 if (pin > KS8695_GPIO_15)
121 /* set pin to GPIO mode */
122 ks8695_gpio_mode(pin, 1);
124 local_irq_save(flags);
127 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
132 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
134 /* set pin as output */
135 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
137 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
139 local_irq_restore(flags);
146 * Set the state of an output GPIO line.
148 static void ks8695_gpio_set_value(struct gpio_chip *gc,
149 unsigned int pin, int state)
151 unsigned long x, flags;
153 if (pin > KS8695_GPIO_15)
156 local_irq_save(flags);
158 /* set output line state */
159 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
164 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
166 local_irq_restore(flags);
171 * Read the state of a GPIO line.
173 static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
177 if (pin > KS8695_GPIO_15)
180 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
181 return (x & IOPD(pin)) != 0;
186 * Map GPIO line to IRQ number.
188 static int ks8695_gpio_to_irq(struct gpio_chip *gc, unsigned int pin)
190 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
193 return gpio_irq[pin];
196 /* GPIOLIB interface */
198 static struct gpio_chip ks8695_gpio_chip = {
200 .direction_input = ks8695_gpio_direction_input,
201 .direction_output = ks8695_gpio_direction_output,
202 .get = ks8695_gpio_get_value,
203 .set = ks8695_gpio_set_value,
204 .to_irq = ks8695_gpio_to_irq,
210 /* Register the GPIOs */
211 void ks8695_register_gpios(void)
213 if (gpiochip_add_data(&ks8695_gpio_chip, NULL))
214 printk(KERN_ERR "Unable to register core GPIOs\n");
217 /* .... Debug interface ..................................................... */
219 #ifdef CONFIG_DEBUG_FS
221 static int ks8695_gpio_show(struct seq_file *s, void *unused)
223 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
224 unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
225 unsigned long mode, ctrl, data;
228 mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
229 ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
230 data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
232 seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
234 for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
235 seq_printf(s, "%i:\t", i);
237 seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
239 if (i <= KS8695_GPIO_3) {
240 if (ctrl & enable[i]) {
241 seq_printf(s, "EXT%i ", i);
243 switch ((ctrl & intmask[i]) >> (4 * i)) {
245 seq_printf(s, "(Low)"); break;
247 seq_printf(s, "(High)"); break;
249 seq_printf(s, "(Rising)"); break;
250 case IOPC_TM_FALLING:
251 seq_printf(s, "(Falling)"); break;
253 seq_printf(s, "(Edges)"); break;
256 seq_printf(s, "GPIO\t");
257 } else if (i <= KS8695_GPIO_5) {
258 if (ctrl & enable[i])
259 seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
261 seq_printf(s, "GPIO\t");
263 seq_printf(s, "GPIO\t");
268 seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
273 DEFINE_SHOW_ATTRIBUTE(ks8695_gpio);
275 static int __init ks8695_gpio_debugfs_init(void)
277 /* /sys/kernel/debug/ks8695_gpio */
278 debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL,
282 postcore_initcall(ks8695_gpio_debugfs_init);