xref: /openbmc/linux/drivers/gpio/gpio-stmpe.c (revision 3836309d93462bbf34851c078be6e5e77d888e3d)
1c103de24SGrant Likely /*
2c103de24SGrant Likely  * Copyright (C) ST-Ericsson SA 2010
3c103de24SGrant Likely  *
4c103de24SGrant Likely  * License Terms: GNU General Public License, version 2
5c103de24SGrant Likely  * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
6c103de24SGrant Likely  */
7c103de24SGrant Likely 
8c103de24SGrant Likely #include <linux/module.h>
9c103de24SGrant Likely #include <linux/init.h>
10c103de24SGrant Likely #include <linux/platform_device.h>
11c103de24SGrant Likely #include <linux/slab.h>
12c103de24SGrant Likely #include <linux/gpio.h>
13c103de24SGrant Likely #include <linux/irq.h>
14c103de24SGrant Likely #include <linux/interrupt.h>
15c103de24SGrant Likely #include <linux/mfd/stmpe.h>
16c103de24SGrant Likely 
17c103de24SGrant Likely /*
18c103de24SGrant Likely  * These registers are modified under the irq bus lock and cached to avoid
19c103de24SGrant Likely  * unnecessary writes in bus_sync_unlock.
20c103de24SGrant Likely  */
21c103de24SGrant Likely enum { REG_RE, REG_FE, REG_IE };
22c103de24SGrant Likely 
23c103de24SGrant Likely #define CACHE_NR_REGS	3
24c103de24SGrant Likely #define CACHE_NR_BANKS	(STMPE_NR_GPIOS / 8)
25c103de24SGrant Likely 
26c103de24SGrant Likely struct stmpe_gpio {
27c103de24SGrant Likely 	struct gpio_chip chip;
28c103de24SGrant Likely 	struct stmpe *stmpe;
29c103de24SGrant Likely 	struct device *dev;
30c103de24SGrant Likely 	struct mutex irq_lock;
31c103de24SGrant Likely 
32c103de24SGrant Likely 	int irq_base;
33c103de24SGrant Likely 	unsigned norequest_mask;
34c103de24SGrant Likely 
35c103de24SGrant Likely 	/* Caches of interrupt control registers for bus_lock */
36c103de24SGrant Likely 	u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
37c103de24SGrant Likely 	u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
38c103de24SGrant Likely };
39c103de24SGrant Likely 
40c103de24SGrant Likely static inline struct stmpe_gpio *to_stmpe_gpio(struct gpio_chip *chip)
41c103de24SGrant Likely {
42c103de24SGrant Likely 	return container_of(chip, struct stmpe_gpio, chip);
43c103de24SGrant Likely }
44c103de24SGrant Likely 
45c103de24SGrant Likely static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset)
46c103de24SGrant Likely {
47c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
48c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
49c103de24SGrant Likely 	u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB] - (offset / 8);
50c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
51c103de24SGrant Likely 	int ret;
52c103de24SGrant Likely 
53c103de24SGrant Likely 	ret = stmpe_reg_read(stmpe, reg);
54c103de24SGrant Likely 	if (ret < 0)
55c103de24SGrant Likely 		return ret;
56c103de24SGrant Likely 
577535b8beSBhupesh Sharma 	return !!(ret & mask);
58c103de24SGrant Likely }
59c103de24SGrant Likely 
60c103de24SGrant Likely static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
61c103de24SGrant Likely {
62c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
63c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
64c103de24SGrant Likely 	int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB;
65c103de24SGrant Likely 	u8 reg = stmpe->regs[which] - (offset / 8);
66c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
67c103de24SGrant Likely 
68cccdceb9SViresh Kumar 	/*
69cccdceb9SViresh Kumar 	 * Some variants have single register for gpio set/clear functionality.
70cccdceb9SViresh Kumar 	 * For them we need to write 0 to clear and 1 to set.
71cccdceb9SViresh Kumar 	 */
72cccdceb9SViresh Kumar 	if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB])
73cccdceb9SViresh Kumar 		stmpe_set_bits(stmpe, reg, mask, val ? mask : 0);
74cccdceb9SViresh Kumar 	else
75c103de24SGrant Likely 		stmpe_reg_write(stmpe, reg, mask);
76c103de24SGrant Likely }
77c103de24SGrant Likely 
78c103de24SGrant Likely static int stmpe_gpio_direction_output(struct gpio_chip *chip,
79c103de24SGrant Likely 					 unsigned offset, int val)
80c103de24SGrant Likely {
81c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
82c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
83c103de24SGrant Likely 	u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
84c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
85c103de24SGrant Likely 
86c103de24SGrant Likely 	stmpe_gpio_set(chip, offset, val);
87c103de24SGrant Likely 
88c103de24SGrant Likely 	return stmpe_set_bits(stmpe, reg, mask, mask);
89c103de24SGrant Likely }
90c103de24SGrant Likely 
91c103de24SGrant Likely static int stmpe_gpio_direction_input(struct gpio_chip *chip,
92c103de24SGrant Likely 					unsigned offset)
93c103de24SGrant Likely {
94c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
95c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
96c103de24SGrant Likely 	u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
97c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
98c103de24SGrant Likely 
99c103de24SGrant Likely 	return stmpe_set_bits(stmpe, reg, mask, 0);
100c103de24SGrant Likely }
101c103de24SGrant Likely 
102c103de24SGrant Likely static int stmpe_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
103c103de24SGrant Likely {
104c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
105c103de24SGrant Likely 
106c103de24SGrant Likely 	return stmpe_gpio->irq_base + offset;
107c103de24SGrant Likely }
108c103de24SGrant Likely 
109c103de24SGrant Likely static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset)
110c103de24SGrant Likely {
111c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
112c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
113c103de24SGrant Likely 
114c103de24SGrant Likely 	if (stmpe_gpio->norequest_mask & (1 << offset))
115c103de24SGrant Likely 		return -EINVAL;
116c103de24SGrant Likely 
117c103de24SGrant Likely 	return stmpe_set_altfunc(stmpe, 1 << offset, STMPE_BLOCK_GPIO);
118c103de24SGrant Likely }
119c103de24SGrant Likely 
120c103de24SGrant Likely static struct gpio_chip template_chip = {
121c103de24SGrant Likely 	.label			= "stmpe",
122c103de24SGrant Likely 	.owner			= THIS_MODULE,
123c103de24SGrant Likely 	.direction_input	= stmpe_gpio_direction_input,
124c103de24SGrant Likely 	.get			= stmpe_gpio_get,
125c103de24SGrant Likely 	.direction_output	= stmpe_gpio_direction_output,
126c103de24SGrant Likely 	.set			= stmpe_gpio_set,
127c103de24SGrant Likely 	.to_irq			= stmpe_gpio_to_irq,
128c103de24SGrant Likely 	.request		= stmpe_gpio_request,
129c103de24SGrant Likely 	.can_sleep		= 1,
130c103de24SGrant Likely };
131c103de24SGrant Likely 
132c103de24SGrant Likely static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type)
133c103de24SGrant Likely {
134c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
135c103de24SGrant Likely 	int offset = d->irq - stmpe_gpio->irq_base;
136c103de24SGrant Likely 	int regoffset = offset / 8;
137c103de24SGrant Likely 	int mask = 1 << (offset % 8);
138c103de24SGrant Likely 
139c103de24SGrant Likely 	if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH)
140c103de24SGrant Likely 		return -EINVAL;
141c103de24SGrant Likely 
142cccdceb9SViresh Kumar 	/* STMPE801 doesn't have RE and FE registers */
143cccdceb9SViresh Kumar 	if (stmpe_gpio->stmpe->partnum == STMPE801)
144cccdceb9SViresh Kumar 		return 0;
145cccdceb9SViresh Kumar 
146c103de24SGrant Likely 	if (type == IRQ_TYPE_EDGE_RISING)
147c103de24SGrant Likely 		stmpe_gpio->regs[REG_RE][regoffset] |= mask;
148c103de24SGrant Likely 	else
149c103de24SGrant Likely 		stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;
150c103de24SGrant Likely 
151c103de24SGrant Likely 	if (type == IRQ_TYPE_EDGE_FALLING)
152c103de24SGrant Likely 		stmpe_gpio->regs[REG_FE][regoffset] |= mask;
153c103de24SGrant Likely 	else
154c103de24SGrant Likely 		stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;
155c103de24SGrant Likely 
156c103de24SGrant Likely 	return 0;
157c103de24SGrant Likely }
158c103de24SGrant Likely 
159c103de24SGrant Likely static void stmpe_gpio_irq_lock(struct irq_data *d)
160c103de24SGrant Likely {
161c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
162c103de24SGrant Likely 
163c103de24SGrant Likely 	mutex_lock(&stmpe_gpio->irq_lock);
164c103de24SGrant Likely }
165c103de24SGrant Likely 
166c103de24SGrant Likely static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
167c103de24SGrant Likely {
168c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
169c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
170c103de24SGrant Likely 	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
171c103de24SGrant Likely 	static const u8 regmap[] = {
172c103de24SGrant Likely 		[REG_RE]	= STMPE_IDX_GPRER_LSB,
173c103de24SGrant Likely 		[REG_FE]	= STMPE_IDX_GPFER_LSB,
174c103de24SGrant Likely 		[REG_IE]	= STMPE_IDX_IEGPIOR_LSB,
175c103de24SGrant Likely 	};
176c103de24SGrant Likely 	int i, j;
177c103de24SGrant Likely 
178c103de24SGrant Likely 	for (i = 0; i < CACHE_NR_REGS; i++) {
179cccdceb9SViresh Kumar 		/* STMPE801 doesn't have RE and FE registers */
180cccdceb9SViresh Kumar 		if ((stmpe->partnum == STMPE801) &&
181cccdceb9SViresh Kumar 				(i != REG_IE))
182cccdceb9SViresh Kumar 			continue;
183cccdceb9SViresh Kumar 
184c103de24SGrant Likely 		for (j = 0; j < num_banks; j++) {
185c103de24SGrant Likely 			u8 old = stmpe_gpio->oldregs[i][j];
186c103de24SGrant Likely 			u8 new = stmpe_gpio->regs[i][j];
187c103de24SGrant Likely 
188c103de24SGrant Likely 			if (new == old)
189c103de24SGrant Likely 				continue;
190c103de24SGrant Likely 
191c103de24SGrant Likely 			stmpe_gpio->oldregs[i][j] = new;
192c103de24SGrant Likely 			stmpe_reg_write(stmpe, stmpe->regs[regmap[i]] - j, new);
193c103de24SGrant Likely 		}
194c103de24SGrant Likely 	}
195c103de24SGrant Likely 
196c103de24SGrant Likely 	mutex_unlock(&stmpe_gpio->irq_lock);
197c103de24SGrant Likely }
198c103de24SGrant Likely 
199c103de24SGrant Likely static void stmpe_gpio_irq_mask(struct irq_data *d)
200c103de24SGrant Likely {
201c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
202c103de24SGrant Likely 	int offset = d->irq - stmpe_gpio->irq_base;
203c103de24SGrant Likely 	int regoffset = offset / 8;
204c103de24SGrant Likely 	int mask = 1 << (offset % 8);
205c103de24SGrant Likely 
206c103de24SGrant Likely 	stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
207c103de24SGrant Likely }
208c103de24SGrant Likely 
209c103de24SGrant Likely static void stmpe_gpio_irq_unmask(struct irq_data *d)
210c103de24SGrant Likely {
211c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
212c103de24SGrant Likely 	int offset = d->irq - stmpe_gpio->irq_base;
213c103de24SGrant Likely 	int regoffset = offset / 8;
214c103de24SGrant Likely 	int mask = 1 << (offset % 8);
215c103de24SGrant Likely 
216c103de24SGrant Likely 	stmpe_gpio->regs[REG_IE][regoffset] |= mask;
217c103de24SGrant Likely }
218c103de24SGrant Likely 
219c103de24SGrant Likely static struct irq_chip stmpe_gpio_irq_chip = {
220c103de24SGrant Likely 	.name			= "stmpe-gpio",
221c103de24SGrant Likely 	.irq_bus_lock		= stmpe_gpio_irq_lock,
222c103de24SGrant Likely 	.irq_bus_sync_unlock	= stmpe_gpio_irq_sync_unlock,
223c103de24SGrant Likely 	.irq_mask		= stmpe_gpio_irq_mask,
224c103de24SGrant Likely 	.irq_unmask		= stmpe_gpio_irq_unmask,
225c103de24SGrant Likely 	.irq_set_type		= stmpe_gpio_irq_set_type,
226c103de24SGrant Likely };
227c103de24SGrant Likely 
228c103de24SGrant Likely static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
229c103de24SGrant Likely {
230c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = dev;
231c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
232c103de24SGrant Likely 	u8 statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
233c103de24SGrant Likely 	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
234c103de24SGrant Likely 	u8 status[num_banks];
235c103de24SGrant Likely 	int ret;
236c103de24SGrant Likely 	int i;
237c103de24SGrant Likely 
238c103de24SGrant Likely 	ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
239c103de24SGrant Likely 	if (ret < 0)
240c103de24SGrant Likely 		return IRQ_NONE;
241c103de24SGrant Likely 
242c103de24SGrant Likely 	for (i = 0; i < num_banks; i++) {
243c103de24SGrant Likely 		int bank = num_banks - i - 1;
244c103de24SGrant Likely 		unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
245c103de24SGrant Likely 		unsigned int stat = status[i];
246c103de24SGrant Likely 
247c103de24SGrant Likely 		stat &= enabled;
248c103de24SGrant Likely 		if (!stat)
249c103de24SGrant Likely 			continue;
250c103de24SGrant Likely 
251c103de24SGrant Likely 		while (stat) {
252c103de24SGrant Likely 			int bit = __ffs(stat);
253c103de24SGrant Likely 			int line = bank * 8 + bit;
254c103de24SGrant Likely 
255c103de24SGrant Likely 			handle_nested_irq(stmpe_gpio->irq_base + line);
256c103de24SGrant Likely 			stat &= ~(1 << bit);
257c103de24SGrant Likely 		}
258c103de24SGrant Likely 
259c103de24SGrant Likely 		stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
260cccdceb9SViresh Kumar 
261cccdceb9SViresh Kumar 		/* Edge detect register is not present on 801 */
262cccdceb9SViresh Kumar 		if (stmpe->partnum != STMPE801)
263cccdceb9SViresh Kumar 			stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_GPEDR_MSB]
264cccdceb9SViresh Kumar 					+ i, status[i]);
265c103de24SGrant Likely 	}
266c103de24SGrant Likely 
267c103de24SGrant Likely 	return IRQ_HANDLED;
268c103de24SGrant Likely }
269c103de24SGrant Likely 
270*3836309dSBill Pemberton static int stmpe_gpio_irq_init(struct stmpe_gpio *stmpe_gpio)
271c103de24SGrant Likely {
272c103de24SGrant Likely 	int base = stmpe_gpio->irq_base;
273c103de24SGrant Likely 	int irq;
274c103de24SGrant Likely 
275c103de24SGrant Likely 	for (irq = base; irq < base + stmpe_gpio->chip.ngpio; irq++) {
276c103de24SGrant Likely 		irq_set_chip_data(irq, stmpe_gpio);
277c103de24SGrant Likely 		irq_set_chip_and_handler(irq, &stmpe_gpio_irq_chip,
278c103de24SGrant Likely 					 handle_simple_irq);
279c103de24SGrant Likely 		irq_set_nested_thread(irq, 1);
280c103de24SGrant Likely #ifdef CONFIG_ARM
281c103de24SGrant Likely 		set_irq_flags(irq, IRQF_VALID);
282c103de24SGrant Likely #else
283c103de24SGrant Likely 		irq_set_noprobe(irq);
284c103de24SGrant Likely #endif
285c103de24SGrant Likely 	}
286c103de24SGrant Likely 
287c103de24SGrant Likely 	return 0;
288c103de24SGrant Likely }
289c103de24SGrant Likely 
290c103de24SGrant Likely static void stmpe_gpio_irq_remove(struct stmpe_gpio *stmpe_gpio)
291c103de24SGrant Likely {
292c103de24SGrant Likely 	int base = stmpe_gpio->irq_base;
293c103de24SGrant Likely 	int irq;
294c103de24SGrant Likely 
295c103de24SGrant Likely 	for (irq = base; irq < base + stmpe_gpio->chip.ngpio; irq++) {
296c103de24SGrant Likely #ifdef CONFIG_ARM
297c103de24SGrant Likely 		set_irq_flags(irq, 0);
298c103de24SGrant Likely #endif
299c103de24SGrant Likely 		irq_set_chip_and_handler(irq, NULL, NULL);
300c103de24SGrant Likely 		irq_set_chip_data(irq, NULL);
301c103de24SGrant Likely 	}
302c103de24SGrant Likely }
303c103de24SGrant Likely 
304*3836309dSBill Pemberton static int stmpe_gpio_probe(struct platform_device *pdev)
305c103de24SGrant Likely {
306c103de24SGrant Likely 	struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
307c103de24SGrant Likely 	struct stmpe_gpio_platform_data *pdata;
308c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio;
309c103de24SGrant Likely 	int ret;
31038040c85SChris Blair 	int irq = 0;
311c103de24SGrant Likely 
312c103de24SGrant Likely 	pdata = stmpe->pdata->gpio;
313c103de24SGrant Likely 
314c103de24SGrant Likely 	irq = platform_get_irq(pdev, 0);
315c103de24SGrant Likely 
316c103de24SGrant Likely 	stmpe_gpio = kzalloc(sizeof(struct stmpe_gpio), GFP_KERNEL);
317c103de24SGrant Likely 	if (!stmpe_gpio)
318c103de24SGrant Likely 		return -ENOMEM;
319c103de24SGrant Likely 
320c103de24SGrant Likely 	mutex_init(&stmpe_gpio->irq_lock);
321c103de24SGrant Likely 
322c103de24SGrant Likely 	stmpe_gpio->dev = &pdev->dev;
323c103de24SGrant Likely 	stmpe_gpio->stmpe = stmpe;
324c103de24SGrant Likely 	stmpe_gpio->norequest_mask = pdata ? pdata->norequest_mask : 0;
325c103de24SGrant Likely 
326c103de24SGrant Likely 	stmpe_gpio->chip = template_chip;
327c103de24SGrant Likely 	stmpe_gpio->chip.ngpio = stmpe->num_gpios;
328c103de24SGrant Likely 	stmpe_gpio->chip.dev = &pdev->dev;
329c103de24SGrant Likely 	stmpe_gpio->chip.base = pdata ? pdata->gpio_base : -1;
330c103de24SGrant Likely 
33138040c85SChris Blair 	if (irq >= 0)
332c103de24SGrant Likely 		stmpe_gpio->irq_base = stmpe->irq_base + STMPE_INT_GPIO(0);
33338040c85SChris Blair 	else
33438040c85SChris Blair 		dev_info(&pdev->dev,
33538040c85SChris Blair 			"device configured in no-irq mode; "
33638040c85SChris Blair 			"irqs are not available\n");
337c103de24SGrant Likely 
338c103de24SGrant Likely 	ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
339c103de24SGrant Likely 	if (ret)
340c103de24SGrant Likely 		goto out_free;
341c103de24SGrant Likely 
34238040c85SChris Blair 	if (irq >= 0) {
343c103de24SGrant Likely 		ret = stmpe_gpio_irq_init(stmpe_gpio);
344c103de24SGrant Likely 		if (ret)
345c103de24SGrant Likely 			goto out_disable;
346c103de24SGrant Likely 
34738040c85SChris Blair 		ret = request_threaded_irq(irq, NULL, stmpe_gpio_irq,
34838040c85SChris Blair 				IRQF_ONESHOT, "stmpe-gpio", stmpe_gpio);
349c103de24SGrant Likely 		if (ret) {
350c103de24SGrant Likely 			dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
351c103de24SGrant Likely 			goto out_removeirq;
352c103de24SGrant Likely 		}
35338040c85SChris Blair 	}
354c103de24SGrant Likely 
355c103de24SGrant Likely 	ret = gpiochip_add(&stmpe_gpio->chip);
356c103de24SGrant Likely 	if (ret) {
357c103de24SGrant Likely 		dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
358c103de24SGrant Likely 		goto out_freeirq;
359c103de24SGrant Likely 	}
360c103de24SGrant Likely 
361c103de24SGrant Likely 	if (pdata && pdata->setup)
362c103de24SGrant Likely 		pdata->setup(stmpe, stmpe_gpio->chip.base);
363c103de24SGrant Likely 
364c103de24SGrant Likely 	platform_set_drvdata(pdev, stmpe_gpio);
365c103de24SGrant Likely 
366c103de24SGrant Likely 	return 0;
367c103de24SGrant Likely 
368c103de24SGrant Likely out_freeirq:
36938040c85SChris Blair 	if (irq >= 0)
370c103de24SGrant Likely 		free_irq(irq, stmpe_gpio);
371c103de24SGrant Likely out_removeirq:
37238040c85SChris Blair 	if (irq >= 0)
373c103de24SGrant Likely 		stmpe_gpio_irq_remove(stmpe_gpio);
374c103de24SGrant Likely out_disable:
375c103de24SGrant Likely 	stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
376c103de24SGrant Likely out_free:
377c103de24SGrant Likely 	kfree(stmpe_gpio);
378c103de24SGrant Likely 	return ret;
379c103de24SGrant Likely }
380c103de24SGrant Likely 
381c103de24SGrant Likely static int __devexit stmpe_gpio_remove(struct platform_device *pdev)
382c103de24SGrant Likely {
383c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = platform_get_drvdata(pdev);
384c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
385c103de24SGrant Likely 	struct stmpe_gpio_platform_data *pdata = stmpe->pdata->gpio;
386c103de24SGrant Likely 	int irq = platform_get_irq(pdev, 0);
387c103de24SGrant Likely 	int ret;
388c103de24SGrant Likely 
389c103de24SGrant Likely 	if (pdata && pdata->remove)
390c103de24SGrant Likely 		pdata->remove(stmpe, stmpe_gpio->chip.base);
391c103de24SGrant Likely 
392c103de24SGrant Likely 	ret = gpiochip_remove(&stmpe_gpio->chip);
393c103de24SGrant Likely 	if (ret < 0) {
394c103de24SGrant Likely 		dev_err(stmpe_gpio->dev,
395c103de24SGrant Likely 			"unable to remove gpiochip: %d\n", ret);
396c103de24SGrant Likely 		return ret;
397c103de24SGrant Likely 	}
398c103de24SGrant Likely 
399c103de24SGrant Likely 	stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
400c103de24SGrant Likely 
40138040c85SChris Blair 	if (irq >= 0) {
402c103de24SGrant Likely 		free_irq(irq, stmpe_gpio);
403c103de24SGrant Likely 		stmpe_gpio_irq_remove(stmpe_gpio);
40438040c85SChris Blair 	}
405c103de24SGrant Likely 	platform_set_drvdata(pdev, NULL);
406c103de24SGrant Likely 	kfree(stmpe_gpio);
407c103de24SGrant Likely 
408c103de24SGrant Likely 	return 0;
409c103de24SGrant Likely }
410c103de24SGrant Likely 
411c103de24SGrant Likely static struct platform_driver stmpe_gpio_driver = {
412c103de24SGrant Likely 	.driver.name	= "stmpe-gpio",
413c103de24SGrant Likely 	.driver.owner	= THIS_MODULE,
414c103de24SGrant Likely 	.probe		= stmpe_gpio_probe,
4158283c4ffSBill Pemberton 	.remove		= stmpe_gpio_remove,
416c103de24SGrant Likely };
417c103de24SGrant Likely 
418c103de24SGrant Likely static int __init stmpe_gpio_init(void)
419c103de24SGrant Likely {
420c103de24SGrant Likely 	return platform_driver_register(&stmpe_gpio_driver);
421c103de24SGrant Likely }
422c103de24SGrant Likely subsys_initcall(stmpe_gpio_init);
423c103de24SGrant Likely 
424c103de24SGrant Likely static void __exit stmpe_gpio_exit(void)
425c103de24SGrant Likely {
426c103de24SGrant Likely 	platform_driver_unregister(&stmpe_gpio_driver);
427c103de24SGrant Likely }
428c103de24SGrant Likely module_exit(stmpe_gpio_exit);
429c103de24SGrant Likely 
430c103de24SGrant Likely MODULE_LICENSE("GPL v2");
431c103de24SGrant Likely MODULE_DESCRIPTION("STMPExxxx GPIO driver");
432c103de24SGrant Likely MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");
433