xref: /openbmc/linux/drivers/gpio/gpio-stmpe.c (revision 58383c78425e4ee1c077253cf297b641c861c02e)
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/interrupt.h>
1486605cfeSVipul Kumar Samar #include <linux/of.h>
15c103de24SGrant Likely #include <linux/mfd/stmpe.h>
1627ec8a9cSLinus Walleij #include <linux/seq_file.h>
17c103de24SGrant Likely 
18c103de24SGrant Likely /*
19c103de24SGrant Likely  * These registers are modified under the irq bus lock and cached to avoid
20c103de24SGrant Likely  * unnecessary writes in bus_sync_unlock.
21c103de24SGrant Likely  */
22c103de24SGrant Likely enum { REG_RE, REG_FE, REG_IE };
23c103de24SGrant Likely 
24c103de24SGrant Likely #define CACHE_NR_REGS	3
259e9dc7d9SLinus Walleij /* No variant has more than 24 GPIOs */
269e9dc7d9SLinus Walleij #define CACHE_NR_BANKS	(24 / 8)
27c103de24SGrant Likely 
28c103de24SGrant Likely struct stmpe_gpio {
29c103de24SGrant Likely 	struct gpio_chip chip;
30c103de24SGrant Likely 	struct stmpe *stmpe;
31c103de24SGrant Likely 	struct device *dev;
32c103de24SGrant Likely 	struct mutex irq_lock;
331dfb4a0dSLinus Walleij 	u32 norequest_mask;
34c103de24SGrant Likely 	/* Caches of interrupt control registers for bus_lock */
35c103de24SGrant Likely 	u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
36c103de24SGrant Likely 	u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
37c103de24SGrant Likely };
38c103de24SGrant Likely 
39c103de24SGrant Likely static inline struct stmpe_gpio *to_stmpe_gpio(struct gpio_chip *chip)
40c103de24SGrant Likely {
41c103de24SGrant Likely 	return container_of(chip, struct stmpe_gpio, chip);
42c103de24SGrant Likely }
43c103de24SGrant Likely 
44c103de24SGrant Likely static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset)
45c103de24SGrant Likely {
46c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
47c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
48c103de24SGrant Likely 	u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB] - (offset / 8);
49c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
50c103de24SGrant Likely 	int ret;
51c103de24SGrant Likely 
52c103de24SGrant Likely 	ret = stmpe_reg_read(stmpe, reg);
53c103de24SGrant Likely 	if (ret < 0)
54c103de24SGrant Likely 		return ret;
55c103de24SGrant Likely 
567535b8beSBhupesh Sharma 	return !!(ret & mask);
57c103de24SGrant Likely }
58c103de24SGrant Likely 
59c103de24SGrant Likely static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
60c103de24SGrant Likely {
61c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
62c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
63c103de24SGrant Likely 	int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB;
64c103de24SGrant Likely 	u8 reg = stmpe->regs[which] - (offset / 8);
65c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
66c103de24SGrant Likely 
67cccdceb9SViresh Kumar 	/*
68cccdceb9SViresh Kumar 	 * Some variants have single register for gpio set/clear functionality.
69cccdceb9SViresh Kumar 	 * For them we need to write 0 to clear and 1 to set.
70cccdceb9SViresh Kumar 	 */
71cccdceb9SViresh Kumar 	if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB])
72cccdceb9SViresh Kumar 		stmpe_set_bits(stmpe, reg, mask, val ? mask : 0);
73cccdceb9SViresh Kumar 	else
74c103de24SGrant Likely 		stmpe_reg_write(stmpe, reg, mask);
75c103de24SGrant Likely }
76c103de24SGrant Likely 
77c103de24SGrant Likely static int stmpe_gpio_direction_output(struct gpio_chip *chip,
78c103de24SGrant Likely 					 unsigned offset, int val)
79c103de24SGrant Likely {
80c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
81c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
82c103de24SGrant Likely 	u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
83c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
84c103de24SGrant Likely 
85c103de24SGrant Likely 	stmpe_gpio_set(chip, offset, val);
86c103de24SGrant Likely 
87c103de24SGrant Likely 	return stmpe_set_bits(stmpe, reg, mask, mask);
88c103de24SGrant Likely }
89c103de24SGrant Likely 
90c103de24SGrant Likely static int stmpe_gpio_direction_input(struct gpio_chip *chip,
91c103de24SGrant Likely 					unsigned offset)
92c103de24SGrant Likely {
93c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
94c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
95c103de24SGrant Likely 	u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
96c103de24SGrant Likely 	u8 mask = 1 << (offset % 8);
97c103de24SGrant Likely 
98c103de24SGrant Likely 	return stmpe_set_bits(stmpe, reg, mask, 0);
99c103de24SGrant Likely }
100c103de24SGrant Likely 
101c103de24SGrant Likely static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset)
102c103de24SGrant Likely {
103c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
104c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
105c103de24SGrant Likely 
106c103de24SGrant Likely 	if (stmpe_gpio->norequest_mask & (1 << offset))
107c103de24SGrant Likely 		return -EINVAL;
108c103de24SGrant Likely 
109c103de24SGrant Likely 	return stmpe_set_altfunc(stmpe, 1 << offset, STMPE_BLOCK_GPIO);
110c103de24SGrant Likely }
111c103de24SGrant Likely 
112c103de24SGrant Likely static struct gpio_chip template_chip = {
113c103de24SGrant Likely 	.label			= "stmpe",
114c103de24SGrant Likely 	.owner			= THIS_MODULE,
115c103de24SGrant Likely 	.direction_input	= stmpe_gpio_direction_input,
116c103de24SGrant Likely 	.get			= stmpe_gpio_get,
117c103de24SGrant Likely 	.direction_output	= stmpe_gpio_direction_output,
118c103de24SGrant Likely 	.set			= stmpe_gpio_set,
119c103de24SGrant Likely 	.request		= stmpe_gpio_request,
1209fb1f39eSLinus Walleij 	.can_sleep		= true,
121c103de24SGrant Likely };
122c103de24SGrant Likely 
123c103de24SGrant Likely static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type)
124c103de24SGrant Likely {
125fe44e70dSLinus Walleij 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
126fe44e70dSLinus Walleij 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
127fc13d5a5SLee Jones 	int offset = d->hwirq;
128c103de24SGrant Likely 	int regoffset = offset / 8;
129c103de24SGrant Likely 	int mask = 1 << (offset % 8);
130c103de24SGrant Likely 
1311fe3bd9eSLinus Walleij 	if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH)
132c103de24SGrant Likely 		return -EINVAL;
133c103de24SGrant Likely 
134cccdceb9SViresh Kumar 	/* STMPE801 doesn't have RE and FE registers */
135cccdceb9SViresh Kumar 	if (stmpe_gpio->stmpe->partnum == STMPE801)
136cccdceb9SViresh Kumar 		return 0;
137cccdceb9SViresh Kumar 
1381fe3bd9eSLinus Walleij 	if (type & IRQ_TYPE_EDGE_RISING)
139c103de24SGrant Likely 		stmpe_gpio->regs[REG_RE][regoffset] |= mask;
140c103de24SGrant Likely 	else
141c103de24SGrant Likely 		stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;
142c103de24SGrant Likely 
1431fe3bd9eSLinus Walleij 	if (type & IRQ_TYPE_EDGE_FALLING)
144c103de24SGrant Likely 		stmpe_gpio->regs[REG_FE][regoffset] |= mask;
145c103de24SGrant Likely 	else
146c103de24SGrant Likely 		stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;
147c103de24SGrant Likely 
148c103de24SGrant Likely 	return 0;
149c103de24SGrant Likely }
150c103de24SGrant Likely 
151c103de24SGrant Likely static void stmpe_gpio_irq_lock(struct irq_data *d)
152c103de24SGrant Likely {
153fe44e70dSLinus Walleij 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
154fe44e70dSLinus Walleij 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
155c103de24SGrant Likely 
156c103de24SGrant Likely 	mutex_lock(&stmpe_gpio->irq_lock);
157c103de24SGrant Likely }
158c103de24SGrant Likely 
159c103de24SGrant Likely static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
160c103de24SGrant Likely {
161fe44e70dSLinus Walleij 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
162fe44e70dSLinus Walleij 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
163c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
164c103de24SGrant Likely 	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
165c103de24SGrant Likely 	static const u8 regmap[] = {
166c103de24SGrant Likely 		[REG_RE]	= STMPE_IDX_GPRER_LSB,
167c103de24SGrant Likely 		[REG_FE]	= STMPE_IDX_GPFER_LSB,
168c103de24SGrant Likely 		[REG_IE]	= STMPE_IDX_IEGPIOR_LSB,
169c103de24SGrant Likely 	};
170c103de24SGrant Likely 	int i, j;
171c103de24SGrant Likely 
172c103de24SGrant Likely 	for (i = 0; i < CACHE_NR_REGS; i++) {
173cccdceb9SViresh Kumar 		/* STMPE801 doesn't have RE and FE registers */
174cccdceb9SViresh Kumar 		if ((stmpe->partnum == STMPE801) &&
175cccdceb9SViresh Kumar 				(i != REG_IE))
176cccdceb9SViresh Kumar 			continue;
177cccdceb9SViresh Kumar 
178c103de24SGrant Likely 		for (j = 0; j < num_banks; j++) {
179c103de24SGrant Likely 			u8 old = stmpe_gpio->oldregs[i][j];
180c103de24SGrant Likely 			u8 new = stmpe_gpio->regs[i][j];
181c103de24SGrant Likely 
182c103de24SGrant Likely 			if (new == old)
183c103de24SGrant Likely 				continue;
184c103de24SGrant Likely 
185c103de24SGrant Likely 			stmpe_gpio->oldregs[i][j] = new;
186c103de24SGrant Likely 			stmpe_reg_write(stmpe, stmpe->regs[regmap[i]] - j, new);
187c103de24SGrant Likely 		}
188c103de24SGrant Likely 	}
189c103de24SGrant Likely 
190c103de24SGrant Likely 	mutex_unlock(&stmpe_gpio->irq_lock);
191c103de24SGrant Likely }
192c103de24SGrant Likely 
193c103de24SGrant Likely static void stmpe_gpio_irq_mask(struct irq_data *d)
194c103de24SGrant Likely {
195fe44e70dSLinus Walleij 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
196fe44e70dSLinus Walleij 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
197fc13d5a5SLee Jones 	int offset = d->hwirq;
198c103de24SGrant Likely 	int regoffset = offset / 8;
199c103de24SGrant Likely 	int mask = 1 << (offset % 8);
200c103de24SGrant Likely 
201c103de24SGrant Likely 	stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
202c103de24SGrant Likely }
203c103de24SGrant Likely 
204c103de24SGrant Likely static void stmpe_gpio_irq_unmask(struct irq_data *d)
205c103de24SGrant Likely {
206fe44e70dSLinus Walleij 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
207fe44e70dSLinus Walleij 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
208fc13d5a5SLee Jones 	int offset = d->hwirq;
209c103de24SGrant Likely 	int regoffset = offset / 8;
210c103de24SGrant Likely 	int mask = 1 << (offset % 8);
211c103de24SGrant Likely 
212c103de24SGrant Likely 	stmpe_gpio->regs[REG_IE][regoffset] |= mask;
213c103de24SGrant Likely }
214c103de24SGrant Likely 
21527ec8a9cSLinus Walleij static void stmpe_dbg_show_one(struct seq_file *s,
21627ec8a9cSLinus Walleij 			       struct gpio_chip *gc,
21727ec8a9cSLinus Walleij 			       unsigned offset, unsigned gpio)
21827ec8a9cSLinus Walleij {
21927ec8a9cSLinus Walleij 	struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
22027ec8a9cSLinus Walleij 	struct stmpe *stmpe = stmpe_gpio->stmpe;
22127ec8a9cSLinus Walleij 	const char *label = gpiochip_is_requested(gc, offset);
22227ec8a9cSLinus Walleij 	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
22327ec8a9cSLinus Walleij 	bool val = !!stmpe_gpio_get(gc, offset);
22427ec8a9cSLinus Walleij 	u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
22527ec8a9cSLinus Walleij 	u8 mask = 1 << (offset % 8);
22627ec8a9cSLinus Walleij 	int ret;
22727ec8a9cSLinus Walleij 	u8 dir;
22827ec8a9cSLinus Walleij 
22927ec8a9cSLinus Walleij 	ret = stmpe_reg_read(stmpe, dir_reg);
23027ec8a9cSLinus Walleij 	if (ret < 0)
23127ec8a9cSLinus Walleij 		return;
23227ec8a9cSLinus Walleij 	dir = !!(ret & mask);
23327ec8a9cSLinus Walleij 
23427ec8a9cSLinus Walleij 	if (dir) {
23527ec8a9cSLinus Walleij 		seq_printf(s, " gpio-%-3d (%-20.20s) out %s",
23627ec8a9cSLinus Walleij 			   gpio, label ?: "(none)",
23727ec8a9cSLinus Walleij 			   val ? "hi" : "lo");
23827ec8a9cSLinus Walleij 	} else {
23927ec8a9cSLinus Walleij 		u8 edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_MSB] + num_banks - 1 - (offset / 8);
24027ec8a9cSLinus Walleij 		u8 rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB] - (offset / 8);
24127ec8a9cSLinus Walleij 		u8 fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB] - (offset / 8);
24227ec8a9cSLinus Walleij 		u8 irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB] - (offset / 8);
24327ec8a9cSLinus Walleij 		bool edge_det;
24427ec8a9cSLinus Walleij 		bool rise;
24527ec8a9cSLinus Walleij 		bool fall;
24627ec8a9cSLinus Walleij 		bool irqen;
24727ec8a9cSLinus Walleij 
24827ec8a9cSLinus Walleij 		ret = stmpe_reg_read(stmpe, edge_det_reg);
24927ec8a9cSLinus Walleij 		if (ret < 0)
25027ec8a9cSLinus Walleij 			return;
25127ec8a9cSLinus Walleij 		edge_det = !!(ret & mask);
25227ec8a9cSLinus Walleij 		ret = stmpe_reg_read(stmpe, rise_reg);
25327ec8a9cSLinus Walleij 		if (ret < 0)
25427ec8a9cSLinus Walleij 			return;
25527ec8a9cSLinus Walleij 		rise = !!(ret & mask);
25627ec8a9cSLinus Walleij 		ret = stmpe_reg_read(stmpe, fall_reg);
25727ec8a9cSLinus Walleij 		if (ret < 0)
25827ec8a9cSLinus Walleij 			return;
25927ec8a9cSLinus Walleij 		fall = !!(ret & mask);
26027ec8a9cSLinus Walleij 		ret = stmpe_reg_read(stmpe, irqen_reg);
26127ec8a9cSLinus Walleij 		if (ret < 0)
26227ec8a9cSLinus Walleij 			return;
26327ec8a9cSLinus Walleij 		irqen = !!(ret & mask);
26427ec8a9cSLinus Walleij 
26527ec8a9cSLinus Walleij 		seq_printf(s, " gpio-%-3d (%-20.20s) in  %s %s %s%s%s",
26627ec8a9cSLinus Walleij 			   gpio, label ?: "(none)",
26727ec8a9cSLinus Walleij 			   val ? "hi" : "lo",
26827ec8a9cSLinus Walleij 			   edge_det ? "edge-asserted" : "edge-inactive",
26927ec8a9cSLinus Walleij 			   irqen ? "IRQ-enabled" : "",
27027ec8a9cSLinus Walleij 			   rise ? " rising-edge-detection" : "",
27127ec8a9cSLinus Walleij 			   fall ? " falling-edge-detection" : "");
27227ec8a9cSLinus Walleij 	}
27327ec8a9cSLinus Walleij }
27427ec8a9cSLinus Walleij 
27527ec8a9cSLinus Walleij static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
27627ec8a9cSLinus Walleij {
27727ec8a9cSLinus Walleij 	unsigned i;
27827ec8a9cSLinus Walleij 	unsigned gpio = gc->base;
27927ec8a9cSLinus Walleij 
28027ec8a9cSLinus Walleij 	for (i = 0; i < gc->ngpio; i++, gpio++) {
28127ec8a9cSLinus Walleij 		stmpe_dbg_show_one(s, gc, i, gpio);
28227ec8a9cSLinus Walleij 		seq_printf(s, "\n");
28327ec8a9cSLinus Walleij 	}
28427ec8a9cSLinus Walleij }
28527ec8a9cSLinus Walleij 
286c103de24SGrant Likely static struct irq_chip stmpe_gpio_irq_chip = {
287c103de24SGrant Likely 	.name			= "stmpe-gpio",
288c103de24SGrant Likely 	.irq_bus_lock		= stmpe_gpio_irq_lock,
289c103de24SGrant Likely 	.irq_bus_sync_unlock	= stmpe_gpio_irq_sync_unlock,
290c103de24SGrant Likely 	.irq_mask		= stmpe_gpio_irq_mask,
291c103de24SGrant Likely 	.irq_unmask		= stmpe_gpio_irq_unmask,
292c103de24SGrant Likely 	.irq_set_type		= stmpe_gpio_irq_set_type,
293c103de24SGrant Likely };
294c103de24SGrant Likely 
295c103de24SGrant Likely static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
296c103de24SGrant Likely {
297c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = dev;
298c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
299c103de24SGrant Likely 	u8 statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
300c103de24SGrant Likely 	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
301c103de24SGrant Likely 	u8 status[num_banks];
302c103de24SGrant Likely 	int ret;
303c103de24SGrant Likely 	int i;
304c103de24SGrant Likely 
305c103de24SGrant Likely 	ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
306c103de24SGrant Likely 	if (ret < 0)
307c103de24SGrant Likely 		return IRQ_NONE;
308c103de24SGrant Likely 
309c103de24SGrant Likely 	for (i = 0; i < num_banks; i++) {
310c103de24SGrant Likely 		int bank = num_banks - i - 1;
311c103de24SGrant Likely 		unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
312c103de24SGrant Likely 		unsigned int stat = status[i];
313c103de24SGrant Likely 
314c103de24SGrant Likely 		stat &= enabled;
315c103de24SGrant Likely 		if (!stat)
316c103de24SGrant Likely 			continue;
317c103de24SGrant Likely 
318c103de24SGrant Likely 		while (stat) {
319c103de24SGrant Likely 			int bit = __ffs(stat);
320c103de24SGrant Likely 			int line = bank * 8 + bit;
321fe44e70dSLinus Walleij 			int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain,
322ed05e204SLinus Walleij 							 line);
323c103de24SGrant Likely 
324ed05e204SLinus Walleij 			handle_nested_irq(child_irq);
325c103de24SGrant Likely 			stat &= ~(1 << bit);
326c103de24SGrant Likely 		}
327c103de24SGrant Likely 
328c103de24SGrant Likely 		stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
329cccdceb9SViresh Kumar 
330cccdceb9SViresh Kumar 		/* Edge detect register is not present on 801 */
331cccdceb9SViresh Kumar 		if (stmpe->partnum != STMPE801)
332cccdceb9SViresh Kumar 			stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_GPEDR_MSB]
333cccdceb9SViresh Kumar 					+ i, status[i]);
334c103de24SGrant Likely 	}
335c103de24SGrant Likely 
336c103de24SGrant Likely 	return IRQ_HANDLED;
337c103de24SGrant Likely }
338c103de24SGrant Likely 
3393836309dSBill Pemberton static int stmpe_gpio_probe(struct platform_device *pdev)
340c103de24SGrant Likely {
341c103de24SGrant Likely 	struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
34286605cfeSVipul Kumar Samar 	struct device_node *np = pdev->dev.of_node;
343c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio;
344c103de24SGrant Likely 	int ret;
34538040c85SChris Blair 	int irq = 0;
346c103de24SGrant Likely 
347c103de24SGrant Likely 	irq = platform_get_irq(pdev, 0);
348c103de24SGrant Likely 
349c103de24SGrant Likely 	stmpe_gpio = kzalloc(sizeof(struct stmpe_gpio), GFP_KERNEL);
350c103de24SGrant Likely 	if (!stmpe_gpio)
351c103de24SGrant Likely 		return -ENOMEM;
352c103de24SGrant Likely 
353c103de24SGrant Likely 	mutex_init(&stmpe_gpio->irq_lock);
354c103de24SGrant Likely 
355c103de24SGrant Likely 	stmpe_gpio->dev = &pdev->dev;
356c103de24SGrant Likely 	stmpe_gpio->stmpe = stmpe;
357c103de24SGrant Likely 	stmpe_gpio->chip = template_chip;
358c103de24SGrant Likely 	stmpe_gpio->chip.ngpio = stmpe->num_gpios;
359*58383c78SLinus Walleij 	stmpe_gpio->chip.parent = &pdev->dev;
3609afd9b70SGabriel Fernandez 	stmpe_gpio->chip.of_node = np;
3619e9dc7d9SLinus Walleij 	stmpe_gpio->chip.base = -1;
362c103de24SGrant Likely 
36327ec8a9cSLinus Walleij 	if (IS_ENABLED(CONFIG_DEBUG_FS))
36427ec8a9cSLinus Walleij                 stmpe_gpio->chip.dbg_show = stmpe_dbg_show;
36527ec8a9cSLinus Walleij 
36686605cfeSVipul Kumar Samar 	of_property_read_u32(np, "st,norequest-mask",
36786605cfeSVipul Kumar Samar 			&stmpe_gpio->norequest_mask);
36886605cfeSVipul Kumar Samar 
3699e9dc7d9SLinus Walleij 	if (irq < 0)
37038040c85SChris Blair 		dev_info(&pdev->dev,
371fe44e70dSLinus Walleij 			"device configured in no-irq mode: "
37238040c85SChris Blair 			"irqs are not available\n");
373c103de24SGrant Likely 
374c103de24SGrant Likely 	ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
375c103de24SGrant Likely 	if (ret)
376c103de24SGrant Likely 		goto out_free;
377c103de24SGrant Likely 
3783f97d5fcSLinus Walleij 	ret = gpiochip_add(&stmpe_gpio->chip);
3793f97d5fcSLinus Walleij 	if (ret) {
3803f97d5fcSLinus Walleij 		dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
3813f97d5fcSLinus Walleij 		goto out_disable;
3823f97d5fcSLinus Walleij 	}
3833f97d5fcSLinus Walleij 
384fe44e70dSLinus Walleij 	if (irq > 0) {
385fe44e70dSLinus Walleij 		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
386fe44e70dSLinus Walleij 				stmpe_gpio_irq, IRQF_ONESHOT,
387fe44e70dSLinus Walleij 				"stmpe-gpio", stmpe_gpio);
388c103de24SGrant Likely 		if (ret) {
389c103de24SGrant Likely 			dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
390fc13d5a5SLee Jones 			goto out_disable;
391c103de24SGrant Likely 		}
392fe44e70dSLinus Walleij 		ret =  gpiochip_irqchip_add(&stmpe_gpio->chip,
393fe44e70dSLinus Walleij 					    &stmpe_gpio_irq_chip,
394fe44e70dSLinus Walleij 					    0,
395fe44e70dSLinus Walleij 					    handle_simple_irq,
396fe44e70dSLinus Walleij 					    IRQ_TYPE_NONE);
397fe44e70dSLinus Walleij 		if (ret) {
398fe44e70dSLinus Walleij 			dev_err(&pdev->dev,
399fe44e70dSLinus Walleij 				"could not connect irqchip to gpiochip\n");
4003f97d5fcSLinus Walleij 			goto out_disable;
40138040c85SChris Blair 		}
402c103de24SGrant Likely 
4033f97d5fcSLinus Walleij 		gpiochip_set_chained_irqchip(&stmpe_gpio->chip,
4043f97d5fcSLinus Walleij 					     &stmpe_gpio_irq_chip,
4053f97d5fcSLinus Walleij 					     irq,
4063f97d5fcSLinus Walleij 					     NULL);
407c103de24SGrant Likely 	}
408c103de24SGrant Likely 
409c103de24SGrant Likely 	platform_set_drvdata(pdev, stmpe_gpio);
410c103de24SGrant Likely 
411c103de24SGrant Likely 	return 0;
412c103de24SGrant Likely 
413c103de24SGrant Likely out_disable:
414c103de24SGrant Likely 	stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
4153f97d5fcSLinus Walleij 	gpiochip_remove(&stmpe_gpio->chip);
416c103de24SGrant Likely out_free:
417c103de24SGrant Likely 	kfree(stmpe_gpio);
418c103de24SGrant Likely 	return ret;
419c103de24SGrant Likely }
420c103de24SGrant Likely 
421206210ceSBill Pemberton static int stmpe_gpio_remove(struct platform_device *pdev)
422c103de24SGrant Likely {
423c103de24SGrant Likely 	struct stmpe_gpio *stmpe_gpio = platform_get_drvdata(pdev);
424c103de24SGrant Likely 	struct stmpe *stmpe = stmpe_gpio->stmpe;
425c103de24SGrant Likely 
4269f5132aeSabdoulaye berthe 	gpiochip_remove(&stmpe_gpio->chip);
427c103de24SGrant Likely 	stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
428c103de24SGrant Likely 	kfree(stmpe_gpio);
429c103de24SGrant Likely 
430c103de24SGrant Likely 	return 0;
431c103de24SGrant Likely }
432c103de24SGrant Likely 
433c103de24SGrant Likely static struct platform_driver stmpe_gpio_driver = {
434c103de24SGrant Likely 	.driver.name	= "stmpe-gpio",
435c103de24SGrant Likely 	.driver.owner	= THIS_MODULE,
436c103de24SGrant Likely 	.probe		= stmpe_gpio_probe,
4378283c4ffSBill Pemberton 	.remove		= stmpe_gpio_remove,
438c103de24SGrant Likely };
439c103de24SGrant Likely 
440c103de24SGrant Likely static int __init stmpe_gpio_init(void)
441c103de24SGrant Likely {
442c103de24SGrant Likely 	return platform_driver_register(&stmpe_gpio_driver);
443c103de24SGrant Likely }
444c103de24SGrant Likely subsys_initcall(stmpe_gpio_init);
445c103de24SGrant Likely 
446c103de24SGrant Likely static void __exit stmpe_gpio_exit(void)
447c103de24SGrant Likely {
448c103de24SGrant Likely 	platform_driver_unregister(&stmpe_gpio_driver);
449c103de24SGrant Likely }
450c103de24SGrant Likely module_exit(stmpe_gpio_exit);
451c103de24SGrant Likely 
452c103de24SGrant Likely MODULE_LICENSE("GPL v2");
453c103de24SGrant Likely MODULE_DESCRIPTION("STMPExxxx GPIO driver");
454c103de24SGrant Likely MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");
455