blob: 79a7b1c5a57e5ad70b296f4e4f3617ae96e66803 [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
Marek Szyprowski2de09262011-03-15 21:17:43 +090025#define GPIO_BASE(chip) (((unsigned long)(chip)->base) & 0xFFFFF000u)
Marek Szyprowski170a4612010-10-01 10:40:37 +090026
Marek Szyprowski2de09262011-03-15 21:17:43 +090027#define CON_OFFSET 0x700
28#define MASK_OFFSET 0x900
29#define PEND_OFFSET 0xA00
30#define REG_OFFSET(x) ((x) << 2)
Marek Szyprowski170a4612010-10-01 10:40:37 +090031
Marek Szyprowski170a4612010-10-01 10:40:37 +090032static struct s3c_gpio_chip *irq_chips[S5P_GPIOINT_GROUP_MAXNR];
33
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010034static int s5p_gpioint_get_offset(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090035{
Marek Szyprowski2de09262011-03-15 21:17:43 +090036 struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
37 return data->irq - chip->irq_base;
Marek Szyprowski170a4612010-10-01 10:40:37 +090038}
39
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010040static void s5p_gpioint_ack(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090041{
Marek Szyprowski2de09262011-03-15 21:17:43 +090042 struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090043 int group, offset, pend_offset;
44 unsigned int value;
45
Marek Szyprowski2de09262011-03-15 21:17:43 +090046 group = chip->group;
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010047 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski2de09262011-03-15 21:17:43 +090048 pend_offset = REG_OFFSET(group);
Marek Szyprowski170a4612010-10-01 10:40:37 +090049
Marek Szyprowski2de09262011-03-15 21:17:43 +090050 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);
Marek Szyprowski170a4612010-10-01 10:40:37 +090053}
54
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010055static void s5p_gpioint_mask(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090056{
Marek Szyprowski2de09262011-03-15 21:17:43 +090057 struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090058 int group, offset, mask_offset;
59 unsigned int value;
60
Marek Szyprowski2de09262011-03-15 21:17:43 +090061 group = chip->group;
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010062 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski2de09262011-03-15 21:17:43 +090063 mask_offset = REG_OFFSET(group);
Marek Szyprowski170a4612010-10-01 10:40:37 +090064
Marek Szyprowski2de09262011-03-15 21:17:43 +090065 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);
Marek Szyprowski170a4612010-10-01 10:40:37 +090068}
69
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010070static void s5p_gpioint_unmask(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090071{
Marek Szyprowski2de09262011-03-15 21:17:43 +090072 struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090073 int group, offset, mask_offset;
74 unsigned int value;
75
Marek Szyprowski2de09262011-03-15 21:17:43 +090076 group = chip->group;
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010077 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski2de09262011-03-15 21:17:43 +090078 mask_offset = REG_OFFSET(group);
Marek Szyprowski170a4612010-10-01 10:40:37 +090079
Marek Szyprowski2de09262011-03-15 21:17:43 +090080 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);
Marek Szyprowski170a4612010-10-01 10:40:37 +090083}
84
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010085static void s5p_gpioint_mask_ack(struct irq_data *data)
Marek Szyprowski170a4612010-10-01 10:40:37 +090086{
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010087 s5p_gpioint_mask(data);
88 s5p_gpioint_ack(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090089}
90
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010091static int s5p_gpioint_set_type(struct irq_data *data, unsigned int type)
Marek Szyprowski170a4612010-10-01 10:40:37 +090092{
Marek Szyprowski2de09262011-03-15 21:17:43 +090093 struct s3c_gpio_chip *chip = irq_data_get_irq_data(data);
Marek Szyprowski170a4612010-10-01 10:40:37 +090094 int group, offset, con_offset;
95 unsigned int value;
96
Marek Szyprowski2de09262011-03-15 21:17:43 +090097 group = chip->group;
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +010098 offset = s5p_gpioint_get_offset(data);
Marek Szyprowski2de09262011-03-15 21:17:43 +090099 con_offset = REG_OFFSET(group);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900100
101 switch (type) {
102 case IRQ_TYPE_EDGE_RISING:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900103 type = S5P_IRQ_TYPE_EDGE_RISING;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900104 break;
105 case IRQ_TYPE_EDGE_FALLING:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900106 type = S5P_IRQ_TYPE_EDGE_FALLING;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900107 break;
108 case IRQ_TYPE_EDGE_BOTH:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900109 type = S5P_IRQ_TYPE_EDGE_BOTH;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900110 break;
111 case IRQ_TYPE_LEVEL_HIGH:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900112 type = S5P_IRQ_TYPE_LEVEL_HIGH;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900113 break;
114 case IRQ_TYPE_LEVEL_LOW:
Marek Szyprowski9adf5d22010-10-02 11:48:09 +0900115 type = S5P_IRQ_TYPE_LEVEL_LOW;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900116 break;
117 case IRQ_TYPE_NONE:
118 default:
119 printk(KERN_WARNING "No irq type\n");
120 return -EINVAL;
121 }
122
Marek Szyprowski2de09262011-03-15 21:17:43 +0900123 value = __raw_readl(GPIO_BASE(chip) + CON_OFFSET + con_offset);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900124 value &= ~(0x7 << (offset * 0x4));
125 value |= (type << (offset * 0x4));
Marek Szyprowski2de09262011-03-15 21:17:43 +0900126 __raw_writel(value, GPIO_BASE(chip) + CON_OFFSET + con_offset);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900127
128 return 0;
129}
130
Marek Szyprowski2de09262011-03-15 21:17:43 +0900131static struct irq_chip s5p_gpioint = {
Marek Szyprowski170a4612010-10-01 10:40:37 +0900132 .name = "s5p_gpioint",
Lennert Buytenhekbb0b2372010-12-14 22:55:26 +0100133 .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,
Marek Szyprowski170a4612010-10-01 10:40:37 +0900138};
139
140static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc)
141{
Marek Szyprowski2de09262011-03-15 21:17:43 +0900142 int group, pend_offset, mask_offset;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900143 unsigned int pend, mask;
144
145 for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++) {
Marek Szyprowski2de09262011-03-15 21:17:43 +0900146 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);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900152 if (!pend)
153 continue;
154
Marek Szyprowski2de09262011-03-15 21:17:43 +0900155 mask_offset = REG_OFFSET(group);
156 mask = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900157 pend &= ~mask;
158
Marek Szyprowski2de09262011-03-15 21:17:43 +0900159 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);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900164 }
165 }
166}
167
168static __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);
Marek Szyprowski2de09262011-03-15 21:17:43 +0900191 set_irq_data(irq, chip);
Marek Szyprowski170a4612010-10-01 10:40:37 +0900192 set_irq_handler(irq, handle_level_irq);
193 set_irq_flags(irq, IRQF_VALID);
194 }
195 return 0;
196}
197
198int __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) {
Joonyoung Shim8ce14a22010-10-01 11:24:39 +0900217 my_chip->chip.to_irq = samsung_gpiolib_to_irq;
Marek Szyprowski170a4612010-10-01 10:40:37 +0900218 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}