Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2010 |
| 3 | * |
| 4 | * License Terms: GNU General Public License, version 2 |
| 5 | * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson |
| 6 | * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/gpio.h> |
| 14 | #include <linux/irq.h> |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 15 | #include <linux/irqdomain.h> |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Sundar Iyer | c6eda6c | 2010-12-13 09:33:12 +0530 | [diff] [blame] | 17 | #include <linux/mfd/tc3589x.h> |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * These registers are modified under the irq bus lock and cached to avoid |
| 21 | * unnecessary writes in bus_sync_unlock. |
| 22 | */ |
| 23 | enum { REG_IBE, REG_IEV, REG_IS, REG_IE }; |
| 24 | |
| 25 | #define CACHE_NR_REGS 4 |
| 26 | #define CACHE_NR_BANKS 3 |
| 27 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 28 | struct tc3589x_gpio { |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 29 | struct gpio_chip chip; |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 30 | struct tc3589x *tc3589x; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 31 | struct device *dev; |
| 32 | struct mutex irq_lock; |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 33 | struct irq_domain *domain; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 34 | |
| 35 | int irq_base; |
| 36 | |
| 37 | /* Caches of interrupt control registers for bus_lock */ |
| 38 | u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS]; |
| 39 | u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS]; |
| 40 | }; |
| 41 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 42 | static inline struct tc3589x_gpio *to_tc3589x_gpio(struct gpio_chip *chip) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 43 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 44 | return container_of(chip, struct tc3589x_gpio, chip); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 47 | static int tc3589x_gpio_get(struct gpio_chip *chip, unsigned offset) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 48 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 49 | struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip); |
| 50 | struct tc3589x *tc3589x = tc3589x_gpio->tc3589x; |
| 51 | u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 52 | u8 mask = 1 << (offset % 8); |
| 53 | int ret; |
| 54 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 55 | ret = tc3589x_reg_read(tc3589x, reg); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 56 | if (ret < 0) |
| 57 | return ret; |
| 58 | |
| 59 | return ret & mask; |
| 60 | } |
| 61 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 62 | static void tc3589x_gpio_set(struct gpio_chip *chip, unsigned offset, int val) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 63 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 64 | struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip); |
| 65 | struct tc3589x *tc3589x = tc3589x_gpio->tc3589x; |
| 66 | u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 67 | unsigned pos = offset % 8; |
| 68 | u8 data[] = {!!val << pos, 1 << pos}; |
| 69 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 70 | tc3589x_block_write(tc3589x, reg, ARRAY_SIZE(data), data); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 73 | static int tc3589x_gpio_direction_output(struct gpio_chip *chip, |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 74 | unsigned offset, int val) |
| 75 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 76 | struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip); |
| 77 | struct tc3589x *tc3589x = tc3589x_gpio->tc3589x; |
| 78 | u8 reg = TC3589x_GPIODIR0 + offset / 8; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 79 | unsigned pos = offset % 8; |
| 80 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 81 | tc3589x_gpio_set(chip, offset, val); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 82 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 83 | return tc3589x_set_bits(tc3589x, reg, 1 << pos, 1 << pos); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 86 | static int tc3589x_gpio_direction_input(struct gpio_chip *chip, |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 87 | unsigned offset) |
| 88 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 89 | struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip); |
| 90 | struct tc3589x *tc3589x = tc3589x_gpio->tc3589x; |
| 91 | u8 reg = TC3589x_GPIODIR0 + offset / 8; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 92 | unsigned pos = offset % 8; |
| 93 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 94 | return tc3589x_set_bits(tc3589x, reg, 1 << pos, 0); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 97 | /** |
| 98 | * tc3589x_gpio_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ |
| 99 | * |
| 100 | * @tc3589x_gpio: tc3589x_gpio_irq controller to operate on. |
| 101 | * @irq: index of the interrupt requested in the chip IRQs |
| 102 | * |
| 103 | * Useful for drivers to request their own IRQs. |
| 104 | */ |
| 105 | static int tc3589x_gpio_irq_get_virq(struct tc3589x_gpio *tc3589x_gpio, |
| 106 | int irq) |
| 107 | { |
| 108 | if (!tc3589x_gpio) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | return irq_create_mapping(tc3589x_gpio->domain, irq); |
| 112 | } |
| 113 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 114 | static int tc3589x_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 115 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 116 | struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 117 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 118 | return tc3589x_gpio_irq_get_virq(tc3589x_gpio, offset); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static struct gpio_chip template_chip = { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 122 | .label = "tc3589x", |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 123 | .owner = THIS_MODULE, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 124 | .direction_input = tc3589x_gpio_direction_input, |
| 125 | .get = tc3589x_gpio_get, |
| 126 | .direction_output = tc3589x_gpio_direction_output, |
| 127 | .set = tc3589x_gpio_set, |
| 128 | .to_irq = tc3589x_gpio_to_irq, |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 129 | .can_sleep = 1, |
| 130 | }; |
| 131 | |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 132 | static int tc3589x_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 133 | { |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 134 | struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d); |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 135 | int offset = d->hwirq; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 136 | int regoffset = offset / 8; |
| 137 | int mask = 1 << (offset % 8); |
| 138 | |
| 139 | if (type == IRQ_TYPE_EDGE_BOTH) { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 140 | tc3589x_gpio->regs[REG_IBE][regoffset] |= mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 144 | tc3589x_gpio->regs[REG_IBE][regoffset] &= ~mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 145 | |
| 146 | if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 147 | tc3589x_gpio->regs[REG_IS][regoffset] |= mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 148 | else |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 149 | tc3589x_gpio->regs[REG_IS][regoffset] &= ~mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 150 | |
| 151 | if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 152 | tc3589x_gpio->regs[REG_IEV][regoffset] |= mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 153 | else |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 154 | tc3589x_gpio->regs[REG_IEV][regoffset] &= ~mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 159 | static void tc3589x_gpio_irq_lock(struct irq_data *d) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 160 | { |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 161 | struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 162 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 163 | mutex_lock(&tc3589x_gpio->irq_lock); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 166 | static void tc3589x_gpio_irq_sync_unlock(struct irq_data *d) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 167 | { |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 168 | struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d); |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 169 | struct tc3589x *tc3589x = tc3589x_gpio->tc3589x; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 170 | static const u8 regmap[] = { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 171 | [REG_IBE] = TC3589x_GPIOIBE0, |
| 172 | [REG_IEV] = TC3589x_GPIOIEV0, |
| 173 | [REG_IS] = TC3589x_GPIOIS0, |
| 174 | [REG_IE] = TC3589x_GPIOIE0, |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 175 | }; |
| 176 | int i, j; |
| 177 | |
| 178 | for (i = 0; i < CACHE_NR_REGS; i++) { |
| 179 | for (j = 0; j < CACHE_NR_BANKS; j++) { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 180 | u8 old = tc3589x_gpio->oldregs[i][j]; |
| 181 | u8 new = tc3589x_gpio->regs[i][j]; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 182 | |
| 183 | if (new == old) |
| 184 | continue; |
| 185 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 186 | tc3589x_gpio->oldregs[i][j] = new; |
| 187 | tc3589x_reg_write(tc3589x, regmap[i] + j * 8, new); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 191 | mutex_unlock(&tc3589x_gpio->irq_lock); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 194 | static void tc3589x_gpio_irq_mask(struct irq_data *d) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 195 | { |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 196 | struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d); |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 197 | int offset = d->hwirq; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 198 | int regoffset = offset / 8; |
| 199 | int mask = 1 << (offset % 8); |
| 200 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 201 | tc3589x_gpio->regs[REG_IE][regoffset] &= ~mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 204 | static void tc3589x_gpio_irq_unmask(struct irq_data *d) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 205 | { |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 206 | struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d); |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 207 | int offset = d->hwirq; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 208 | int regoffset = offset / 8; |
| 209 | int mask = 1 << (offset % 8); |
| 210 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 211 | tc3589x_gpio->regs[REG_IE][regoffset] |= mask; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 214 | static struct irq_chip tc3589x_gpio_irq_chip = { |
| 215 | .name = "tc3589x-gpio", |
Lennert Buytenhek | 33fcc1b | 2011-01-12 17:00:19 -0800 | [diff] [blame] | 216 | .irq_bus_lock = tc3589x_gpio_irq_lock, |
| 217 | .irq_bus_sync_unlock = tc3589x_gpio_irq_sync_unlock, |
| 218 | .irq_mask = tc3589x_gpio_irq_mask, |
| 219 | .irq_unmask = tc3589x_gpio_irq_unmask, |
| 220 | .irq_set_type = tc3589x_gpio_irq_set_type, |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 221 | }; |
| 222 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 223 | static irqreturn_t tc3589x_gpio_irq(int irq, void *dev) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 224 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 225 | struct tc3589x_gpio *tc3589x_gpio = dev; |
| 226 | struct tc3589x *tc3589x = tc3589x_gpio->tc3589x; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 227 | u8 status[CACHE_NR_BANKS]; |
| 228 | int ret; |
| 229 | int i; |
| 230 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 231 | ret = tc3589x_block_read(tc3589x, TC3589x_GPIOMIS0, |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 232 | ARRAY_SIZE(status), status); |
| 233 | if (ret < 0) |
| 234 | return IRQ_NONE; |
| 235 | |
| 236 | for (i = 0; i < ARRAY_SIZE(status); i++) { |
| 237 | unsigned int stat = status[i]; |
| 238 | if (!stat) |
| 239 | continue; |
| 240 | |
| 241 | while (stat) { |
| 242 | int bit = __ffs(stat); |
| 243 | int line = i * 8 + bit; |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 244 | int virq = tc3589x_gpio_irq_get_virq(tc3589x_gpio, line); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 245 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 246 | handle_nested_irq(virq); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 247 | stat &= ~(1 << bit); |
| 248 | } |
| 249 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 250 | tc3589x_reg_write(tc3589x, TC3589x_GPIOIC0 + i, status[i]); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return IRQ_HANDLED; |
| 254 | } |
| 255 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 256 | static int tc3589x_gpio_irq_map(struct irq_domain *d, unsigned int virq, |
| 257 | irq_hw_number_t hwirq) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 258 | { |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 259 | struct tc3589x *tc3589x_gpio = d->host_data; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 260 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 261 | irq_set_chip_data(virq, tc3589x_gpio); |
| 262 | irq_set_chip_and_handler(virq, &tc3589x_gpio_irq_chip, |
| 263 | handle_simple_irq); |
| 264 | irq_set_nested_thread(virq, 1); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 265 | #ifdef CONFIG_ARM |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 266 | set_irq_flags(virq, IRQF_VALID); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 267 | #else |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 268 | irq_set_noprobe(virq); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 269 | #endif |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 274 | static void tc3589x_gpio_irq_unmap(struct irq_domain *d, unsigned int virq) |
| 275 | { |
| 276 | #ifdef CONFIG_ARM |
| 277 | set_irq_flags(virq, 0); |
| 278 | #endif |
| 279 | irq_set_chip_and_handler(virq, NULL, NULL); |
| 280 | irq_set_chip_data(virq, NULL); |
| 281 | } |
| 282 | |
| 283 | static struct irq_domain_ops tc3589x_irq_ops = { |
| 284 | .map = tc3589x_gpio_irq_map, |
| 285 | .unmap = tc3589x_gpio_irq_unmap, |
| 286 | .xlate = irq_domain_xlate_twocell, |
| 287 | }; |
| 288 | |
| 289 | static int tc3589x_gpio_irq_init(struct tc3589x_gpio *tc3589x_gpio) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 290 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 291 | int base = tc3589x_gpio->irq_base; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 292 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 293 | if (base) { |
| 294 | tc3589x_gpio->domain = irq_domain_add_legacy( |
| 295 | NULL, tc3589x_gpio->chip.ngpio, base, |
| 296 | 0, &tc3589x_irq_ops, tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 297 | } |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 298 | else { |
| 299 | tc3589x_gpio->domain = irq_domain_add_linear( |
| 300 | NULL, tc3589x_gpio->chip.ngpio, |
| 301 | &tc3589x_irq_ops, tc3589x_gpio); |
| 302 | } |
| 303 | |
| 304 | if (!tc3589x_gpio->domain) { |
| 305 | dev_err(tc3589x_gpio->dev, "Failed to create irqdomain\n"); |
| 306 | return -ENOSYS; |
| 307 | } |
| 308 | |
| 309 | return 0; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 312 | static int __devinit tc3589x_gpio_probe(struct platform_device *pdev) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 313 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 314 | struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent); |
| 315 | struct tc3589x_gpio_platform_data *pdata; |
| 316 | struct tc3589x_gpio *tc3589x_gpio; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 317 | int ret; |
| 318 | int irq; |
| 319 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 320 | pdata = tc3589x->pdata->gpio; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 321 | if (!pdata) |
| 322 | return -ENODEV; |
| 323 | |
| 324 | irq = platform_get_irq(pdev, 0); |
| 325 | if (irq < 0) |
| 326 | return irq; |
| 327 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 328 | tc3589x_gpio = kzalloc(sizeof(struct tc3589x_gpio), GFP_KERNEL); |
| 329 | if (!tc3589x_gpio) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 330 | return -ENOMEM; |
| 331 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 332 | mutex_init(&tc3589x_gpio->irq_lock); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 333 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 334 | tc3589x_gpio->dev = &pdev->dev; |
| 335 | tc3589x_gpio->tc3589x = tc3589x; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 336 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 337 | tc3589x_gpio->chip = template_chip; |
| 338 | tc3589x_gpio->chip.ngpio = tc3589x->num_gpio; |
| 339 | tc3589x_gpio->chip.dev = &pdev->dev; |
| 340 | tc3589x_gpio->chip.base = pdata->gpio_base; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 341 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 342 | tc3589x_gpio->irq_base = tc3589x->irq_base + TC3589x_INT_GPIO(0); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 343 | |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 344 | tc3589x_gpio->irq_base = tc3589x->irq_base ? |
| 345 | tc3589x->irq_base + TC3589x_INT_GPIO(0) : 0; |
| 346 | |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 347 | /* Bring the GPIO module out of reset */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 348 | ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, |
| 349 | TC3589x_RSTCTRL_GPIRST, 0); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 350 | if (ret < 0) |
| 351 | goto out_free; |
| 352 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 353 | ret = tc3589x_gpio_irq_init(tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 354 | if (ret) |
| 355 | goto out_free; |
| 356 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 357 | ret = request_threaded_irq(irq, NULL, tc3589x_gpio_irq, IRQF_ONESHOT, |
| 358 | "tc3589x-gpio", tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 359 | if (ret) { |
| 360 | dev_err(&pdev->dev, "unable to get irq: %d\n", ret); |
Lee Jones | efe4c94 | 2012-09-07 12:14:58 +0100 | [diff] [blame^] | 361 | goto out_free; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 364 | ret = gpiochip_add(&tc3589x_gpio->chip); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 365 | if (ret) { |
| 366 | dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret); |
| 367 | goto out_freeirq; |
| 368 | } |
| 369 | |
Rabin Vincent | f0a7a98 | 2010-09-13 13:04:02 +0100 | [diff] [blame] | 370 | if (pdata->setup) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 371 | pdata->setup(tc3589x, tc3589x_gpio->chip.base); |
Rabin Vincent | f0a7a98 | 2010-09-13 13:04:02 +0100 | [diff] [blame] | 372 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 373 | platform_set_drvdata(pdev, tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 374 | |
| 375 | return 0; |
| 376 | |
| 377 | out_freeirq: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 378 | free_irq(irq, tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 379 | out_free: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 380 | kfree(tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 381 | return ret; |
| 382 | } |
| 383 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 384 | static int __devexit tc3589x_gpio_remove(struct platform_device *pdev) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 385 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 386 | struct tc3589x_gpio *tc3589x_gpio = platform_get_drvdata(pdev); |
| 387 | struct tc3589x *tc3589x = tc3589x_gpio->tc3589x; |
| 388 | struct tc3589x_gpio_platform_data *pdata = tc3589x->pdata->gpio; |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 389 | int irq = platform_get_irq(pdev, 0); |
| 390 | int ret; |
| 391 | |
Rabin Vincent | f0a7a98 | 2010-09-13 13:04:02 +0100 | [diff] [blame] | 392 | if (pdata->remove) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 393 | pdata->remove(tc3589x, tc3589x_gpio->chip.base); |
Rabin Vincent | f0a7a98 | 2010-09-13 13:04:02 +0100 | [diff] [blame] | 394 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 395 | ret = gpiochip_remove(&tc3589x_gpio->chip); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 396 | if (ret < 0) { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 397 | dev_err(tc3589x_gpio->dev, |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 398 | "unable to remove gpiochip: %d\n", ret); |
| 399 | return ret; |
| 400 | } |
| 401 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 402 | free_irq(irq, tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 403 | |
| 404 | platform_set_drvdata(pdev, NULL); |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 405 | kfree(tc3589x_gpio); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 410 | static struct platform_driver tc3589x_gpio_driver = { |
| 411 | .driver.name = "tc3589x-gpio", |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 412 | .driver.owner = THIS_MODULE, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 413 | .probe = tc3589x_gpio_probe, |
| 414 | .remove = __devexit_p(tc3589x_gpio_remove), |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 415 | }; |
| 416 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 417 | static int __init tc3589x_gpio_init(void) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 418 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 419 | return platform_driver_register(&tc3589x_gpio_driver); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 420 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 421 | subsys_initcall(tc3589x_gpio_init); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 422 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 423 | static void __exit tc3589x_gpio_exit(void) |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 424 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 425 | platform_driver_unregister(&tc3589x_gpio_driver); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 426 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 427 | module_exit(tc3589x_gpio_exit); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 428 | |
| 429 | MODULE_LICENSE("GPL v2"); |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 430 | MODULE_DESCRIPTION("TC3589x GPIO driver"); |
Rabin Vincent | d88b25b | 2010-05-10 23:43:47 +0200 | [diff] [blame] | 431 | MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent"); |