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> |
| 20 | |
| 21 | #include <mach/map.h> |
| 22 | #include <plat/gpio-core.h> |
| 23 | #include <plat/gpio-cfg.h> |
| 24 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 25 | #define GPIO_BASE(chip) (((unsigned long)(chip)->base) & 0xFFFFF000u) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 26 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 27 | #define CON_OFFSET 0x700 |
| 28 | #define MASK_OFFSET 0x900 |
| 29 | #define PEND_OFFSET 0xA00 |
| 30 | #define REG_OFFSET(x) ((x) << 2) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 31 | |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 32 | static struct s3c_gpio_chip *irq_chips[S5P_GPIOINT_GROUP_MAXNR]; |
| 33 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 34 | static int s5p_gpioint_get_offset(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 35 | { |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 36 | struct s3c_gpio_chip *chip = irq_data_get_irq_data(data); |
| 37 | return data->irq - chip->irq_base; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 38 | } |
| 39 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 40 | static void s5p_gpioint_ack(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 41 | { |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 42 | struct s3c_gpio_chip *chip = irq_data_get_irq_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 43 | int group, offset, pend_offset; |
| 44 | unsigned int value; |
| 45 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 46 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 47 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 48 | pend_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 49 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 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); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 55 | static void s5p_gpioint_mask(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 56 | { |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 57 | struct s3c_gpio_chip *chip = irq_data_get_irq_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 58 | int group, offset, mask_offset; |
| 59 | unsigned int value; |
| 60 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 61 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 62 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 63 | mask_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 64 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 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); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 68 | } |
| 69 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 70 | static void s5p_gpioint_unmask(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 71 | { |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 72 | struct s3c_gpio_chip *chip = irq_data_get_irq_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 73 | int group, offset, mask_offset; |
| 74 | unsigned int value; |
| 75 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 76 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 77 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 78 | mask_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 79 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 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); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 85 | static void s5p_gpioint_mask_ack(struct irq_data *data) |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 86 | { |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 87 | s5p_gpioint_mask(data); |
| 88 | s5p_gpioint_ack(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 91 | 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] | 92 | { |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 93 | struct s3c_gpio_chip *chip = irq_data_get_irq_data(data); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 94 | int group, offset, con_offset; |
| 95 | unsigned int value; |
| 96 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 97 | group = chip->group; |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 98 | offset = s5p_gpioint_get_offset(data); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 99 | con_offset = REG_OFFSET(group); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 100 | |
| 101 | switch (type) { |
| 102 | case IRQ_TYPE_EDGE_RISING: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 103 | type = S5P_IRQ_TYPE_EDGE_RISING; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 104 | break; |
| 105 | case IRQ_TYPE_EDGE_FALLING: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 106 | type = S5P_IRQ_TYPE_EDGE_FALLING; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 107 | break; |
| 108 | case IRQ_TYPE_EDGE_BOTH: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 109 | type = S5P_IRQ_TYPE_EDGE_BOTH; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 110 | break; |
| 111 | case IRQ_TYPE_LEVEL_HIGH: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 112 | type = S5P_IRQ_TYPE_LEVEL_HIGH; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 113 | break; |
| 114 | case IRQ_TYPE_LEVEL_LOW: |
Marek Szyprowski | 9adf5d2 | 2010-10-02 11:48:09 +0900 | [diff] [blame] | 115 | type = S5P_IRQ_TYPE_LEVEL_LOW; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 116 | break; |
| 117 | case IRQ_TYPE_NONE: |
| 118 | default: |
| 119 | printk(KERN_WARNING "No irq type\n"); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 123 | value = __raw_readl(GPIO_BASE(chip) + CON_OFFSET + con_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 124 | value &= ~(0x7 << (offset * 0x4)); |
| 125 | value |= (type << (offset * 0x4)); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 126 | __raw_writel(value, GPIO_BASE(chip) + CON_OFFSET + con_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 131 | static struct irq_chip s5p_gpioint = { |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 132 | .name = "s5p_gpioint", |
Lennert Buytenhek | bb0b237 | 2010-12-14 22:55:26 +0100 | [diff] [blame] | 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, |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static void s5p_gpioint_handler(unsigned int irq, struct irq_desc *desc) |
| 141 | { |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 142 | int group, pend_offset, mask_offset; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 143 | unsigned int pend, mask; |
| 144 | |
| 145 | for (group = 0; group < S5P_GPIOINT_GROUP_MAXNR; group++) { |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 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); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 152 | if (!pend) |
| 153 | continue; |
| 154 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 155 | mask_offset = REG_OFFSET(group); |
| 156 | mask = __raw_readl(GPIO_BASE(chip) + MASK_OFFSET + mask_offset); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 157 | pend &= ~mask; |
| 158 | |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 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); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 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); |
Marek Szyprowski | 2de0926 | 2011-03-15 21:17:43 +0900 | [diff] [blame^] | 191 | set_irq_data(irq, chip); |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 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) { |
Joonyoung Shim | 8ce14a2 | 2010-10-01 11:24:39 +0900 | [diff] [blame] | 217 | my_chip->chip.to_irq = samsung_gpiolib_to_irq; |
Marek Szyprowski | 170a461 | 2010-10-01 10:40:37 +0900 | [diff] [blame] | 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 | } |