79a7b1c5a57e5ad70b296f4e4f3617ae96e66803
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / arm / plat-s5p / irq-gpioint.c
1 /* 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 GPIO_BASE(chip)         (((unsigned long)(chip)->base) & 0xFFFFF000u)
26
27 #define CON_OFFSET              0x700
28 #define MASK_OFFSET             0x900
29 #define PEND_OFFSET             0xA00
30 #define REG_OFFSET(x)           ((x) << 2)
31
32 static struct s3c_gpio_chip *irq_chips[S5P_GPIOINT_GROUP_MAXNR];
33
34 static int s5p_gpioint_get_offset(struct irq_data *data)
35 {
36         struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
37         return data->irq - chip->irq_base;
38 }
39
40 static void s5p_gpioint_ack(struct irq_data *data)
41 {
42         struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
43         int group, offset, pend_offset;
44         unsigned int value;
45
46         group = chip->group;
47         offset = s5p_gpioint_get_offset(data);
48         pend_offset = REG_OFFSET(group);
49
50         value = __raw_readl(GPIO_BASE(chip) + PEND_OFFSET + pend_offset);
51         value |= BIT(offset);
52         __raw_writel(value, GPIO_BASE(chip) + PEND_OFFSET + pend_offset);
53 }
54
55 static void s5p_gpioint_mask(struct irq_data *data)
56 {
57         struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
58         int group, offset, mask_offset;
59         unsigned int value;
60
61         group = chip->group;
62         offset = s5p_gpioint_get_offset(data);
63         mask_offset = REG_OFFSET(group);
64
65         value = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
66         value |= BIT(offset);
67         __raw_writel(value, GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
68 }
69
70 static void s5p_gpioint_unmask(struct irq_data *data)
71 {
72         struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
73         int group, offset, mask_offset;
74         unsigned int value;
75
76         group = chip->group;
77         offset = s5p_gpioint_get_offset(data);
78         mask_offset = REG_OFFSET(group);
79
80         value = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
81         value &= ~BIT(offset);
82         __raw_writel(value, GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
83 }
84
85 static void s5p_gpioint_mask_ack(struct irq_data *data)
86 {
87         s5p_gpioint_mask(data);
88         s5p_gpioint_ack(data);
89 }
90
91 static int s5p_gpioint_set_type(struct irq_data *data, unsigned int type)
92 {
93         struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
94         int group, offset, con_offset;
95         unsigned int value;
96
97         group = chip->group;
98         offset = s5p_gpioint_get_offset(data);
99         con_offset = REG_OFFSET(group);
100
101         switch (type) {
102         case IRQ_TYPE_EDGE_RISING:
103                 type = S5P_IRQ_TYPE_EDGE_RISING;
104                 break;
105         case IRQ_TYPE_EDGE_FALLING:
106                 type = S5P_IRQ_TYPE_EDGE_FALLING;
107                 break;
108         case IRQ_TYPE_EDGE_BOTH:
109                 type = S5P_IRQ_TYPE_EDGE_BOTH;
110                 break;
111         case IRQ_TYPE_LEVEL_HIGH:
112                 type = S5P_IRQ_TYPE_LEVEL_HIGH;
113                 break;
114         case IRQ_TYPE_LEVEL_LOW:
115                 type = S5P_IRQ_TYPE_LEVEL_LOW;
116                 break;
117         case IRQ_TYPE_NONE:
118         default:
119                 printk(KERN_WARNING "No irq type\n");
120                 return -EINVAL;
121         }
122
123         value = __raw_readl(GPIO_BASE(chip) + CON_OFFSET + con_offset);
124         value &= ~(0x7 << (offset * 0x4));
125         value |= (type << (offset * 0x4));
126         __raw_writel(value, GPIO_BASE(chip) + CON_OFFSET + con_offset);
127
128         return 0;
129 }
130
131 static struct irq_chip s5p_gpioint = {
132         .name           = "s5p_gpioint",
133         .irq_ack        = s5p_gpioint_ack,
134         .irq_mask       = s5p_gpioint_mask,
135         .irq_mask_ack   = s5p_gpioint_mask_ack,
136         .irq_unmask     = s5p_gpioint_unmask,
137         .irq_set_type   = s5p_gpioint_set_type,
138 };
139
140 static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc)
141 {
142         int group, pend_offset, mask_offset;
143         unsigned int pend, mask;
144
145         for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++) {
146                 struct s3c_gpio_chip *chip = irq_chips[group];
147                 if (!chip)
148                         continue;
149
150                 pend_offset = REG_OFFSET(group);
151                 pend = __raw_readl(GPIO_BASE(chip) + PEND_OFFSET + pend_offset);
152                 if (!pend)
153                         continue;
154
155                 mask_offset = REG_OFFSET(group);
156                 mask = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
157                 pend &= ~mask;
158
159                 while (pend) {
160                         int offset = fls(pend) - 1;
161                         int real_irq = chip->irq_base + offset;
162                         generic_handle_irq(real_irq);
163                         pend &= ~BIT(offset);
164                 }
165         }
166 }
167
168 static __init int s5p_gpioint_add(struct s3c_gpio_chip *chip)
169 {
170         static int used_gpioint_groups = 0;
171         static bool handler_registered = 0;
172         int irq, group = chip->group;
173         int i;
174
175         if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT)
176                 return -ENOMEM;
177
178         chip->irq_base = S5P_GPIOINT_BASE +
179                          used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE;
180         used_gpioint_groups++;
181
182         if (!handler_registered) {
183                 set_irq_chained_handler(IRQ_GPIOINT, s5p_gpioint_handler);
184                 handler_registered = 1;
185         }
186
187         irq_chips[group] = chip;
188         for (i = 0; i < chip->chip.ngpio; i++) {
189                 irq = chip->irq_base + i;
190                 set_irq_chip(irq, &s5p_gpioint);
191                 set_irq_data(irq, chip);
192                 set_irq_handler(irq, handle_level_irq);
193                 set_irq_flags(irq, IRQF_VALID);
194         }
195         return 0;
196 }
197
198 int __init s5p_register_gpio_interrupt(int pin)
199 {
200         struct s3c_gpio_chip *my_chip = s3c_gpiolib_getchip(pin);
201         int offset, group;
202         int ret;
203
204         if (!my_chip)
205                 return -EINVAL;
206
207         offset = pin - my_chip->chip.base;
208         group = my_chip->group;
209
210         /* check if the group has been already registered */
211         if (my_chip->irq_base)
212                 return my_chip->irq_base + offset;
213
214         /* register gpio group */
215         ret = s5p_gpioint_add(my_chip);
216         if (ret == 0) {
217                 my_chip->chip.to_irq = samsung_gpiolib_to_irq;
218                 printk(KERN_INFO "Registered interrupt support for gpio group %d.\n",
219                        group);
220                 return my_chip->irq_base + offset;
221         }
222         return ret;
223 }