blob: 5c0d8189a304351b469b097043c8e224fb1be3b7 [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
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200147 /* STMPE801 and STMPE 1600 don't have RE and FE registers */
148 if (stmpe_gpio->stmpe->partnum == STMPE801 ||
149 stmpe_gpio->stmpe->partnum == STMPE1600)
Viresh Kumarcccdceb2011-12-14 09:28:27 +0530150 return 0;
151
Linus Walleij1fe3bd92014-10-02 07:55:27 +0200152 if (type & IRQ_TYPE_EDGE_RISING)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530153 stmpe_gpio->regs[REG_RE][regoffset] |= mask;
154 else
155 stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;
156
Linus Walleij1fe3bd92014-10-02 07:55:27 +0200157 if (type & IRQ_TYPE_EDGE_FALLING)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530158 stmpe_gpio->regs[REG_FE][regoffset] |= mask;
159 else
160 stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;
161
162 return 0;
163}
164
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800165static void stmpe_gpio_irq_lock(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530166{
Linus Walleijfe44e702014-04-15 23:38:56 +0200167 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100168 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530169
170 mutex_lock(&stmpe_gpio->irq_lock);
171}
172
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800173static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530174{
Linus Walleijfe44e702014-04-15 23:38:56 +0200175 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100176 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530177 struct stmpe *stmpe = stmpe_gpio->stmpe;
178 int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
Patrice Chotard43db2892016-08-10 09:39:12 +0200179 static const u8 regmap[CACHE_NR_REGS][CACHE_NR_BANKS] = {
180 [REG_RE][LSB] = STMPE_IDX_GPRER_LSB,
181 [REG_RE][CSB] = STMPE_IDX_GPRER_CSB,
182 [REG_RE][MSB] = STMPE_IDX_GPRER_MSB,
183 [REG_FE][LSB] = STMPE_IDX_GPFER_LSB,
184 [REG_FE][CSB] = STMPE_IDX_GPFER_CSB,
185 [REG_FE][MSB] = STMPE_IDX_GPFER_MSB,
186 [REG_IE][LSB] = STMPE_IDX_IEGPIOR_LSB,
187 [REG_IE][CSB] = STMPE_IDX_IEGPIOR_CSB,
188 [REG_IE][MSB] = STMPE_IDX_IEGPIOR_MSB,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530189 };
190 int i, j;
191
192 for (i = 0; i < CACHE_NR_REGS; i++) {
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200193 /* STMPE801 and STMPE1600 don't have RE and FE registers */
194 if ((stmpe->partnum == STMPE801 ||
195 stmpe->partnum == STMPE1600) &&
196 (i != REG_IE))
Viresh Kumarcccdceb2011-12-14 09:28:27 +0530197 continue;
198
Rabin Vincent03f822f2010-07-02 16:52:09 +0530199 for (j = 0; j < num_banks; j++) {
200 u8 old = stmpe_gpio->oldregs[i][j];
201 u8 new = stmpe_gpio->regs[i][j];
202
203 if (new == old)
204 continue;
205
206 stmpe_gpio->oldregs[i][j] = new;
Patrice Chotard43db2892016-08-10 09:39:12 +0200207 stmpe_reg_write(stmpe, stmpe->regs[regmap[i][j]], new);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530208 }
209 }
210
211 mutex_unlock(&stmpe_gpio->irq_lock);
212}
213
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800214static void stmpe_gpio_irq_mask(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530215{
Linus Walleijfe44e702014-04-15 23:38:56 +0200216 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100217 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Lee Jonesfc13d5a2012-12-10 10:07:54 +0000218 int offset = d->hwirq;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530219 int regoffset = offset / 8;
220 int mask = 1 << (offset % 8);
221
222 stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
223}
224
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800225static void stmpe_gpio_irq_unmask(struct irq_data *d)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530226{
Linus Walleijfe44e702014-04-15 23:38:56 +0200227 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijb03c04a2015-12-07 14:32:13 +0100228 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200229 struct stmpe *stmpe = stmpe_gpio->stmpe;
Lee Jonesfc13d5a2012-12-10 10:07:54 +0000230 int offset = d->hwirq;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530231 int regoffset = offset / 8;
232 int mask = 1 << (offset % 8);
233
234 stmpe_gpio->regs[REG_IE][regoffset] |= mask;
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200235
236 /*
237 * STMPE1600 workaround: to be able to get IRQ from pins,
238 * a read must be done on GPMR register, or a write in
239 * GPSR or GPCR registers
240 */
241 if (stmpe->partnum == STMPE1600)
242 stmpe_reg_read(stmpe,
243 stmpe->regs[STMPE_IDX_GPMR_LSB + regoffset]);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530244}
245
Linus Walleij27ec8a92014-10-02 07:55:41 +0200246static void stmpe_dbg_show_one(struct seq_file *s,
247 struct gpio_chip *gc,
248 unsigned offset, unsigned gpio)
249{
Linus Walleijb03c04a2015-12-07 14:32:13 +0100250 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
Linus Walleij27ec8a92014-10-02 07:55:41 +0200251 struct stmpe *stmpe = stmpe_gpio->stmpe;
252 const char *label = gpiochip_is_requested(gc, offset);
Linus Walleij27ec8a92014-10-02 07:55:41 +0200253 bool val = !!stmpe_gpio_get(gc, offset);
Patrice Chotard43db2892016-08-10 09:39:12 +0200254 u8 bank = offset / 8;
255 u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank];
Linus Walleij27ec8a92014-10-02 07:55:41 +0200256 u8 mask = 1 << (offset % 8);
257 int ret;
258 u8 dir;
259
260 ret = stmpe_reg_read(stmpe, dir_reg);
261 if (ret < 0)
262 return;
263 dir = !!(ret & mask);
264
265 if (dir) {
266 seq_printf(s, " gpio-%-3d (%-20.20s) out %s",
267 gpio, label ?: "(none)",
268 val ? "hi" : "lo");
269 } else {
Patrice Chotard287849c2016-08-10 09:39:08 +0200270 u8 edge_det_reg;
271 u8 rise_reg;
272 u8 fall_reg;
273 u8 irqen_reg;
274
275 char *edge_det_values[] = {"edge-inactive",
276 "edge-asserted",
277 "not-supported"};
278 char *rise_values[] = {"no-rising-edge-detection",
279 "rising-edge-detection",
280 "not-supported"};
281 char *fall_values[] = {"no-falling-edge-detection",
282 "falling-edge-detection",
283 "not-supported"};
284 #define NOT_SUPPORTED_IDX 2
285 u8 edge_det = NOT_SUPPORTED_IDX;
286 u8 rise = NOT_SUPPORTED_IDX;
287 u8 fall = NOT_SUPPORTED_IDX;
Linus Walleij27ec8a92014-10-02 07:55:41 +0200288 bool irqen;
289
Patrice Chotard287849c2016-08-10 09:39:08 +0200290 switch (stmpe->partnum) {
291 case STMPE610:
292 case STMPE811:
293 case STMPE1601:
294 case STMPE2401:
295 case STMPE2403:
Patrice Chotard43db2892016-08-10 09:39:12 +0200296 edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_LSB + bank];
Patrice Chotard287849c2016-08-10 09:39:08 +0200297 ret = stmpe_reg_read(stmpe, edge_det_reg);
298 if (ret < 0)
299 return;
300 edge_det = !!(ret & mask);
301
302 case STMPE1801:
Patrice Chotard43db2892016-08-10 09:39:12 +0200303 rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank];
304 fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank];
305
Patrice Chotard287849c2016-08-10 09:39:08 +0200306 ret = stmpe_reg_read(stmpe, rise_reg);
307 if (ret < 0)
308 return;
309 rise = !!(ret & mask);
310 ret = stmpe_reg_read(stmpe, fall_reg);
311 if (ret < 0)
312 return;
313 fall = !!(ret & mask);
314
315 case STMPE801:
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200316 case STMPE1600:
Patrice Chotard43db2892016-08-10 09:39:12 +0200317 irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank];
Patrice Chotard287849c2016-08-10 09:39:08 +0200318 break;
319
320 default:
Linus Walleij27ec8a92014-10-02 07:55:41 +0200321 return;
Patrice Chotard287849c2016-08-10 09:39:08 +0200322 }
323
Linus Walleij27ec8a92014-10-02 07:55:41 +0200324 ret = stmpe_reg_read(stmpe, irqen_reg);
325 if (ret < 0)
326 return;
327 irqen = !!(ret & mask);
328
Patrice Chotard287849c2016-08-10 09:39:08 +0200329 seq_printf(s, " gpio-%-3d (%-20.20s) in %s %13s %13s %25s %25s",
Linus Walleij27ec8a92014-10-02 07:55:41 +0200330 gpio, label ?: "(none)",
331 val ? "hi" : "lo",
Patrice Chotard287849c2016-08-10 09:39:08 +0200332 edge_det_values[edge_det],
333 irqen ? "IRQ-enabled" : "IRQ-disabled",
334 rise_values[rise],
335 fall_values[fall]);
Linus Walleij27ec8a92014-10-02 07:55:41 +0200336 }
337}
338
339static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
340{
341 unsigned i;
342 unsigned gpio = gc->base;
343
344 for (i = 0; i < gc->ngpio; i++, gpio++) {
345 stmpe_dbg_show_one(s, gc, i, gpio);
346 seq_printf(s, "\n");
347 }
348}
349
Rabin Vincent03f822f2010-07-02 16:52:09 +0530350static struct irq_chip stmpe_gpio_irq_chip = {
351 .name = "stmpe-gpio",
Lennert Buytenhek2a866f32011-01-12 17:00:17 -0800352 .irq_bus_lock = stmpe_gpio_irq_lock,
353 .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock,
354 .irq_mask = stmpe_gpio_irq_mask,
355 .irq_unmask = stmpe_gpio_irq_unmask,
356 .irq_set_type = stmpe_gpio_irq_set_type,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530357};
358
359static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
360{
361 struct stmpe_gpio *stmpe_gpio = dev;
362 struct stmpe *stmpe = stmpe_gpio->stmpe;
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200363 u8 statmsbreg;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530364 int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
365 u8 status[num_banks];
366 int ret;
367 int i;
368
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200369 /*
370 * the stmpe_block_read() call below, imposes to set statmsbreg
371 * with the register located at the lowest address. As STMPE1600
372 * variant is the only one which respect registers address's order
373 * (LSB regs located at lowest address than MSB ones) whereas all
374 * the others have a registers layout with MSB located before the
375 * LSB regs.
376 */
377 if (stmpe->partnum == STMPE1600)
378 statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_LSB];
379 else
380 statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
381
Rabin Vincent03f822f2010-07-02 16:52:09 +0530382 ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
383 if (ret < 0)
384 return IRQ_NONE;
385
386 for (i = 0; i < num_banks; i++) {
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200387 int bank = (stmpe_gpio->stmpe->partnum == STMPE1600) ? i :
388 num_banks - i - 1;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530389 unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
390 unsigned int stat = status[i];
391
392 stat &= enabled;
393 if (!stat)
394 continue;
395
396 while (stat) {
397 int bit = __ffs(stat);
398 int line = bank * 8 + bit;
Linus Walleijfe44e702014-04-15 23:38:56 +0200399 int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain,
Linus Walleijed05e202013-10-11 19:51:38 +0200400 line);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530401
Linus Walleijed05e202013-10-11 19:51:38 +0200402 handle_nested_irq(child_irq);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530403 stat &= ~(1 << bit);
404 }
405
Patrice Chotard6936e1f2016-08-10 09:39:09 +0200406 /*
407 * interrupt status register write has no effect on
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200408 * 801/1801/1600, bits are cleared when read.
409 * Edge detect register is not present on 801/1600/1801
Patrice Chotard6936e1f2016-08-10 09:39:09 +0200410 */
Patrice Chotardc6a05a02016-08-10 09:39:15 +0200411 if (stmpe->partnum != STMPE801 || stmpe->partnum != STMPE1600 ||
412 stmpe->partnum != STMPE1801) {
Patrice Chotard6936e1f2016-08-10 09:39:09 +0200413 stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
Patrice Chotard43db2892016-08-10 09:39:12 +0200414 stmpe_reg_write(stmpe,
415 stmpe->regs[STMPE_IDX_GPEDR_LSB + i],
416 status[i]);
Patrice Chotard6936e1f2016-08-10 09:39:09 +0200417 }
Rabin Vincent03f822f2010-07-02 16:52:09 +0530418 }
419
420 return IRQ_HANDLED;
421}
422
Bill Pemberton38363092012-11-19 13:22:34 -0500423static int stmpe_gpio_probe(struct platform_device *pdev)
Rabin Vincent03f822f2010-07-02 16:52:09 +0530424{
425 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
Vipul Kumar Samar86605cf2012-11-26 17:06:51 +0530426 struct device_node *np = pdev->dev.of_node;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530427 struct stmpe_gpio *stmpe_gpio;
428 int ret;
Chris Blair38040c82012-01-26 22:17:15 +0100429 int irq = 0;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530430
Rabin Vincent03f822f2010-07-02 16:52:09 +0530431 irq = platform_get_irq(pdev, 0);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530432
433 stmpe_gpio = kzalloc(sizeof(struct stmpe_gpio), GFP_KERNEL);
434 if (!stmpe_gpio)
435 return -ENOMEM;
436
437 mutex_init(&stmpe_gpio->irq_lock);
438
439 stmpe_gpio->dev = &pdev->dev;
440 stmpe_gpio->stmpe = stmpe;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530441 stmpe_gpio->chip = template_chip;
442 stmpe_gpio->chip.ngpio = stmpe->num_gpios;
Linus Walleij58383c782015-11-04 09:56:26 +0100443 stmpe_gpio->chip.parent = &pdev->dev;
Gabriel Fernandez9afd9b72013-03-18 11:45:05 +0100444 stmpe_gpio->chip.of_node = np;
Linus Walleij9e9dc7d2014-05-08 23:16:34 +0200445 stmpe_gpio->chip.base = -1;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530446
Linus Walleij27ec8a92014-10-02 07:55:41 +0200447 if (IS_ENABLED(CONFIG_DEBUG_FS))
448 stmpe_gpio->chip.dbg_show = stmpe_dbg_show;
449
Linus Walleij1dfb4a02015-01-13 08:00:29 +0100450 of_property_read_u32(np, "st,norequest-mask",
451 &stmpe_gpio->norequest_mask);
Vipul Kumar Samar86605cf2012-11-26 17:06:51 +0530452
Linus Walleij9e9dc7d2014-05-08 23:16:34 +0200453 if (irq < 0)
Chris Blair38040c82012-01-26 22:17:15 +0100454 dev_info(&pdev->dev,
Linus Walleijfe44e702014-04-15 23:38:56 +0200455 "device configured in no-irq mode: "
Chris Blair38040c82012-01-26 22:17:15 +0100456 "irqs are not available\n");
Rabin Vincent03f822f2010-07-02 16:52:09 +0530457
458 ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
459 if (ret)
Vasiliy Kulikov02bf0742010-09-12 22:57:19 +0400460 goto out_free;
Rabin Vincent03f822f2010-07-02 16:52:09 +0530461
Linus Walleijb03c04a2015-12-07 14:32:13 +0100462 ret = gpiochip_add_data(&stmpe_gpio->chip, stmpe_gpio);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200463 if (ret) {
464 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
465 goto out_disable;
466 }
467
Linus Walleijfe44e702014-04-15 23:38:56 +0200468 if (irq > 0) {
469 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
470 stmpe_gpio_irq, IRQF_ONESHOT,
471 "stmpe-gpio", stmpe_gpio);
Chris Blair38040c82012-01-26 22:17:15 +0100472 if (ret) {
473 dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
Lee Jonesfc13d5a2012-12-10 10:07:54 +0000474 goto out_disable;
Chris Blair38040c82012-01-26 22:17:15 +0100475 }
Linus Walleijfe44e702014-04-15 23:38:56 +0200476 ret = gpiochip_irqchip_add(&stmpe_gpio->chip,
477 &stmpe_gpio_irq_chip,
478 0,
479 handle_simple_irq,
480 IRQ_TYPE_NONE);
481 if (ret) {
482 dev_err(&pdev->dev,
483 "could not connect irqchip to gpiochip\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200484 goto out_disable;
Linus Walleijfe44e702014-04-15 23:38:56 +0200485 }
Rabin Vincent03f822f2010-07-02 16:52:09 +0530486
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200487 gpiochip_set_chained_irqchip(&stmpe_gpio->chip,
488 &stmpe_gpio_irq_chip,
489 irq,
490 NULL);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530491 }
492
Rabin Vincent03f822f2010-07-02 16:52:09 +0530493 platform_set_drvdata(pdev, stmpe_gpio);
494
495 return 0;
496
Vasiliy Kulikov02bf0742010-09-12 22:57:19 +0400497out_disable:
498 stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200499 gpiochip_remove(&stmpe_gpio->chip);
Rabin Vincent03f822f2010-07-02 16:52:09 +0530500out_free:
501 kfree(stmpe_gpio);
502 return ret;
503}
504
Rabin Vincent03f822f2010-07-02 16:52:09 +0530505static struct platform_driver stmpe_gpio_driver = {
Paul Gortmaker3b52bb92016-05-09 19:59:56 -0400506 .driver = {
507 .suppress_bind_attrs = true,
508 .name = "stmpe-gpio",
Paul Gortmaker3b52bb92016-05-09 19:59:56 -0400509 },
Rabin Vincent03f822f2010-07-02 16:52:09 +0530510 .probe = stmpe_gpio_probe,
Rabin Vincent03f822f2010-07-02 16:52:09 +0530511};
512
513static int __init stmpe_gpio_init(void)
514{
515 return platform_driver_register(&stmpe_gpio_driver);
516}
517subsys_initcall(stmpe_gpio_init);