blob: dd0aedf298fb2b7074b7483e3e75fd3c195056df [file] [log] [blame]
Rabin Vincent03f822f2010-07-02 16:52:09 +05301/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License, version 2
5 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
6 */
7
Rabin Vincent03f822f2010-07-02 16:52:09 +05308#include <linux/init.h>
9#include <linux/platform_device.h>
10#include <linux/slab.h>
11#include <linux/gpio.h>
Rabin Vincent03f822f2010-07-02 16:52:09 +053012#include <linux/interrupt.h>
Vipul Kumar Samar86605cf2012-11-26 17:06:51 +053013#include <linux/of.h>
Rabin Vincent03f822f2010-07-02 16:52:09 +053014#include <linux/mfd/stmpe.h>
Linus Walleij27ec8a92014-10-02 07:55:41 +020015#include <linux/seq_file.h>
Rabin Vincent03f822f2010-07-02 16:52:09 +053016
17/*
18 * These registers are modified under the irq bus lock and cached to avoid
19 * unnecessary writes in bus_sync_unlock.
20 */
21enum { REG_RE, REG_FE, REG_IE };
22
Patrice Chotard43db2892016-08-10 09:39:12 +020023enum { LSB, CSB, MSB };
24
Rabin Vincent03f822f2010-07-02 16:52:09 +053025#define CACHE_NR_REGS 3
Linus Walleij9e9dc7d2014-05-08 23:16:34 +020026/* No variant has more than 24 GPIOs */
27#define CACHE_NR_BANKS (24 / 8)
Rabin Vincent03f822f2010-07-02 16:52:09 +053028
29struct stmpe_gpio {
30 struct gpio_chip chip;
31 struct stmpe *stmpe;
32 struct device *dev;
33 struct mutex irq_lock;
Linus Walleij1dfb4a02015-01-13 08:00:29 +010034 u32 norequest_mask;
Rabin Vincent03f822f2010-07-02 16:52:09 +053035 /* Caches of interrupt control registers for bus_lock */
36 u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
37 u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
38};
39
Rabin Vincent03f822f2010-07-02 16:52:09 +053040static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset)
41{
Linus Walleijb03c04a2015-12-07 14:32:13 +010042 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
Rabin Vincent03f822f2010-07-02 16:52:09 +053043 struct stmpe *stmpe = stmpe_gpio->stmpe;
Patrice Chotard43db2892016-08-10 09:39:12 +020044 u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB + (offset / 8)];
Rabin Vincent03f822f2010-07-02 16:52:09 +053045 u8 mask = 1 << (offset % 8);
46 int ret;
47
48 ret = stmpe_reg_read(stmpe, reg);
49 if (ret < 0)
50 return ret;
51
Bhupesh Sharma7535b8b2012-02-27 11:19:43 +053052 return !!(ret & mask);
Rabin Vincent03f822f2010-07-02 16:52:09 +053053}
54
55static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
56{
Linus Walleijb03c04a2015-12-07 14:32:13 +010057 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
Rabin Vincent03f822f2010-07-02 16:52:09 +053058 struct stmpe *stmpe = stmpe_gpio->stmpe;
59 int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB;
Patrice Chotard43db2892016-08-10 09:39:12 +020060 u8 reg = stmpe->regs[which + (offset / 8)];
Rabin Vincent03f822f2010-07-02 16:52:09 +053061 u8 mask = 1 << (offset % 8);
62
Viresh Kumarcccdceb2011-12-14 09:28:27 +053063 /*
64 * Some variants have single register for gpio set/clear functionality.
65 * For them we need to write 0 to clear and 1 to set.
66 */
67 if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB])
68 stmpe_set_bits(stmpe, reg, mask, val ? mask : 0);
69 else
70 stmpe_reg_write(stmpe, reg, mask);
Rabin Vincent03f822f2010-07-02 16:52:09 +053071}
72
Linus Walleij8e293fb2016-04-28 15:00:18 +020073static int stmpe_gpio_get_direction(struct gpio_chip *chip,
74 unsigned offset)
75{
76 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
77 struct stmpe *stmpe = stmpe_gpio->stmpe;
78 u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
79 u8 mask = 1 << (offset % 8);
80 int ret;
81
82 ret = stmpe_reg_read(stmpe, reg);
83 if (ret < 0)
84 return ret;
85
86 return !(ret & mask);
87}
88
Rabin Vincent03f822f2010-07-02 16:52:09 +053089static int stmpe_gpio_direction_output(struct gpio_chip *chip,
90 unsigned offset, int val)
91{
Linus Walleijb03c04a2015-12-07 14:32:13 +010092 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
Rabin Vincent03f822f2010-07-02 16:52:09 +053093 struct stmpe *stmpe = stmpe_gpio->stmpe;
Patrice Chotard43db2892016-08-10 09:39:12 +020094 u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)];
Rabin Vincent03f822f2010-07-02 16:52:09 +053095 u8 mask = 1 << (offset % 8);
96
97 stmpe_gpio_set(chip, offset, val);
98
99 return stmpe_set_bits(stmpe, reg, mask, mask);
100}
101
102static int stmpe_gpio_direction_input(struct gpio_chip *chip,
103 unsigned offset)
104{
Linus Walleijb03c04a2015-12-07 14:32:13 +0100105 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530106 struct stmpe *stmpe = stmpe_gpio->stmpe;
Patrice Chotard43db2892016-08-10 09:39:12 +0200107 u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)];
Rabin Vincent03f822f2010-07-02 16:52:09 +0530108 u8 mask = 1 << (offset % 8);
109
110 return stmpe_set_bits(stmpe, reg, mask, 0);
111}
112
Rabin Vincent03f822f2010-07-02 16:52:09 +0530113static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset)
114{
Linus Walleijb03c04a2015-12-07 14:32:13 +0100115 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530116 struct stmpe *stmpe = stmpe_gpio->stmpe;
117
Wolfram Sangb8e9cf02010-08-16 17:14:44 +0200118 if (stmpe_gpio->norequest_mask & (1 << offset))
119 return -EINVAL;
120
Rabin Vincent03f822f2010-07-02 16:52:09 +0530121 return stmpe_set_altfunc(stmpe, 1 << offset, STMPE_BLOCK_GPIO);
122}
123
124static struct gpio_chip template_chip = {
125 .label = "stmpe",
126 .owner = THIS_MODULE,
Linus Walleij8e293fb2016-04-28 15:00:18 +0200127 .get_direction = stmpe_gpio_get_direction,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530128 .direction_input = stmpe_gpio_direction_input,
129 .get = stmpe_gpio_get,
130 .direction_output = stmpe_gpio_direction_output,
131 .set = stmpe_gpio_set,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530132 .request = stmpe_gpio_request,
Linus Walleij9fb1f392013-12-04 14:42:46 +0100133 .can_sleep = true,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530134};
135
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800136static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530137{
Linus Walleijfe44e702014-04-15 23:38:56 +0200138 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100139 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Lee Jonesfc13d5a2012-12-10 10:07:54 +0000140 int offset = d->hwirq;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530141 int regoffset = offset / 8;
142 int mask = 1 << (offset % 8);
143
Linus Walleij1fe3bd92014-10-02 07:55:27 +0200144 if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530145 return -EINVAL;
146
Viresh Kumarcccdceb2011-12-14 09:28:27 +0530147 /* STMPE801 doesn't have RE and FE registers */
148 if (stmpe_gpio->stmpe->partnum == STMPE801)
149 return 0;
150
Linus Walleij1fe3bd92014-10-02 07:55:27 +0200151 if (type & IRQ_TYPE_EDGE_RISING)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530152 stmpe_gpio->regs[REG_RE][regoffset] |= mask;
153 else
154 stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;
155
Linus Walleij1fe3bd92014-10-02 07:55:27 +0200156 if (type & IRQ_TYPE_EDGE_FALLING)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530157 stmpe_gpio->regs[REG_FE][regoffset] |= mask;
158 else
159 stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;
160
161 return 0;
162}
163
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800164static void stmpe_gpio_irq_lock(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530165{
Linus Walleijfe44e702014-04-15 23:38:56 +0200166 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100167 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530168
169 mutex_lock(&stmpe_gpio->irq_lock);
170}
171
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800172static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530173{
Linus Walleijfe44e702014-04-15 23:38:56 +0200174 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100175 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530176 struct stmpe *stmpe = stmpe_gpio->stmpe;
177 int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
Patrice Chotard43db2892016-08-10 09:39:12 +0200178 static const u8 regmap[CACHE_NR_REGS][CACHE_NR_BANKS] = {
179 [REG_RE][LSB] = STMPE_IDX_GPRER_LSB,
180 [REG_RE][CSB] = STMPE_IDX_GPRER_CSB,
181 [REG_RE][MSB] = STMPE_IDX_GPRER_MSB,
182 [REG_FE][LSB] = STMPE_IDX_GPFER_LSB,
183 [REG_FE][CSB] = STMPE_IDX_GPFER_CSB,
184 [REG_FE][MSB] = STMPE_IDX_GPFER_MSB,
185 [REG_IE][LSB] = STMPE_IDX_IEGPIOR_LSB,
186 [REG_IE][CSB] = STMPE_IDX_IEGPIOR_CSB,
187 [REG_IE][MSB] = STMPE_IDX_IEGPIOR_MSB,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530188 };
189 int i, j;
190
191 for (i = 0; i < CACHE_NR_REGS; i++) {
Viresh Kumarcccdceb2011-12-14 09:28:27 +0530192 /* STMPE801 doesn't have RE and FE registers */
193 if ((stmpe->partnum == STMPE801) &&
194 (i != REG_IE))
195 continue;
196
Rabin Vincent03f822f2010-07-02 16:52:09 +0530197 for (j = 0; j < num_banks; j++) {
198 u8 old = stmpe_gpio->oldregs[i][j];
199 u8 new = stmpe_gpio->regs[i][j];
200
201 if (new == old)
202 continue;
203
204 stmpe_gpio->oldregs[i][j] = new;
Patrice Chotard43db2892016-08-10 09:39:12 +0200205 stmpe_reg_write(stmpe, stmpe->regs[regmap[i][j]], new);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530206 }
207 }
208
209 mutex_unlock(&stmpe_gpio->irq_lock);
210}
211
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800212static void stmpe_gpio_irq_mask(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530213{
Linus Walleijfe44e702014-04-15 23:38:56 +0200214 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100215 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Lee Jonesfc13d5a2012-12-10 10:07:54 +0000216 int offset = d->hwirq;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530217 int regoffset = offset / 8;
218 int mask = 1 << (offset % 8);
219
220 stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
221}
222
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800223static void stmpe_gpio_irq_unmask(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530224{
Linus Walleijfe44e702014-04-15 23:38:56 +0200225 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100226 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Lee Jonesfc13d5a2012-12-10 10:07:54 +0000227 int offset = d->hwirq;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530228 int regoffset = offset / 8;
229 int mask = 1 << (offset % 8);
230
231 stmpe_gpio->regs[REG_IE][regoffset] |= mask;
232}
233
Linus Walleij27ec8a92014-10-02 07:55:41 +0200234static void stmpe_dbg_show_one(struct seq_file *s,
235 struct gpio_chip *gc,
236 unsigned offset, unsigned gpio)
237{
Linus Walleijb03c04a2015-12-07 14:32:13 +0100238 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Linus Walleij27ec8a92014-10-02 07:55:41 +0200239 struct stmpe *stmpe = stmpe_gpio->stmpe;
240 const char *label = gpiochip_is_requested(gc, offset);
Linus Walleij27ec8a92014-10-02 07:55:41 +0200241 bool val = !!stmpe_gpio_get(gc, offset);
Patrice Chotard43db2892016-08-10 09:39:12 +0200242 u8 bank = offset / 8;
243 u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank];
Linus Walleij27ec8a92014-10-02 07:55:41 +0200244 u8 mask = 1 << (offset % 8);
245 int ret;
246 u8 dir;
247
248 ret = stmpe_reg_read(stmpe, dir_reg);
249 if (ret < 0)
250 return;
251 dir = !!(ret & mask);
252
253 if (dir) {
254 seq_printf(s, " gpio-%-3d (%-20.20s) out %s",
255 gpio, label ?: "(none)",
256 val ? "hi" : "lo");
257 } else {
Patrice Chotard287849c2016-08-10 09:39:08 +0200258 u8 edge_det_reg;
259 u8 rise_reg;
260 u8 fall_reg;
261 u8 irqen_reg;
262
263 char *edge_det_values[] = {"edge-inactive",
264 "edge-asserted",
265 "not-supported"};
266 char *rise_values[] = {"no-rising-edge-detection",
267 "rising-edge-detection",
268 "not-supported"};
269 char *fall_values[] = {"no-falling-edge-detection",
270 "falling-edge-detection",
271 "not-supported"};
272 #define NOT_SUPPORTED_IDX 2
273 u8 edge_det = NOT_SUPPORTED_IDX;
274 u8 rise = NOT_SUPPORTED_IDX;
275 u8 fall = NOT_SUPPORTED_IDX;
Linus Walleij27ec8a92014-10-02 07:55:41 +0200276 bool irqen;
277
Patrice Chotard287849c2016-08-10 09:39:08 +0200278 switch (stmpe->partnum) {
279 case STMPE610:
280 case STMPE811:
281 case STMPE1601:
282 case STMPE2401:
283 case STMPE2403:
Patrice Chotard43db2892016-08-10 09:39:12 +0200284 edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_LSB + bank];
Patrice Chotard287849c2016-08-10 09:39:08 +0200285 ret = stmpe_reg_read(stmpe, edge_det_reg);
286 if (ret < 0)
287 return;
288 edge_det = !!(ret & mask);
289
290 case STMPE1801:
Patrice Chotard43db2892016-08-10 09:39:12 +0200291 rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank];
292 fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank];
293
Patrice Chotard287849c2016-08-10 09:39:08 +0200294 ret = stmpe_reg_read(stmpe, rise_reg);
295 if (ret < 0)
296 return;
297 rise = !!(ret & mask);
298 ret = stmpe_reg_read(stmpe, fall_reg);
299 if (ret < 0)
300 return;
301 fall = !!(ret & mask);
302
303 case STMPE801:
Patrice Chotard43db2892016-08-10 09:39:12 +0200304 irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank];
Patrice Chotard287849c2016-08-10 09:39:08 +0200305 break;
306
307 default:
Linus Walleij27ec8a92014-10-02 07:55:41 +0200308 return;
Patrice Chotard287849c2016-08-10 09:39:08 +0200309 }
310
Linus Walleij27ec8a92014-10-02 07:55:41 +0200311 ret = stmpe_reg_read(stmpe, irqen_reg);
312 if (ret < 0)
313 return;
314 irqen = !!(ret & mask);
315
Patrice Chotard287849c2016-08-10 09:39:08 +0200316 seq_printf(s, " gpio-%-3d (%-20.20s) in %s %13s %13s %25s %25s",
Linus Walleij27ec8a92014-10-02 07:55:41 +0200317 gpio, label ?: "(none)",
318 val ? "hi" : "lo",
Patrice Chotard287849c2016-08-10 09:39:08 +0200319 edge_det_values[edge_det],
320 irqen ? "IRQ-enabled" : "IRQ-disabled",
321 rise_values[rise],
322 fall_values[fall]);
Linus Walleij27ec8a92014-10-02 07:55:41 +0200323 }
324}
325
326static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
327{
328 unsigned i;
329 unsigned gpio = gc->base;
330
331 for (i = 0; i < gc->ngpio; i++, gpio++) {
332 stmpe_dbg_show_one(s, gc, i, gpio);
333 seq_printf(s, "\n");
334 }
335}
336
Rabin Vincent03f822f2010-07-02 16:52:09 +0530337static struct irq_chip stmpe_gpio_irq_chip = {
338 .name = "stmpe-gpio",
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800339 .irq_bus_lock = stmpe_gpio_irq_lock,
340 .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock,
341 .irq_mask = stmpe_gpio_irq_mask,
342 .irq_unmask = stmpe_gpio_irq_unmask,
343 .irq_set_type = stmpe_gpio_irq_set_type,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530344};
345
346static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
347{
348 struct stmpe_gpio *stmpe_gpio = dev;
349 struct stmpe *stmpe = stmpe_gpio->stmpe;
350 u8 statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
351 int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
352 u8 status[num_banks];
353 int ret;
354 int i;
355
356 ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
357 if (ret < 0)
358 return IRQ_NONE;
359
360 for (i = 0; i < num_banks; i++) {
361 int bank = num_banks - i - 1;
362 unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
363 unsigned int stat = status[i];
364
365 stat &= enabled;
366 if (!stat)
367 continue;
368
369 while (stat) {
370 int bit = __ffs(stat);
371 int line = bank * 8 + bit;
Linus Walleijfe44e702014-04-15 23:38:56 +0200372 int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain,
Linus Walleijed05e202013-10-11 19:51:38 +0200373 line);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530374
Linus Walleijed05e202013-10-11 19:51:38 +0200375 handle_nested_irq(child_irq);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530376 stat &= ~(1 << bit);
377 }
378
Patrice Chotard6936e1f2016-08-10 09:39:09 +0200379 /*
380 * interrupt status register write has no effect on
381 * 801 and 1801, bits are cleared when read.
382 * Edge detect register is not present on 801 and 1801
383 */
384 if (stmpe->partnum != STMPE801 || stmpe->partnum != STMPE1801) {
385 stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
Patrice Chotard43db2892016-08-10 09:39:12 +0200386 stmpe_reg_write(stmpe,
387 stmpe->regs[STMPE_IDX_GPEDR_LSB + i],
388 status[i]);
Patrice Chotard6936e1f2016-08-10 09:39:09 +0200389 }
Rabin Vincent03f822f2010-07-02 16:52:09 +0530390 }
391
392 return IRQ_HANDLED;
393}
394
Bill Pemberton38363092012-11-19 13:22:34 -0500395static int stmpe_gpio_probe(struct platform_device *pdev)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530396{
397 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
Vipul Kumar Samar86605cf2012-11-26 17:06:51 +0530398 struct device_node *np = pdev->dev.of_node;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530399 struct stmpe_gpio *stmpe_gpio;
400 int ret;
Chris Blair38040c82012-01-26 22:17:15 +0100401 int irq = 0;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530402
Rabin Vincent03f822f2010-07-02 16:52:09 +0530403 irq = platform_get_irq(pdev, 0);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530404
405 stmpe_gpio = kzalloc(sizeof(struct stmpe_gpio), GFP_KERNEL);
406 if (!stmpe_gpio)
407 return -ENOMEM;
408
409 mutex_init(&stmpe_gpio->irq_lock);
410
411 stmpe_gpio->dev = &pdev->dev;
412 stmpe_gpio->stmpe = stmpe;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530413 stmpe_gpio->chip = template_chip;
414 stmpe_gpio->chip.ngpio = stmpe->num_gpios;
Linus Walleij58383c72015-11-04 09:56:26 +0100415 stmpe_gpio->chip.parent = &pdev->dev;
Gabriel Fernandez9afd9b72013-03-18 11:45:05 +0100416 stmpe_gpio->chip.of_node = np;
Linus Walleij9e9dc7d2014-05-08 23:16:34 +0200417 stmpe_gpio->chip.base = -1;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530418
Linus Walleij27ec8a92014-10-02 07:55:41 +0200419 if (IS_ENABLED(CONFIG_DEBUG_FS))
420 stmpe_gpio->chip.dbg_show = stmpe_dbg_show;
421
Linus Walleij1dfb4a02015-01-13 08:00:29 +0100422 of_property_read_u32(np, "st,norequest-mask",
423 &stmpe_gpio->norequest_mask);
Vipul Kumar Samar86605cf2012-11-26 17:06:51 +0530424
Linus Walleij9e9dc7d2014-05-08 23:16:34 +0200425 if (irq < 0)
Chris Blair38040c82012-01-26 22:17:15 +0100426 dev_info(&pdev->dev,
Linus Walleijfe44e702014-04-15 23:38:56 +0200427 "device configured in no-irq mode: "
Chris Blair38040c82012-01-26 22:17:15 +0100428 "irqs are not available\n");
Rabin Vincent03f822f2010-07-02 16:52:09 +0530429
430 ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
431 if (ret)
Vasiliy Kulikov02bf0742010-09-12 22:57:19 +0400432 goto out_free;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530433
Linus Walleijb03c04a2015-12-07 14:32:13 +0100434 ret = gpiochip_add_data(&stmpe_gpio->chip, stmpe_gpio);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200435 if (ret) {
436 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
437 goto out_disable;
438 }
439
Linus Walleijfe44e702014-04-15 23:38:56 +0200440 if (irq > 0) {
441 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
442 stmpe_gpio_irq, IRQF_ONESHOT,
443 "stmpe-gpio", stmpe_gpio);
Chris Blair38040c82012-01-26 22:17:15 +0100444 if (ret) {
445 dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
Lee Jonesfc13d5a2012-12-10 10:07:54 +0000446 goto out_disable;
Chris Blair38040c82012-01-26 22:17:15 +0100447 }
Linus Walleijfe44e702014-04-15 23:38:56 +0200448 ret = gpiochip_irqchip_add(&stmpe_gpio->chip,
449 &stmpe_gpio_irq_chip,
450 0,
451 handle_simple_irq,
452 IRQ_TYPE_NONE);
453 if (ret) {
454 dev_err(&pdev->dev,
455 "could not connect irqchip to gpiochip\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200456 goto out_disable;
Linus Walleijfe44e702014-04-15 23:38:56 +0200457 }
Rabin Vincent03f822f2010-07-02 16:52:09 +0530458
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200459 gpiochip_set_chained_irqchip(&stmpe_gpio->chip,
460 &stmpe_gpio_irq_chip,
461 irq,
462 NULL);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530463 }
464
Rabin Vincent03f822f2010-07-02 16:52:09 +0530465 platform_set_drvdata(pdev, stmpe_gpio);
466
467 return 0;
468
Vasiliy Kulikov02bf0742010-09-12 22:57:19 +0400469out_disable:
470 stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200471 gpiochip_remove(&stmpe_gpio->chip);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530472out_free:
473 kfree(stmpe_gpio);
474 return ret;
475}
476
Rabin Vincent03f822f2010-07-02 16:52:09 +0530477static struct platform_driver stmpe_gpio_driver = {
Paul Gortmaker3b52bb92016-05-09 19:59:56 -0400478 .driver = {
479 .suppress_bind_attrs = true,
480 .name = "stmpe-gpio",
Paul Gortmaker3b52bb92016-05-09 19:59:56 -0400481 },
Rabin Vincent03f822f2010-07-02 16:52:09 +0530482 .probe = stmpe_gpio_probe,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530483};
484
485static int __init stmpe_gpio_init(void)
486{
487 return platform_driver_register(&stmpe_gpio_driver);
488}
489subsys_initcall(stmpe_gpio_init);