blob: 6e8900933972ee7de0921fc3abef6704eaca9337 [file] [log] [blame]
Rabin Vincentd88b25b2010-05-10 23:43:47 +02001/*
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 Jonesefe4c942012-09-07 12:14:58 +010015#include <linux/irqdomain.h>
Rabin Vincentd88b25b2010-05-10 23:43:47 +020016#include <linux/interrupt.h>
Sundar Iyerc6eda6c2010-12-13 09:33:12 +053017#include <linux/mfd/tc3589x.h>
Rabin Vincentd88b25b2010-05-10 23:43:47 +020018
19/*
20 * These registers are modified under the irq bus lock and cached to avoid
21 * unnecessary writes in bus_sync_unlock.
22 */
23enum { REG_IBE, REG_IEV, REG_IS, REG_IE };
24
25#define CACHE_NR_REGS 4
26#define CACHE_NR_BANKS 3
27
Sundar Iyer20406eb2010-12-13 09:33:14 +053028struct tc3589x_gpio {
Rabin Vincentd88b25b2010-05-10 23:43:47 +020029 struct gpio_chip chip;
Sundar Iyer20406eb2010-12-13 09:33:14 +053030 struct tc3589x *tc3589x;
Rabin Vincentd88b25b2010-05-10 23:43:47 +020031 struct device *dev;
32 struct mutex irq_lock;
Lee Jonesefe4c942012-09-07 12:14:58 +010033 struct irq_domain *domain;
Rabin Vincentd88b25b2010-05-10 23:43:47 +020034
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 Iyer20406eb2010-12-13 09:33:14 +053042static inline struct tc3589x_gpio *to_tc3589x_gpio(struct gpio_chip *chip)
Rabin Vincentd88b25b2010-05-10 23:43:47 +020043{
Sundar Iyer20406eb2010-12-13 09:33:14 +053044 return container_of(chip, struct tc3589x_gpio, chip);
Rabin Vincentd88b25b2010-05-10 23:43:47 +020045}
46
Sundar Iyer20406eb2010-12-13 09:33:14 +053047static int tc3589x_gpio_get(struct gpio_chip *chip, unsigned offset)
Rabin Vincentd88b25b2010-05-10 23:43:47 +020048{
Sundar Iyer20406eb2010-12-13 09:33:14 +053049 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 Vincentd88b25b2010-05-10 23:43:47 +020052 u8 mask = 1 << (offset % 8);
53 int ret;
54
Sundar Iyer20406eb2010-12-13 09:33:14 +053055 ret = tc3589x_reg_read(tc3589x, reg);
Rabin Vincentd88b25b2010-05-10 23:43:47 +020056 if (ret < 0)
57 return ret;
58
59 return ret & mask;
60}
61
Sundar Iyer20406eb2010-12-13 09:33:14 +053062static void tc3589x_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
Rabin Vincentd88b25b2010-05-10 23:43:47 +020063{
Sundar Iyer20406eb2010-12-13 09:33:14 +053064 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 Vincentd88b25b2010-05-10 23:43:47 +020067 unsigned pos = offset % 8;
68 u8 data[] = {!!val << pos, 1 << pos};
69
Sundar Iyer20406eb2010-12-13 09:33:14 +053070 tc3589x_block_write(tc3589x, reg, ARRAY_SIZE(data), data);
Rabin Vincentd88b25b2010-05-10 23:43:47 +020071}
72
Sundar Iyer20406eb2010-12-13 09:33:14 +053073static int tc3589x_gpio_direction_output(struct gpio_chip *chip,
Rabin Vincentd88b25b2010-05-10 23:43:47 +020074 unsigned offset, int val)
75{
Sundar Iyer20406eb2010-12-13 09:33:14 +053076 struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
77 struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
78 u8 reg = TC3589x_GPIODIR0 + offset / 8;
Rabin Vincentd88b25b2010-05-10 23:43:47 +020079 unsigned pos = offset % 8;
80
Sundar Iyer20406eb2010-12-13 09:33:14 +053081 tc3589x_gpio_set(chip, offset, val);
Rabin Vincentd88b25b2010-05-10 23:43:47 +020082
Sundar Iyer20406eb2010-12-13 09:33:14 +053083 return tc3589x_set_bits(tc3589x, reg, 1 << pos, 1 << pos);
Rabin Vincentd88b25b2010-05-10 23:43:47 +020084}
85
Sundar Iyer20406eb2010-12-13 09:33:14 +053086static int tc3589x_gpio_direction_input(struct gpio_chip *chip,
Rabin Vincentd88b25b2010-05-10 23:43:47 +020087 unsigned offset)
88{
Sundar Iyer20406eb2010-12-13 09:33:14 +053089 struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
90 struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
91 u8 reg = TC3589x_GPIODIR0 + offset / 8;
Rabin Vincentd88b25b2010-05-10 23:43:47 +020092 unsigned pos = offset % 8;
93
Sundar Iyer20406eb2010-12-13 09:33:14 +053094 return tc3589x_set_bits(tc3589x, reg, 1 << pos, 0);
Rabin Vincentd88b25b2010-05-10 23:43:47 +020095}
96
Lee Jonesefe4c942012-09-07 12:14:58 +010097/**
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 */
105static 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 Iyer20406eb2010-12-13 09:33:14 +0530114static int tc3589x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200115{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530116 struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200117
Lee Jonesefe4c942012-09-07 12:14:58 +0100118 return tc3589x_gpio_irq_get_virq(tc3589x_gpio, offset);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200119}
120
121static struct gpio_chip template_chip = {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530122 .label = "tc3589x",
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200123 .owner = THIS_MODULE,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530124 .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 Vincentd88b25b2010-05-10 23:43:47 +0200129 .can_sleep = 1,
130};
131
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800132static int tc3589x_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200133{
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800134 struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
Lee Jonesefe4c942012-09-07 12:14:58 +0100135 int offset = d->hwirq;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200136 int regoffset = offset / 8;
137 int mask = 1 << (offset % 8);
138
139 if (type == IRQ_TYPE_EDGE_BOTH) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530140 tc3589x_gpio->regs[REG_IBE][regoffset] |= mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200141 return 0;
142 }
143
Sundar Iyer20406eb2010-12-13 09:33:14 +0530144 tc3589x_gpio->regs[REG_IBE][regoffset] &= ~mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200145
146 if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH)
Sundar Iyer20406eb2010-12-13 09:33:14 +0530147 tc3589x_gpio->regs[REG_IS][regoffset] |= mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200148 else
Sundar Iyer20406eb2010-12-13 09:33:14 +0530149 tc3589x_gpio->regs[REG_IS][regoffset] &= ~mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200150
151 if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH)
Sundar Iyer20406eb2010-12-13 09:33:14 +0530152 tc3589x_gpio->regs[REG_IEV][regoffset] |= mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200153 else
Sundar Iyer20406eb2010-12-13 09:33:14 +0530154 tc3589x_gpio->regs[REG_IEV][regoffset] &= ~mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200155
156 return 0;
157}
158
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800159static void tc3589x_gpio_irq_lock(struct irq_data *d)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200160{
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800161 struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200162
Sundar Iyer20406eb2010-12-13 09:33:14 +0530163 mutex_lock(&tc3589x_gpio->irq_lock);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200164}
165
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800166static void tc3589x_gpio_irq_sync_unlock(struct irq_data *d)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200167{
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800168 struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
Sundar Iyer20406eb2010-12-13 09:33:14 +0530169 struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200170 static const u8 regmap[] = {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530171 [REG_IBE] = TC3589x_GPIOIBE0,
172 [REG_IEV] = TC3589x_GPIOIEV0,
173 [REG_IS] = TC3589x_GPIOIS0,
174 [REG_IE] = TC3589x_GPIOIE0,
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200175 };
176 int i, j;
177
178 for (i = 0; i < CACHE_NR_REGS; i++) {
179 for (j = 0; j < CACHE_NR_BANKS; j++) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530180 u8 old = tc3589x_gpio->oldregs[i][j];
181 u8 new = tc3589x_gpio->regs[i][j];
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200182
183 if (new == old)
184 continue;
185
Sundar Iyer20406eb2010-12-13 09:33:14 +0530186 tc3589x_gpio->oldregs[i][j] = new;
187 tc3589x_reg_write(tc3589x, regmap[i] + j * 8, new);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200188 }
189 }
190
Sundar Iyer20406eb2010-12-13 09:33:14 +0530191 mutex_unlock(&tc3589x_gpio->irq_lock);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200192}
193
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800194static void tc3589x_gpio_irq_mask(struct irq_data *d)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200195{
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800196 struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
Lee Jonesefe4c942012-09-07 12:14:58 +0100197 int offset = d->hwirq;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200198 int regoffset = offset / 8;
199 int mask = 1 << (offset % 8);
200
Sundar Iyer20406eb2010-12-13 09:33:14 +0530201 tc3589x_gpio->regs[REG_IE][regoffset] &= ~mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200202}
203
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800204static void tc3589x_gpio_irq_unmask(struct irq_data *d)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200205{
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800206 struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
Lee Jonesefe4c942012-09-07 12:14:58 +0100207 int offset = d->hwirq;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200208 int regoffset = offset / 8;
209 int mask = 1 << (offset % 8);
210
Sundar Iyer20406eb2010-12-13 09:33:14 +0530211 tc3589x_gpio->regs[REG_IE][regoffset] |= mask;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200212}
213
Sundar Iyer20406eb2010-12-13 09:33:14 +0530214static struct irq_chip tc3589x_gpio_irq_chip = {
215 .name = "tc3589x-gpio",
Lennert Buytenhek33fcc1b2011-01-12 17:00:19 -0800216 .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 Vincentd88b25b2010-05-10 23:43:47 +0200221};
222
Sundar Iyer20406eb2010-12-13 09:33:14 +0530223static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200224{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530225 struct tc3589x_gpio *tc3589x_gpio = dev;
226 struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200227 u8 status[CACHE_NR_BANKS];
228 int ret;
229 int i;
230
Sundar Iyer20406eb2010-12-13 09:33:14 +0530231 ret = tc3589x_block_read(tc3589x, TC3589x_GPIOMIS0,
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200232 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 Jonesefe4c942012-09-07 12:14:58 +0100244 int virq = tc3589x_gpio_irq_get_virq(tc3589x_gpio, line);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200245
Lee Jonesefe4c942012-09-07 12:14:58 +0100246 handle_nested_irq(virq);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200247 stat &= ~(1 << bit);
248 }
249
Sundar Iyer20406eb2010-12-13 09:33:14 +0530250 tc3589x_reg_write(tc3589x, TC3589x_GPIOIC0 + i, status[i]);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200251 }
252
253 return IRQ_HANDLED;
254}
255
Lee Jonesefe4c942012-09-07 12:14:58 +0100256static int tc3589x_gpio_irq_map(struct irq_domain *d, unsigned int virq,
257 irq_hw_number_t hwirq)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200258{
Lee Jonesefe4c942012-09-07 12:14:58 +0100259 struct tc3589x *tc3589x_gpio = d->host_data;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200260
Lee Jonesefe4c942012-09-07 12:14:58 +0100261 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 Vincentd88b25b2010-05-10 23:43:47 +0200265#ifdef CONFIG_ARM
Lee Jonesefe4c942012-09-07 12:14:58 +0100266 set_irq_flags(virq, IRQF_VALID);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200267#else
Lee Jonesefe4c942012-09-07 12:14:58 +0100268 irq_set_noprobe(virq);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200269#endif
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200270
271 return 0;
272}
273
Lee Jonesefe4c942012-09-07 12:14:58 +0100274static 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
283static 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
289static int tc3589x_gpio_irq_init(struct tc3589x_gpio *tc3589x_gpio)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200290{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530291 int base = tc3589x_gpio->irq_base;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200292
Lee Jonesefe4c942012-09-07 12:14:58 +0100293 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 Vincentd88b25b2010-05-10 23:43:47 +0200297 }
Lee Jonesefe4c942012-09-07 12:14:58 +0100298 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 Vincentd88b25b2010-05-10 23:43:47 +0200310}
311
Sundar Iyer20406eb2010-12-13 09:33:14 +0530312static int __devinit tc3589x_gpio_probe(struct platform_device *pdev)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200313{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530314 struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
315 struct tc3589x_gpio_platform_data *pdata;
316 struct tc3589x_gpio *tc3589x_gpio;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200317 int ret;
318 int irq;
319
Sundar Iyer20406eb2010-12-13 09:33:14 +0530320 pdata = tc3589x->pdata->gpio;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200321 if (!pdata)
322 return -ENODEV;
323
324 irq = platform_get_irq(pdev, 0);
325 if (irq < 0)
326 return irq;
327
Sundar Iyer20406eb2010-12-13 09:33:14 +0530328 tc3589x_gpio = kzalloc(sizeof(struct tc3589x_gpio), GFP_KERNEL);
329 if (!tc3589x_gpio)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200330 return -ENOMEM;
331
Sundar Iyer20406eb2010-12-13 09:33:14 +0530332 mutex_init(&tc3589x_gpio->irq_lock);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200333
Sundar Iyer20406eb2010-12-13 09:33:14 +0530334 tc3589x_gpio->dev = &pdev->dev;
335 tc3589x_gpio->tc3589x = tc3589x;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200336
Sundar Iyer20406eb2010-12-13 09:33:14 +0530337 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 Vincentd88b25b2010-05-10 23:43:47 +0200341
Sundar Iyer20406eb2010-12-13 09:33:14 +0530342 tc3589x_gpio->irq_base = tc3589x->irq_base + TC3589x_INT_GPIO(0);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200343
Lee Jonesefe4c942012-09-07 12:14:58 +0100344 tc3589x_gpio->irq_base = tc3589x->irq_base ?
345 tc3589x->irq_base + TC3589x_INT_GPIO(0) : 0;
346
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200347 /* Bring the GPIO module out of reset */
Sundar Iyer20406eb2010-12-13 09:33:14 +0530348 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL,
349 TC3589x_RSTCTRL_GPIRST, 0);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200350 if (ret < 0)
351 goto out_free;
352
Sundar Iyer20406eb2010-12-13 09:33:14 +0530353 ret = tc3589x_gpio_irq_init(tc3589x_gpio);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200354 if (ret)
355 goto out_free;
356
Sundar Iyer20406eb2010-12-13 09:33:14 +0530357 ret = request_threaded_irq(irq, NULL, tc3589x_gpio_irq, IRQF_ONESHOT,
358 "tc3589x-gpio", tc3589x_gpio);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200359 if (ret) {
360 dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
Lee Jonesefe4c942012-09-07 12:14:58 +0100361 goto out_free;
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200362 }
363
Sundar Iyer20406eb2010-12-13 09:33:14 +0530364 ret = gpiochip_add(&tc3589x_gpio->chip);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200365 if (ret) {
366 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
367 goto out_freeirq;
368 }
369
Rabin Vincentf0a7a982010-09-13 13:04:02 +0100370 if (pdata->setup)
Sundar Iyer20406eb2010-12-13 09:33:14 +0530371 pdata->setup(tc3589x, tc3589x_gpio->chip.base);
Rabin Vincentf0a7a982010-09-13 13:04:02 +0100372
Sundar Iyer20406eb2010-12-13 09:33:14 +0530373 platform_set_drvdata(pdev, tc3589x_gpio);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200374
375 return 0;
376
377out_freeirq:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530378 free_irq(irq, tc3589x_gpio);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200379out_free:
Sundar Iyer20406eb2010-12-13 09:33:14 +0530380 kfree(tc3589x_gpio);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200381 return ret;
382}
383
Sundar Iyer20406eb2010-12-13 09:33:14 +0530384static int __devexit tc3589x_gpio_remove(struct platform_device *pdev)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200385{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530386 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 Vincentd88b25b2010-05-10 23:43:47 +0200389 int irq = platform_get_irq(pdev, 0);
390 int ret;
391
Rabin Vincentf0a7a982010-09-13 13:04:02 +0100392 if (pdata->remove)
Sundar Iyer20406eb2010-12-13 09:33:14 +0530393 pdata->remove(tc3589x, tc3589x_gpio->chip.base);
Rabin Vincentf0a7a982010-09-13 13:04:02 +0100394
Sundar Iyer20406eb2010-12-13 09:33:14 +0530395 ret = gpiochip_remove(&tc3589x_gpio->chip);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200396 if (ret < 0) {
Sundar Iyer20406eb2010-12-13 09:33:14 +0530397 dev_err(tc3589x_gpio->dev,
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200398 "unable to remove gpiochip: %d\n", ret);
399 return ret;
400 }
401
Sundar Iyer20406eb2010-12-13 09:33:14 +0530402 free_irq(irq, tc3589x_gpio);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200403
404 platform_set_drvdata(pdev, NULL);
Sundar Iyer20406eb2010-12-13 09:33:14 +0530405 kfree(tc3589x_gpio);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200406
407 return 0;
408}
409
Sundar Iyer20406eb2010-12-13 09:33:14 +0530410static struct platform_driver tc3589x_gpio_driver = {
411 .driver.name = "tc3589x-gpio",
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200412 .driver.owner = THIS_MODULE,
Sundar Iyer20406eb2010-12-13 09:33:14 +0530413 .probe = tc3589x_gpio_probe,
414 .remove = __devexit_p(tc3589x_gpio_remove),
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200415};
416
Sundar Iyer20406eb2010-12-13 09:33:14 +0530417static int __init tc3589x_gpio_init(void)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200418{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530419 return platform_driver_register(&tc3589x_gpio_driver);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200420}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530421subsys_initcall(tc3589x_gpio_init);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200422
Sundar Iyer20406eb2010-12-13 09:33:14 +0530423static void __exit tc3589x_gpio_exit(void)
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200424{
Sundar Iyer20406eb2010-12-13 09:33:14 +0530425 platform_driver_unregister(&tc3589x_gpio_driver);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200426}
Sundar Iyer20406eb2010-12-13 09:33:14 +0530427module_exit(tc3589x_gpio_exit);
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200428
429MODULE_LICENSE("GPL v2");
Sundar Iyer20406eb2010-12-13 09:33:14 +0530430MODULE_DESCRIPTION("TC3589x GPIO driver");
Rabin Vincentd88b25b2010-05-10 23:43:47 +0200431MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");