blob: 3b6bf89d17395ccfeed813236aaa6117137c903c [file] [log] [blame]
Marek Szyprowski170a4612010-10-01 10:40:37 +09001/* linux/arch/arm/plat-s5p/irq-gpioint.c
2 *
3 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * Author: Kyungmin Park <kyungmin.park@samsung.com>
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
6 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/kernel.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/io.h>
19#include <linux/gpio.h>
20
21#include <mach/map.h>
22#include <plat/gpio-core.h>
23#include <plat/gpio-cfg.h>
24
25#define S5P_GPIOREG(x) (S5P_VA_GPIO + (x))
26
27#define GPIOINT_CON_OFFSET 0x700
28#define GPIOINT_MASK_OFFSET 0x900
29#define GPIOINT_PEND_OFFSET 0xA00
30
Marek Szyprowski170a4612010-10-01 10:40:37 +090031static struct s3c_gpio_chip *irq_chips[S5P_GPIOINT_GROUP_MAXNR];
32
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010033static int s5p_gpioint_get_group(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090034{
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010035 struct gpio_chip *chip = irq_data_get_irq_data(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090036 struct s3c_gpio_chip *s3c_chip = container_of(chip,
37 struct s3c_gpio_chip, chip);
38 int group;
39
40 for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++)
41 if (s3c_chip == irq_chips[group])
42 break;
43
44 return group;
45}
46
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010047static int s5p_gpioint_get_offset(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090048{
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010049 struct gpio_chip *chip = irq_data_get_irq_data(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090050 struct s3c_gpio_chip *s3c_chip = container_of(chip,
51 struct s3c_gpio_chip, chip);
52
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010053 return data->irq - s3c_chip->irq_base;
Marek Szyprowski170a4612010-10-01 10:40:37 +090054}
55
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010056static void s5p_gpioint_ack(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090057{
58 int group, offset, pend_offset;
59 unsigned int value;
60
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010061 group = s5p_gpioint_get_group(data);
62 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090063 pend_offset = group << 2;
64
65 value = __raw_readl(S5P_GPIOREG(GPIOINT_PEND_OFFSET) + pend_offset);
66 value |= 1 << offset;
67 __raw_writel(value, S5P_GPIOREG(GPIOINT_PEND_OFFSET) + pend_offset);
68}
69
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010070static void s5p_gpioint_mask(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090071{
72 int group, offset, mask_offset;
73 unsigned int value;
74
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010075 group = s5p_gpioint_get_group(data);
76 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090077 mask_offset = group << 2;
78
79 value = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
80 value |= 1 << offset;
81 __raw_writel(value, S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
82}
83
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010084static void s5p_gpioint_unmask(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090085{
86 int group, offset, mask_offset;
87 unsigned int value;
88
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010089 group = s5p_gpioint_get_group(data);
90 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090091 mask_offset = group << 2;
92
93 value = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
94 value &= ~(1 << offset);
95 __raw_writel(value, S5P_GPIOREG(GPIOINT_MASK_OFFSET) + mask_offset);
96}
97
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010098static void s5p_gpioint_mask_ack(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090099{
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +0100100 s5p_gpioint_mask(data);
101 s5p_gpioint_ack(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900102}
103
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +0100104static int s5p_gpioint_set_type(struct irq_data *data, unsigned int type)
Marek Szyprowski170a4612010-10-01 10:40:37 +0900105{
106 int group, offset, con_offset;
107 unsigned int value;
108
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +0100109 group = s5p_gpioint_get_group(data);
110 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900111 con_offset = group << 2;
112
113 switch (type) {
114 case IRQ_TYPE_EDGE_RISING:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900115 type = S5P_IRQ_TYPE_EDGE_RISING;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900116 break;
117 case IRQ_TYPE_EDGE_FALLING:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900118 type = S5P_IRQ_TYPE_EDGE_FALLING;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900119 break;
120 case IRQ_TYPE_EDGE_BOTH:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900121 type = S5P_IRQ_TYPE_EDGE_BOTH;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900122 break;
123 case IRQ_TYPE_LEVEL_HIGH:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900124 type = S5P_IRQ_TYPE_LEVEL_HIGH;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900125 break;
126 case IRQ_TYPE_LEVEL_LOW:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900127 type = S5P_IRQ_TYPE_LEVEL_LOW;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900128 break;
129 case IRQ_TYPE_NONE:
130 default:
131 printk(KERN_WARNING "No irq type\n");
132 return -EINVAL;
133 }
134
135 value = __raw_readl(S5P_GPIOREG(GPIOINT_CON_OFFSET) + con_offset);
136 value &= ~(0x7 << (offset * 0x4));
137 value |= (type << (offset * 0x4));
138 __raw_writel(value, S5P_GPIOREG(GPIOINT_CON_OFFSET) + con_offset);
139
140 return 0;
141}
142
143struct irq_chip s5p_gpioint = {
144 .name = "s5p_gpioint",
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +0100145 .irq_ack = s5p_gpioint_ack,
146 .irq_mask = s5p_gpioint_mask,
147 .irq_mask_ack = s5p_gpioint_mask_ack,
148 .irq_unmask = s5p_gpioint_unmask,
149 .irq_set_type = s5p_gpioint_set_type,
Marek Szyprowski170a4612010-10-01 10:40:37 +0900150};
151
152static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc)
153{
154 int group, offset, pend_offset, mask_offset;
155 int real_irq;
156 unsigned int pend, mask;
157
158 for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++) {
159 pend_offset = group << 2;
160 pend = __raw_readl(S5P_GPIOREG(GPIOINT_PEND_OFFSET) +
161 pend_offset);
162 if (!pend)
163 continue;
164
165 mask_offset = group << 2;
166 mask = __raw_readl(S5P_GPIOREG(GPIOINT_MASK_OFFSET) +
167 mask_offset);
168 pend &= ~mask;
169
170 for (offset = 0; offset < 8; offset++) {
171 if (pend & (1 << offset)) {
172 struct s3c_gpio_chip *chip = irq_chips[group];
173 if (chip) {
174 real_irq = chip->irq_base + offset;
175 generic_handle_irq(real_irq);
176 }
177 }
178 }
179 }
180}
181
182static __init int s5p_gpioint_add(struct s3c_gpio_chip *chip)
183{
184 static int used_gpioint_groups = 0;
185 static bool handler_registered = 0;
186 int irq, group = chip->group;
187 int i;
188
189 if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT)
190 return -ENOMEM;
191
192 chip->irq_base = S5P_GPIOINT_BASE +
193 used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE;
194 used_gpioint_groups++;
195
196 if (!handler_registered) {
197 set_irq_chained_handler(IRQ_GPIOINT, s5p_gpioint_handler);
198 handler_registered = 1;
199 }
200
201 irq_chips[group] = chip;
202 for (i = 0; i < chip->chip.ngpio; i++) {
203 irq = chip->irq_base + i;
204 set_irq_chip(irq, &s5p_gpioint);
205 set_irq_data(irq, &chip->chip);
206 set_irq_handler(irq, handle_level_irq);
207 set_irq_flags(irq, IRQF_VALID);
208 }
209 return 0;
210}
211
212int __init s5p_register_gpio_interrupt(int pin)
213{
214 struct s3c_gpio_chip *my_chip = s3c_gpiolib_getchip(pin);
215 int offset, group;
216 int ret;
217
218 if (!my_chip)
219 return -EINVAL;
220
221 offset = pin - my_chip->chip.base;
222 group = my_chip->group;
223
224 /* check if the group has been already registered */
225 if (my_chip->irq_base)
226 return my_chip->irq_base + offset;
227
228 /* register gpio group */
229 ret = s5p_gpioint_add(my_chip);
230 if (ret == 0) {
Joonyoung Shim8ce14a22010-10-01 11:24:39 +0900231 my_chip->chip.to_irq = samsung_gpiolib_to_irq;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900232 printk(KERN_INFO "Registered interrupt support for gpio group %d.\n",
233 group);
234 return my_chip->irq_base + offset;
235 }
236 return ret;
237}