Merge branch 'ieee802154-for-davem-2019-08-24' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / gpio / gpio-ks8695.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
8a87a996
AV
2/*
3 * arch/arm/mach-ks8695/gpio.c
4 *
5 * Copyright (C) 2006 Andrew Victor
72880ad8
DS
6 * Updated to GPIOLIB, Copyright 2008 Simtec Electronics
7 * Daniel Silverstone <dsilvers@simtec.co.uk>
8a87a996 8 */
6e9554a4 9#include <linux/gpio/driver.h>
8a87a996
AV
10#include <linux/kernel.h>
11#include <linux/mm.h>
12#include <linux/init.h>
20118ff9
AV
13#include <linux/debugfs.h>
14#include <linux/seq_file.h>
8a87a996 15#include <linux/module.h>
fced80c7 16#include <linux/io.h>
8a87a996 17
a09e64fb 18#include <mach/hardware.h>
8a87a996
AV
19#include <asm/mach/irq.h>
20
a09e64fb 21#include <mach/regs-gpio.h>
e24e4498 22#include <mach/gpio-ks8695.h>
8a87a996
AV
23
24/*
25 * Configure a GPIO line for either GPIO function, or its internal
26 * function (Interrupt, Timer, etc).
27 */
72880ad8 28static void ks8695_gpio_mode(unsigned int pin, short gpio)
8a87a996
AV
29{
30 unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
31 unsigned long x, flags;
32
33 if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
34 return;
35
36 local_irq_save(flags);
37
38 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
39 if (gpio) /* GPIO: set bit to 0 */
40 x &= ~enable[pin];
41 else /* Internal function: set bit to 1 */
42 x |= enable[pin];
43 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
44
45 local_irq_restore(flags);
46}
47
48
49static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
50
51/*
52 * Configure GPIO pin as external interrupt source.
53 */
72880ad8 54int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
8a87a996
AV
55{
56 unsigned long x, flags;
57
58 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
59 return -EINVAL;
60
61 local_irq_save(flags);
62
63 /* set pin as input */
64 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
8d163b3f 65 x &= ~IOPM(pin);
8a87a996
AV
66 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
67
68 local_irq_restore(flags);
69
70 /* Set IRQ triggering type */
6845664a 71 irq_set_irq_type(gpio_irq[pin], type);
8a87a996
AV
72
73 /* enable interrupt mode */
74 ks8695_gpio_mode(pin, 0);
75
76 return 0;
77}
78EXPORT_SYMBOL(ks8695_gpio_interrupt);
79
80
81
82/* .... Generic GPIO interface .............................................. */
83
84/*
85 * Configure the GPIO line as an input.
86 */
72880ad8 87static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
8a87a996
AV
88{
89 unsigned long x, flags;
90
91 if (pin > KS8695_GPIO_15)
92 return -EINVAL;
93
94 /* set pin to GPIO mode */
95 ks8695_gpio_mode(pin, 1);
96
97 local_irq_save(flags);
98
99 /* set pin as input */
100 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
8d163b3f 101 x &= ~IOPM(pin);
8a87a996
AV
102 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
103
104 local_irq_restore(flags);
105
106 return 0;
107}
8a87a996
AV
108
109
110/*
111 * Configure the GPIO line as an output, with default state.
112 */
72880ad8
DS
113static int ks8695_gpio_direction_output(struct gpio_chip *gc,
114 unsigned int pin, int state)
8a87a996
AV
115{
116 unsigned long x, flags;
117
118 if (pin > KS8695_GPIO_15)
119 return -EINVAL;
120
121 /* set pin to GPIO mode */
122 ks8695_gpio_mode(pin, 1);
123
124 local_irq_save(flags);
125
126 /* set line state */
127 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
128 if (state)
8d163b3f 129 x |= IOPD(pin);
8a87a996 130 else
8d163b3f 131 x &= ~IOPD(pin);
8a87a996
AV
132 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
133
134 /* set pin as output */
135 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
8d163b3f 136 x |= IOPM(pin);
8a87a996
AV
137 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
138
139 local_irq_restore(flags);
140
141 return 0;
142}
8a87a996
AV
143
144
145/*
146 * Set the state of an output GPIO line.
147 */
72880ad8
DS
148static void ks8695_gpio_set_value(struct gpio_chip *gc,
149 unsigned int pin, int state)
8a87a996
AV
150{
151 unsigned long x, flags;
152
153 if (pin > KS8695_GPIO_15)
154 return;
155
156 local_irq_save(flags);
157
158 /* set output line state */
159 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
160 if (state)
8d163b3f 161 x |= IOPD(pin);
8a87a996 162 else
8d163b3f 163 x &= ~IOPD(pin);
8a87a996
AV
164 __raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
165
166 local_irq_restore(flags);
167}
8a87a996
AV
168
169
170/*
171 * Read the state of a GPIO line.
172 */
72880ad8 173static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
8a87a996
AV
174{
175 unsigned long x;
176
177 if (pin > KS8695_GPIO_15)
178 return -EINVAL;
179
180 x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
8d163b3f 181 return (x & IOPD(pin)) != 0;
8a87a996 182}
8a87a996
AV
183
184
185/*
186 * Map GPIO line to IRQ number.
187 */
7ef71320 188static int ks8695_gpio_to_irq(struct gpio_chip *gc, unsigned int pin)
8a87a996
AV
189{
190 if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
191 return -EINVAL;
192
193 return gpio_irq[pin];
194}
8a87a996 195
72880ad8
DS
196/* GPIOLIB interface */
197
198static struct gpio_chip ks8695_gpio_chip = {
199 .label = "KS8695",
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,
7ef71320 204 .to_irq = ks8695_gpio_to_irq,
72880ad8
DS
205 .base = 0,
206 .ngpio = 16,
9fb1f39e 207 .can_sleep = false,
72880ad8
DS
208};
209
210/* Register the GPIOs */
211void ks8695_register_gpios(void)
212{
4eab22e7 213 if (gpiochip_add_data(&ks8695_gpio_chip, NULL))
72880ad8
DS
214 printk(KERN_ERR "Unable to register core GPIOs\n");
215}
20118ff9
AV
216
217/* .... Debug interface ..................................................... */
218
219#ifdef CONFIG_DEBUG_FS
220
221static int ks8695_gpio_show(struct seq_file *s, void *unused)
222{
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;
226 int i;
227
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);
231
232 seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
233
234 for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
235 seq_printf(s, "%i:\t", i);
236
8d163b3f 237 seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
20118ff9
AV
238
239 if (i <= KS8695_GPIO_3) {
240 if (ctrl & enable[i]) {
241 seq_printf(s, "EXT%i ", i);
242
243 switch ((ctrl & intmask[i]) >> (4 * i)) {
0397375d
VB
244 case IOPC_TM_LOW:
245 seq_printf(s, "(Low)"); break;
246 case IOPC_TM_HIGH:
247 seq_printf(s, "(High)"); break;
248 case IOPC_TM_RISING:
249 seq_printf(s, "(Rising)"); break;
250 case IOPC_TM_FALLING:
251 seq_printf(s, "(Falling)"); break;
252 case IOPC_TM_EDGE:
253 seq_printf(s, "(Edges)"); break;
20118ff9 254 }
36905a33 255 } else
20118ff9 256 seq_printf(s, "GPIO\t");
36905a33 257 } else if (i <= KS8695_GPIO_5) {
20118ff9
AV
258 if (ctrl & enable[i])
259 seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
260 else
261 seq_printf(s, "GPIO\t");
36905a33 262 } else {
20118ff9 263 seq_printf(s, "GPIO\t");
36905a33 264 }
20118ff9
AV
265
266 seq_printf(s, "\t");
267
8d163b3f 268 seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
20118ff9
AV
269 }
270 return 0;
271}
272
9904f032 273DEFINE_SHOW_ATTRIBUTE(ks8695_gpio);
20118ff9
AV
274
275static int __init ks8695_gpio_debugfs_init(void)
276{
277 /* /sys/kernel/debug/ks8695_gpio */
9904f032
YL
278 debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL,
279 &ks8695_gpio_fops);
20118ff9
AV
280 return 0;
281}
282postcore_initcall(ks8695_gpio_debugfs_init);
283
284#endif