Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 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> |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 21 | |
| 22 | #include <mach/map.h> |
| 23 | #include <plat/gpio-core.h> |
| 24 | #include <plat/gpio-cfg.h> |
| 25 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 26 | #define GPIO_BASE(chip) (((unsigned long)(chip)->base) & 0xFFFFF000u) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 27 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 28 | #define CON_OFFSET 0x700 |
| 29 | #define MASK_OFFSET 0x900 |
| 30 | #define PEND_OFFSET 0xA00 |
| 31 | #define REG_OFFSET(x) ((x) << 2) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 32 | |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 33 | struct s5p_gpioint_bank { |
| 34 | struct list_head list; |
| 35 | int start; |
| 36 | int nr_groups; |
| 37 | int irq; |
| 38 | struct s3c_gpio_chip **chips; |
| 39 | void (*handler)(unsigned int, struct irq_desc *); |
| 40 | }; |
| 41 | |
| 42 | LIST_HEAD(banks); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 43 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 44 | static int s5p_gpioint_get_offset(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 45 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 46 | struct s3c_gpio_chip *chip = irq_data_get_irq_handler_data(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 47 | return data->irq - chip->irq_base; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 48 | } |
| 49 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 50 | static void s5p_gpioint_ack(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 51 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 52 | struct s3c_gpio_chip *chip = irq_data_get_irq_handler_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 53 | int group, offset, pend_offset; |
| 54 | unsigned int value; |
| 55 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 56 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 57 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 58 | pend_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 59 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 60 | value = __raw_readl(GPIO_BASE(chip) + PEND_OFFSET + pend_offset); |
| 61 | value |= BIT(offset); |
| 62 | __raw_writel(value, GPIO_BASE(chip) + PEND_OFFSET + pend_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 65 | static void s5p_gpioint_mask(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 66 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 67 | struct s3c_gpio_chip *chip = irq_data_get_irq_handler_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 68 | int group, offset, mask_offset; |
| 69 | unsigned int value; |
| 70 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 71 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 72 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 73 | mask_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 74 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 75 | value = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset); |
| 76 | value |= BIT(offset); |
| 77 | __raw_writel(value, GPIO_BASE(chip) + MASK_OFFSET + mask_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 78 | } |
| 79 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 80 | static void s5p_gpioint_unmask(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 81 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 82 | struct s3c_gpio_chip *chip = irq_data_get_irq_handler_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 83 | int group, offset, mask_offset; |
| 84 | unsigned int value; |
| 85 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 86 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 87 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 88 | mask_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 89 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 90 | value = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset); |
| 91 | value &= ~BIT(offset); |
| 92 | __raw_writel(value, GPIO_BASE(chip) + MASK_OFFSET + mask_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 95 | static void s5p_gpioint_mask_ack(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 96 | { |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 97 | s5p_gpioint_mask(data); |
| 98 | s5p_gpioint_ack(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 99 | } |
| 100 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 101 | static int s5p_gpioint_set_type(struct irq_data *data, unsigned int type) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 102 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 103 | struct s3c_gpio_chip *chip = irq_data_get_irq_handler_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 104 | int group, offset, con_offset; |
| 105 | unsigned int value; |
| 106 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 107 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 108 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 109 | con_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 110 | |
| 111 | switch (type) { |
| 112 | case IRQ_TYPE_EDGE_RISING: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 113 | type = S5P_IRQ_TYPE_EDGE_RISING; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 114 | break; |
| 115 | case IRQ_TYPE_EDGE_FALLING: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 116 | type = S5P_IRQ_TYPE_EDGE_FALLING; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 117 | break; |
| 118 | case IRQ_TYPE_EDGE_BOTH: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 119 | type = S5P_IRQ_TYPE_EDGE_BOTH; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 120 | break; |
| 121 | case IRQ_TYPE_LEVEL_HIGH: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 122 | type = S5P_IRQ_TYPE_LEVEL_HIGH; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 123 | break; |
| 124 | case IRQ_TYPE_LEVEL_LOW: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 125 | type = S5P_IRQ_TYPE_LEVEL_LOW; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 126 | break; |
| 127 | case IRQ_TYPE_NONE: |
| 128 | default: |
| 129 | printk(KERN_WARNING "No irq type\n"); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 133 | value = __raw_readl(GPIO_BASE(chip) + CON_OFFSET + con_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 134 | value &= ~(0x7 << (offset * 0x4)); |
| 135 | value |= (type << (offset * 0x4)); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 136 | __raw_writel(value, GPIO_BASE(chip) + CON_OFFSET + con_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 141 | static struct irq_chip s5p_gpioint = { |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 142 | .name = "s5p_gpioint", |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 143 | .irq_ack = s5p_gpioint_ack, |
| 144 | .irq_mask = s5p_gpioint_mask, |
| 145 | .irq_mask_ack = s5p_gpioint_mask_ack, |
| 146 | .irq_unmask = s5p_gpioint_unmask, |
| 147 | .irq_set_type = s5p_gpioint_set_type, |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc) |
| 151 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 152 | struct s5p_gpioint_bank *bank = irq_get_handler_data(irq); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 153 | int group, pend_offset, mask_offset; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 154 | unsigned int pend, mask; |
| 155 | |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 156 | for (group = 0; group < bank->nr_groups; group++) { |
| 157 | struct s3c_gpio_chip *chip = bank->chips[group]; |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 158 | if (!chip) |
| 159 | continue; |
| 160 | |
| 161 | pend_offset = REG_OFFSET(group); |
| 162 | pend = __raw_readl(GPIO_BASE(chip) + PEND_OFFSET + pend_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 163 | if (!pend) |
| 164 | continue; |
| 165 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 166 | mask_offset = REG_OFFSET(group); |
| 167 | mask = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 168 | pend &= ~mask; |
| 169 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 170 | while (pend) { |
| 171 | int offset = fls(pend) - 1; |
| 172 | int real_irq = chip->irq_base + offset; |
| 173 | generic_handle_irq(real_irq); |
| 174 | pend &= ~BIT(offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static __init int s5p_gpioint_add(struct s3c_gpio_chip *chip) |
| 180 | { |
| 181 | static int used_gpioint_groups = 0; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 182 | int irq, group = chip->group; |
| 183 | int i; |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 184 | struct s5p_gpioint_bank *bank = NULL; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 185 | |
| 186 | if (used_gpioint_groups >= S5P_GPIOINT_GROUP_COUNT) |
| 187 | return -ENOMEM; |
| 188 | |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 189 | list_for_each_entry(bank, &banks, list) { |
| 190 | if (group >= bank->start && |
| 191 | group < bank->start + bank->nr_groups) |
| 192 | break; |
| 193 | } |
| 194 | if (!bank) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | if (!bank->handler) { |
| 198 | bank->chips = kzalloc(sizeof(struct s3c_gpio_chip *) * |
| 199 | bank->nr_groups, GFP_KERNEL); |
| 200 | if (!bank->chips) |
| 201 | return -ENOMEM; |
| 202 | |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 203 | irq_set_chained_handler(bank->irq, s5p_gpioint_handler); |
| 204 | irq_set_handler_data(bank->irq, bank); |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 205 | bank->handler = s5p_gpioint_handler; |
| 206 | printk(KERN_INFO "Registered chained gpio int handler for interrupt %d.\n", |
| 207 | bank->irq); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * chained GPIO irq has been sucessfully registered, allocate new gpio |
| 212 | * int group and assign irq nubmers |
| 213 | */ |
| 214 | |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 215 | chip->irq_base = S5P_GPIOINT_BASE + |
| 216 | used_gpioint_groups * S5P_GPIOINT_GROUP_SIZE; |
| 217 | used_gpioint_groups++; |
| 218 | |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 219 | bank->chips[group - bank->start] = chip; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 220 | for (i = 0; i < chip->chip.ngpio; i++) { |
| 221 | irq = chip->irq_base + i; |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame^] | 222 | irq_set_chip(irq, &s5p_gpioint); |
| 223 | irq_set_handler_data(irq, chip); |
| 224 | irq_set_handler(irq, handle_level_irq); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 225 | set_irq_flags(irq, IRQF_VALID); |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int __init s5p_register_gpio_interrupt(int pin) |
| 231 | { |
| 232 | struct s3c_gpio_chip *my_chip = s3c_gpiolib_getchip(pin); |
| 233 | int offset, group; |
| 234 | int ret; |
| 235 | |
| 236 | if (!my_chip) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | offset = pin - my_chip->chip.base; |
| 240 | group = my_chip->group; |
| 241 | |
| 242 | /* check if the group has been already registered */ |
| 243 | if (my_chip->irq_base) |
| 244 | return my_chip->irq_base + offset; |
| 245 | |
| 246 | /* register gpio group */ |
| 247 | ret = s5p_gpioint_add(my_chip); |
| 248 | if (ret == 0) { |
Joonyoung Shim | 8ce14a2 | 2010-10-01 11:24:39 +0900 | [diff] [blame] | 249 | my_chip->chip.to_irq = samsung_gpiolib_to_irq; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 250 | printk(KERN_INFO "Registered interrupt support for gpio group %d.\n", |
| 251 | group); |
| 252 | return my_chip->irq_base + offset; |
| 253 | } |
| 254 | return ret; |
| 255 | } |
Marek Szyprowski | a43efdd | 2011-03-15 21:17:43 +0900 | [diff] [blame] | 256 | |
| 257 | int __init s5p_register_gpioint_bank(int chain_irq, int start, int nr_groups) |
| 258 | { |
| 259 | struct s5p_gpioint_bank *bank; |
| 260 | |
| 261 | bank = kzalloc(sizeof(*bank), GFP_KERNEL); |
| 262 | if (!bank) |
| 263 | return -ENOMEM; |
| 264 | |
| 265 | bank->start = start; |
| 266 | bank->nr_groups = nr_groups; |
| 267 | bank->irq = chain_irq; |
| 268 | |
| 269 | list_add_tail(&bank->list, &banks); |
| 270 | return 0; |
| 271 | } |