11f67b599SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c103de24SGrant Likely /*
3c103de24SGrant Likely * Copyright (C) ST-Ericsson SA 2010
4c103de24SGrant Likely *
5c103de24SGrant Likely * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
6c103de24SGrant Likely */
7c103de24SGrant Likely
8c103de24SGrant Likely #include <linux/init.h>
9c103de24SGrant Likely #include <linux/platform_device.h>
10c103de24SGrant Likely #include <linux/slab.h>
11ecac6e60SLinus Walleij #include <linux/gpio/driver.h>
12c103de24SGrant Likely #include <linux/interrupt.h>
1386605cfeSVipul Kumar Samar #include <linux/of.h>
14c103de24SGrant Likely #include <linux/mfd/stmpe.h>
1527ec8a9cSLinus Walleij #include <linux/seq_file.h>
1696b2cca6SLinus Walleij #include <linux/bitops.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
2443db289dSPatrice Chotard enum { LSB, CSB, MSB };
2543db289dSPatrice Chotard
26c103de24SGrant Likely #define CACHE_NR_REGS 3
279e9dc7d9SLinus Walleij /* No variant has more than 24 GPIOs */
289e9dc7d9SLinus Walleij #define CACHE_NR_BANKS (24 / 8)
29c103de24SGrant Likely
30c103de24SGrant Likely struct stmpe_gpio {
31c103de24SGrant Likely struct gpio_chip chip;
32c103de24SGrant Likely struct stmpe *stmpe;
33c103de24SGrant Likely struct device *dev;
34c103de24SGrant Likely struct mutex irq_lock;
351dfb4a0dSLinus Walleij u32 norequest_mask;
36c103de24SGrant Likely /* Caches of interrupt control registers for bus_lock */
37c103de24SGrant Likely u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
38c103de24SGrant Likely u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
39c103de24SGrant Likely };
40c103de24SGrant Likely
stmpe_gpio_get(struct gpio_chip * chip,unsigned offset)41c103de24SGrant Likely static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset)
42c103de24SGrant Likely {
43b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
44c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe;
4543db289dSPatrice Chotard u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB + (offset / 8)];
464e2678b5SLinus Walleij u8 mask = BIT(offset % 8);
47c103de24SGrant Likely int ret;
48c103de24SGrant Likely
49c103de24SGrant Likely ret = stmpe_reg_read(stmpe, reg);
50c103de24SGrant Likely if (ret < 0)
51c103de24SGrant Likely return ret;
52c103de24SGrant Likely
537535b8beSBhupesh Sharma return !!(ret & mask);
54c103de24SGrant Likely }
55c103de24SGrant Likely
stmpe_gpio_set(struct gpio_chip * chip,unsigned offset,int val)56c103de24SGrant Likely static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
57c103de24SGrant Likely {
58b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
59c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe;
60c103de24SGrant Likely int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB;
6143db289dSPatrice Chotard u8 reg = stmpe->regs[which + (offset / 8)];
624e2678b5SLinus Walleij u8 mask = BIT(offset % 8);
63c103de24SGrant Likely
64cccdceb9SViresh Kumar /*
65cccdceb9SViresh Kumar * Some variants have single register for gpio set/clear functionality.
66cccdceb9SViresh Kumar * For them we need to write 0 to clear and 1 to set.
67cccdceb9SViresh Kumar */
68cccdceb9SViresh Kumar if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB])
69cccdceb9SViresh Kumar stmpe_set_bits(stmpe, reg, mask, val ? mask : 0);
70cccdceb9SViresh Kumar else
71c103de24SGrant Likely stmpe_reg_write(stmpe, reg, mask);
72c103de24SGrant Likely }
73c103de24SGrant Likely
stmpe_gpio_get_direction(struct gpio_chip * chip,unsigned offset)748e293fb0SLinus Walleij static int stmpe_gpio_get_direction(struct gpio_chip *chip,
758e293fb0SLinus Walleij unsigned offset)
768e293fb0SLinus Walleij {
778e293fb0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
788e293fb0SLinus Walleij struct stmpe *stmpe = stmpe_gpio->stmpe;
798e293fb0SLinus Walleij u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
804e2678b5SLinus Walleij u8 mask = BIT(offset % 8);
818e293fb0SLinus Walleij int ret;
828e293fb0SLinus Walleij
838e293fb0SLinus Walleij ret = stmpe_reg_read(stmpe, reg);
848e293fb0SLinus Walleij if (ret < 0)
858e293fb0SLinus Walleij return ret;
868e293fb0SLinus Walleij
87e42615ecSMatti Vaittinen if (ret & mask)
88e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_OUT;
89e42615ecSMatti Vaittinen
90e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_IN;
918e293fb0SLinus Walleij }
928e293fb0SLinus Walleij
stmpe_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int val)93c103de24SGrant Likely static int stmpe_gpio_direction_output(struct gpio_chip *chip,
94c103de24SGrant Likely unsigned offset, int val)
95c103de24SGrant Likely {
96b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
97c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe;
9843db289dSPatrice Chotard u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)];
994e2678b5SLinus Walleij u8 mask = BIT(offset % 8);
100c103de24SGrant Likely
101c103de24SGrant Likely stmpe_gpio_set(chip, offset, val);
102c103de24SGrant Likely
103c103de24SGrant Likely return stmpe_set_bits(stmpe, reg, mask, mask);
104c103de24SGrant Likely }
105c103de24SGrant Likely
stmpe_gpio_direction_input(struct gpio_chip * chip,unsigned offset)106c103de24SGrant Likely static int stmpe_gpio_direction_input(struct gpio_chip *chip,
107c103de24SGrant Likely unsigned offset)
108c103de24SGrant Likely {
109b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
110c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe;
11143db289dSPatrice Chotard u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)];
1124e2678b5SLinus Walleij u8 mask = BIT(offset % 8);
113c103de24SGrant Likely
114c103de24SGrant Likely return stmpe_set_bits(stmpe, reg, mask, 0);
115c103de24SGrant Likely }
116c103de24SGrant Likely
stmpe_gpio_request(struct gpio_chip * chip,unsigned offset)117c103de24SGrant Likely static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset)
118c103de24SGrant Likely {
119b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
120c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe;
121c103de24SGrant Likely
1224e2678b5SLinus Walleij if (stmpe_gpio->norequest_mask & BIT(offset))
123c103de24SGrant Likely return -EINVAL;
124c103de24SGrant Likely
1254e2678b5SLinus Walleij return stmpe_set_altfunc(stmpe, BIT(offset), STMPE_BLOCK_GPIO);
126c103de24SGrant Likely }
127c103de24SGrant Likely
128e35b5ab0SJulia Lawall static const struct gpio_chip template_chip = {
129c103de24SGrant Likely .label = "stmpe",
130c103de24SGrant Likely .owner = THIS_MODULE,
1318e293fb0SLinus Walleij .get_direction = stmpe_gpio_get_direction,
132c103de24SGrant Likely .direction_input = stmpe_gpio_direction_input,
133c103de24SGrant Likely .get = stmpe_gpio_get,
134c103de24SGrant Likely .direction_output = stmpe_gpio_direction_output,
135c103de24SGrant Likely .set = stmpe_gpio_set,
136c103de24SGrant Likely .request = stmpe_gpio_request,
1379fb1f39eSLinus Walleij .can_sleep = true,
138c103de24SGrant Likely };
139c103de24SGrant Likely
stmpe_gpio_irq_set_type(struct irq_data * d,unsigned int type)140c103de24SGrant Likely static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type)
141c103de24SGrant Likely {
142fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
143b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
144fc13d5a5SLee Jones int offset = d->hwirq;
145c103de24SGrant Likely int regoffset = offset / 8;
1464e2678b5SLinus Walleij int mask = BIT(offset % 8);
147c103de24SGrant Likely
1481fe3bd9eSLinus Walleij if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH)
149c103de24SGrant Likely return -EINVAL;
150c103de24SGrant Likely
151c6a05a05SPatrice Chotard /* STMPE801 and STMPE 1600 don't have RE and FE registers */
152c6a05a05SPatrice Chotard if (stmpe_gpio->stmpe->partnum == STMPE801 ||
153c6a05a05SPatrice Chotard stmpe_gpio->stmpe->partnum == STMPE1600)
154cccdceb9SViresh Kumar return 0;
155cccdceb9SViresh Kumar
1561fe3bd9eSLinus Walleij if (type & IRQ_TYPE_EDGE_RISING)
157c103de24SGrant Likely stmpe_gpio->regs[REG_RE][regoffset] |= mask;
158c103de24SGrant Likely else
159c103de24SGrant Likely stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;
160c103de24SGrant Likely
1611fe3bd9eSLinus Walleij if (type & IRQ_TYPE_EDGE_FALLING)
162c103de24SGrant Likely stmpe_gpio->regs[REG_FE][regoffset] |= mask;
163c103de24SGrant Likely else
164c103de24SGrant Likely stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;
165c103de24SGrant Likely
166c103de24SGrant Likely return 0;
167c103de24SGrant Likely }
168c103de24SGrant Likely
stmpe_gpio_irq_lock(struct irq_data * d)169c103de24SGrant Likely static void stmpe_gpio_irq_lock(struct irq_data *d)
170c103de24SGrant Likely {
171fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
172b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
173c103de24SGrant Likely
174c103de24SGrant Likely mutex_lock(&stmpe_gpio->irq_lock);
175c103de24SGrant Likely }
176c103de24SGrant Likely
stmpe_gpio_irq_sync_unlock(struct irq_data * d)177c103de24SGrant Likely static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
178c103de24SGrant Likely {
179fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
180b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
181c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe;
182c103de24SGrant Likely int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
18343db289dSPatrice Chotard static const u8 regmap[CACHE_NR_REGS][CACHE_NR_BANKS] = {
18443db289dSPatrice Chotard [REG_RE][LSB] = STMPE_IDX_GPRER_LSB,
18543db289dSPatrice Chotard [REG_RE][CSB] = STMPE_IDX_GPRER_CSB,
18643db289dSPatrice Chotard [REG_RE][MSB] = STMPE_IDX_GPRER_MSB,
18743db289dSPatrice Chotard [REG_FE][LSB] = STMPE_IDX_GPFER_LSB,
18843db289dSPatrice Chotard [REG_FE][CSB] = STMPE_IDX_GPFER_CSB,
18943db289dSPatrice Chotard [REG_FE][MSB] = STMPE_IDX_GPFER_MSB,
19043db289dSPatrice Chotard [REG_IE][LSB] = STMPE_IDX_IEGPIOR_LSB,
19143db289dSPatrice Chotard [REG_IE][CSB] = STMPE_IDX_IEGPIOR_CSB,
19243db289dSPatrice Chotard [REG_IE][MSB] = STMPE_IDX_IEGPIOR_MSB,
193c103de24SGrant Likely };
194c103de24SGrant Likely int i, j;
195c103de24SGrant Likely
196b888fb6fSPatrice Chotard /*
197b888fb6fSPatrice Chotard * STMPE1600: to be able to get IRQ from pins,
198b888fb6fSPatrice Chotard * a read must be done on GPMR register, or a write in
199b888fb6fSPatrice Chotard * GPSR or GPCR registers
200b888fb6fSPatrice Chotard */
201b888fb6fSPatrice Chotard if (stmpe->partnum == STMPE1600) {
202b888fb6fSPatrice Chotard stmpe_reg_read(stmpe, stmpe->regs[STMPE_IDX_GPMR_LSB]);
203b888fb6fSPatrice Chotard stmpe_reg_read(stmpe, stmpe->regs[STMPE_IDX_GPMR_CSB]);
204b888fb6fSPatrice Chotard }
205b888fb6fSPatrice Chotard
206c103de24SGrant Likely for (i = 0; i < CACHE_NR_REGS; i++) {
207c6a05a05SPatrice Chotard /* STMPE801 and STMPE1600 don't have RE and FE registers */
208c6a05a05SPatrice Chotard if ((stmpe->partnum == STMPE801 ||
209c6a05a05SPatrice Chotard stmpe->partnum == STMPE1600) &&
210cccdceb9SViresh Kumar (i != REG_IE))
211cccdceb9SViresh Kumar continue;
212cccdceb9SViresh Kumar
213c103de24SGrant Likely for (j = 0; j < num_banks; j++) {
214c103de24SGrant Likely u8 old = stmpe_gpio->oldregs[i][j];
215c103de24SGrant Likely u8 new = stmpe_gpio->regs[i][j];
216c103de24SGrant Likely
217c103de24SGrant Likely if (new == old)
218c103de24SGrant Likely continue;
219c103de24SGrant Likely
220c103de24SGrant Likely stmpe_gpio->oldregs[i][j] = new;
22143db289dSPatrice Chotard stmpe_reg_write(stmpe, stmpe->regs[regmap[i][j]], new);
222c103de24SGrant Likely }
223c103de24SGrant Likely }
224c103de24SGrant Likely
225c103de24SGrant Likely mutex_unlock(&stmpe_gpio->irq_lock);
226c103de24SGrant Likely }
227c103de24SGrant Likely
stmpe_gpio_irq_mask(struct irq_data * d)228c103de24SGrant Likely static void stmpe_gpio_irq_mask(struct irq_data *d)
229c103de24SGrant Likely {
230fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
231b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
232fc13d5a5SLee Jones int offset = d->hwirq;
233c103de24SGrant Likely int regoffset = offset / 8;
2344e2678b5SLinus Walleij int mask = BIT(offset % 8);
235c103de24SGrant Likely
236c103de24SGrant Likely stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
237*32585b56SLinus Walleij gpiochip_disable_irq(gc, offset);
238c103de24SGrant Likely }
239c103de24SGrant Likely
stmpe_gpio_irq_unmask(struct irq_data * d)240c103de24SGrant Likely static void stmpe_gpio_irq_unmask(struct irq_data *d)
241c103de24SGrant Likely {
242fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
243b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
244fc13d5a5SLee Jones int offset = d->hwirq;
245c103de24SGrant Likely int regoffset = offset / 8;
2464e2678b5SLinus Walleij int mask = BIT(offset % 8);
247c103de24SGrant Likely
248*32585b56SLinus Walleij gpiochip_enable_irq(gc, offset);
249c103de24SGrant Likely stmpe_gpio->regs[REG_IE][regoffset] |= mask;
250c103de24SGrant Likely }
251c103de24SGrant Likely
stmpe_dbg_show_one(struct seq_file * s,struct gpio_chip * gc,unsigned offset,unsigned gpio)25227ec8a9cSLinus Walleij static void stmpe_dbg_show_one(struct seq_file *s,
25327ec8a9cSLinus Walleij struct gpio_chip *gc,
25427ec8a9cSLinus Walleij unsigned offset, unsigned gpio)
25527ec8a9cSLinus Walleij {
256b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
25727ec8a9cSLinus Walleij struct stmpe *stmpe = stmpe_gpio->stmpe;
25827ec8a9cSLinus Walleij const char *label = gpiochip_is_requested(gc, offset);
25927ec8a9cSLinus Walleij bool val = !!stmpe_gpio_get(gc, offset);
26043db289dSPatrice Chotard u8 bank = offset / 8;
26143db289dSPatrice Chotard u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank];
2624e2678b5SLinus Walleij u8 mask = BIT(offset % 8);
26327ec8a9cSLinus Walleij int ret;
26427ec8a9cSLinus Walleij u8 dir;
26527ec8a9cSLinus Walleij
26627ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, dir_reg);
26727ec8a9cSLinus Walleij if (ret < 0)
26827ec8a9cSLinus Walleij return;
26927ec8a9cSLinus Walleij dir = !!(ret & mask);
27027ec8a9cSLinus Walleij
27127ec8a9cSLinus Walleij if (dir) {
27227ec8a9cSLinus Walleij seq_printf(s, " gpio-%-3d (%-20.20s) out %s",
27327ec8a9cSLinus Walleij gpio, label ?: "(none)",
27427ec8a9cSLinus Walleij val ? "hi" : "lo");
27527ec8a9cSLinus Walleij } else {
276287849cbSPatrice Chotard u8 edge_det_reg;
277287849cbSPatrice Chotard u8 rise_reg;
278287849cbSPatrice Chotard u8 fall_reg;
279287849cbSPatrice Chotard u8 irqen_reg;
280287849cbSPatrice Chotard
281e2843cb6SColin Ian King static const char * const edge_det_values[] = {
282e2843cb6SColin Ian King "edge-inactive",
283287849cbSPatrice Chotard "edge-asserted",
284e2843cb6SColin Ian King "not-supported"
285e2843cb6SColin Ian King };
286e2843cb6SColin Ian King static const char * const rise_values[] = {
287e2843cb6SColin Ian King "no-rising-edge-detection",
288287849cbSPatrice Chotard "rising-edge-detection",
289e2843cb6SColin Ian King "not-supported"
290e2843cb6SColin Ian King };
291e2843cb6SColin Ian King static const char * const fall_values[] = {
292e2843cb6SColin Ian King "no-falling-edge-detection",
293287849cbSPatrice Chotard "falling-edge-detection",
294e2843cb6SColin Ian King "not-supported"
295e2843cb6SColin Ian King };
296287849cbSPatrice Chotard #define NOT_SUPPORTED_IDX 2
297287849cbSPatrice Chotard u8 edge_det = NOT_SUPPORTED_IDX;
298287849cbSPatrice Chotard u8 rise = NOT_SUPPORTED_IDX;
299287849cbSPatrice Chotard u8 fall = NOT_SUPPORTED_IDX;
30027ec8a9cSLinus Walleij bool irqen;
30127ec8a9cSLinus Walleij
302287849cbSPatrice Chotard switch (stmpe->partnum) {
303287849cbSPatrice Chotard case STMPE610:
304287849cbSPatrice Chotard case STMPE811:
305287849cbSPatrice Chotard case STMPE1601:
306287849cbSPatrice Chotard case STMPE2401:
307287849cbSPatrice Chotard case STMPE2403:
30843db289dSPatrice Chotard edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_LSB + bank];
30927ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, edge_det_reg);
31027ec8a9cSLinus Walleij if (ret < 0)
31127ec8a9cSLinus Walleij return;
31227ec8a9cSLinus Walleij edge_det = !!(ret & mask);
313df561f66SGustavo A. R. Silva fallthrough;
314287849cbSPatrice Chotard case STMPE1801:
31543db289dSPatrice Chotard rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank];
31643db289dSPatrice Chotard fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank];
31743db289dSPatrice Chotard
31827ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, rise_reg);
31927ec8a9cSLinus Walleij if (ret < 0)
32027ec8a9cSLinus Walleij return;
32127ec8a9cSLinus Walleij rise = !!(ret & mask);
32227ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, fall_reg);
32327ec8a9cSLinus Walleij if (ret < 0)
32427ec8a9cSLinus Walleij return;
32527ec8a9cSLinus Walleij fall = !!(ret & mask);
326df561f66SGustavo A. R. Silva fallthrough;
327287849cbSPatrice Chotard case STMPE801:
328c6a05a05SPatrice Chotard case STMPE1600:
32943db289dSPatrice Chotard irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank];
330287849cbSPatrice Chotard break;
331287849cbSPatrice Chotard
332287849cbSPatrice Chotard default:
333287849cbSPatrice Chotard return;
334287849cbSPatrice Chotard }
335287849cbSPatrice Chotard
33627ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, irqen_reg);
33727ec8a9cSLinus Walleij if (ret < 0)
33827ec8a9cSLinus Walleij return;
33927ec8a9cSLinus Walleij irqen = !!(ret & mask);
34027ec8a9cSLinus Walleij
341287849cbSPatrice Chotard seq_printf(s, " gpio-%-3d (%-20.20s) in %s %13s %13s %25s %25s",
34227ec8a9cSLinus Walleij gpio, label ?: "(none)",
34327ec8a9cSLinus Walleij val ? "hi" : "lo",
344287849cbSPatrice Chotard edge_det_values[edge_det],
345287849cbSPatrice Chotard irqen ? "IRQ-enabled" : "IRQ-disabled",
346287849cbSPatrice Chotard rise_values[rise],
347287849cbSPatrice Chotard fall_values[fall]);
34827ec8a9cSLinus Walleij }
34927ec8a9cSLinus Walleij }
35027ec8a9cSLinus Walleij
stmpe_dbg_show(struct seq_file * s,struct gpio_chip * gc)35127ec8a9cSLinus Walleij static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
35227ec8a9cSLinus Walleij {
35327ec8a9cSLinus Walleij unsigned i;
35427ec8a9cSLinus Walleij unsigned gpio = gc->base;
35527ec8a9cSLinus Walleij
35627ec8a9cSLinus Walleij for (i = 0; i < gc->ngpio; i++, gpio++) {
35727ec8a9cSLinus Walleij stmpe_dbg_show_one(s, gc, i, gpio);
3580d83a5ebSMarkus Elfring seq_putc(s, '\n');
35927ec8a9cSLinus Walleij }
36027ec8a9cSLinus Walleij }
36127ec8a9cSLinus Walleij
362*32585b56SLinus Walleij static const struct irq_chip stmpe_gpio_irq_chip = {
363c103de24SGrant Likely .name = "stmpe-gpio",
364c103de24SGrant Likely .irq_bus_lock = stmpe_gpio_irq_lock,
365c103de24SGrant Likely .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock,
366c103de24SGrant Likely .irq_mask = stmpe_gpio_irq_mask,
367c103de24SGrant Likely .irq_unmask = stmpe_gpio_irq_unmask,
368c103de24SGrant Likely .irq_set_type = stmpe_gpio_irq_set_type,
369*32585b56SLinus Walleij .flags = IRQCHIP_IMMUTABLE,
370*32585b56SLinus Walleij GPIOCHIP_IRQ_RESOURCE_HELPERS,
371c103de24SGrant Likely };
372c103de24SGrant Likely
37397fe7befSLaura Abbott #define MAX_GPIOS 24
37497fe7befSLaura Abbott
stmpe_gpio_irq(int irq,void * dev)375c103de24SGrant Likely static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
376c103de24SGrant Likely {
377c103de24SGrant Likely struct stmpe_gpio *stmpe_gpio = dev;
378c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe;
379c6a05a05SPatrice Chotard u8 statmsbreg;
380c103de24SGrant Likely int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
38197fe7befSLaura Abbott u8 status[DIV_ROUND_UP(MAX_GPIOS, 8)];
382c103de24SGrant Likely int ret;
383c103de24SGrant Likely int i;
384c103de24SGrant Likely
385c6a05a05SPatrice Chotard /*
386c6a05a05SPatrice Chotard * the stmpe_block_read() call below, imposes to set statmsbreg
387c6a05a05SPatrice Chotard * with the register located at the lowest address. As STMPE1600
388c6a05a05SPatrice Chotard * variant is the only one which respect registers address's order
389c6a05a05SPatrice Chotard * (LSB regs located at lowest address than MSB ones) whereas all
390c6a05a05SPatrice Chotard * the others have a registers layout with MSB located before the
391c6a05a05SPatrice Chotard * LSB regs.
392c6a05a05SPatrice Chotard */
393c6a05a05SPatrice Chotard if (stmpe->partnum == STMPE1600)
394c6a05a05SPatrice Chotard statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_LSB];
395c6a05a05SPatrice Chotard else
396c6a05a05SPatrice Chotard statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
397c6a05a05SPatrice Chotard
398c103de24SGrant Likely ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
399c103de24SGrant Likely if (ret < 0)
400c103de24SGrant Likely return IRQ_NONE;
401c103de24SGrant Likely
402c103de24SGrant Likely for (i = 0; i < num_banks; i++) {
403c6a05a05SPatrice Chotard int bank = (stmpe_gpio->stmpe->partnum == STMPE1600) ? i :
404c6a05a05SPatrice Chotard num_banks - i - 1;
405c103de24SGrant Likely unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
406c103de24SGrant Likely unsigned int stat = status[i];
407c103de24SGrant Likely
408c103de24SGrant Likely stat &= enabled;
409c103de24SGrant Likely if (!stat)
410c103de24SGrant Likely continue;
411c103de24SGrant Likely
412c103de24SGrant Likely while (stat) {
413c103de24SGrant Likely int bit = __ffs(stat);
414c103de24SGrant Likely int line = bank * 8 + bit;
415f0fbe7bcSThierry Reding int child_irq = irq_find_mapping(stmpe_gpio->chip.irq.domain,
416ed05e204SLinus Walleij line);
417c103de24SGrant Likely
418ed05e204SLinus Walleij handle_nested_irq(child_irq);
4194e2678b5SLinus Walleij stat &= ~BIT(bit);
420c103de24SGrant Likely }
421c103de24SGrant Likely
4226936e1f8SPatrice Chotard /*
4236936e1f8SPatrice Chotard * interrupt status register write has no effect on
424c6a05a05SPatrice Chotard * 801/1801/1600, bits are cleared when read.
425c6a05a05SPatrice Chotard * Edge detect register is not present on 801/1600/1801
4266936e1f8SPatrice Chotard */
427d1ca19cbSDan Carpenter if (stmpe->partnum != STMPE801 && stmpe->partnum != STMPE1600 &&
428c6a05a05SPatrice Chotard stmpe->partnum != STMPE1801) {
429c103de24SGrant Likely stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
43043db289dSPatrice Chotard stmpe_reg_write(stmpe,
4311516c635SLinus Walleij stmpe->regs[STMPE_IDX_GPEDR_MSB] + i,
43243db289dSPatrice Chotard status[i]);
433c103de24SGrant Likely }
4346936e1f8SPatrice Chotard }
435c103de24SGrant Likely
436c103de24SGrant Likely return IRQ_HANDLED;
437c103de24SGrant Likely }
438c103de24SGrant Likely
stmpe_init_irq_valid_mask(struct gpio_chip * gc,unsigned long * valid_mask,unsigned int ngpios)4395fbe5b58SLinus Walleij static void stmpe_init_irq_valid_mask(struct gpio_chip *gc,
4405fbe5b58SLinus Walleij unsigned long *valid_mask,
4415fbe5b58SLinus Walleij unsigned int ngpios)
4425fbe5b58SLinus Walleij {
4435fbe5b58SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
4445fbe5b58SLinus Walleij int i;
4455fbe5b58SLinus Walleij
4465fbe5b58SLinus Walleij if (!stmpe_gpio->norequest_mask)
4475fbe5b58SLinus Walleij return;
4485fbe5b58SLinus Walleij
4495fbe5b58SLinus Walleij /* Forbid unused lines to be mapped as IRQs */
4505fbe5b58SLinus Walleij for (i = 0; i < sizeof(u32); i++) {
4515fbe5b58SLinus Walleij if (stmpe_gpio->norequest_mask & BIT(i))
4525fbe5b58SLinus Walleij clear_bit(i, valid_mask);
4535fbe5b58SLinus Walleij }
4545fbe5b58SLinus Walleij }
4555fbe5b58SLinus Walleij
stmpe_gpio_disable(void * stmpe)4562a9a2ccaSAlexandru Ardelean static void stmpe_gpio_disable(void *stmpe)
4572a9a2ccaSAlexandru Ardelean {
4582a9a2ccaSAlexandru Ardelean stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
4592a9a2ccaSAlexandru Ardelean }
4602a9a2ccaSAlexandru Ardelean
stmpe_gpio_probe(struct platform_device * pdev)4613836309dSBill Pemberton static int stmpe_gpio_probe(struct platform_device *pdev)
462c103de24SGrant Likely {
463c103de24SGrant Likely struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
46486605cfeSVipul Kumar Samar struct device_node *np = pdev->dev.of_node;
465c103de24SGrant Likely struct stmpe_gpio *stmpe_gpio;
4660f719231SMarkus Elfring int ret, irq;
467c103de24SGrant Likely
46897fe7befSLaura Abbott if (stmpe->num_gpios > MAX_GPIOS) {
46997fe7befSLaura Abbott dev_err(&pdev->dev, "Need to increase maximum GPIO number\n");
47097fe7befSLaura Abbott return -EINVAL;
47197fe7befSLaura Abbott }
47297fe7befSLaura Abbott
4732a9a2ccaSAlexandru Ardelean stmpe_gpio = devm_kzalloc(&pdev->dev, sizeof(*stmpe_gpio), GFP_KERNEL);
474c103de24SGrant Likely if (!stmpe_gpio)
475c103de24SGrant Likely return -ENOMEM;
476c103de24SGrant Likely
477c103de24SGrant Likely mutex_init(&stmpe_gpio->irq_lock);
478c103de24SGrant Likely
479c103de24SGrant Likely stmpe_gpio->dev = &pdev->dev;
480c103de24SGrant Likely stmpe_gpio->stmpe = stmpe;
481c103de24SGrant Likely stmpe_gpio->chip = template_chip;
482c103de24SGrant Likely stmpe_gpio->chip.ngpio = stmpe->num_gpios;
48358383c78SLinus Walleij stmpe_gpio->chip.parent = &pdev->dev;
4849e9dc7d9SLinus Walleij stmpe_gpio->chip.base = -1;
485c103de24SGrant Likely
48627ec8a9cSLinus Walleij if (IS_ENABLED(CONFIG_DEBUG_FS))
48727ec8a9cSLinus Walleij stmpe_gpio->chip.dbg_show = stmpe_dbg_show;
48827ec8a9cSLinus Walleij
48986605cfeSVipul Kumar Samar of_property_read_u32(np, "st,norequest-mask",
49086605cfeSVipul Kumar Samar &stmpe_gpio->norequest_mask);
49186605cfeSVipul Kumar Samar
492757ad058SMarkus Elfring irq = platform_get_irq(pdev, 0);
4939e9dc7d9SLinus Walleij if (irq < 0)
49438040c85SChris Blair dev_info(&pdev->dev,
495fe44e70dSLinus Walleij "device configured in no-irq mode: "
49638040c85SChris Blair "irqs are not available\n");
497c103de24SGrant Likely
498c103de24SGrant Likely ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
499c103de24SGrant Likely if (ret)
5002a9a2ccaSAlexandru Ardelean return ret;
5012a9a2ccaSAlexandru Ardelean
5022a9a2ccaSAlexandru Ardelean ret = devm_add_action_or_reset(&pdev->dev, stmpe_gpio_disable, stmpe);
5032a9a2ccaSAlexandru Ardelean if (ret)
5042a9a2ccaSAlexandru Ardelean return ret;
505c103de24SGrant Likely
506fe44e70dSLinus Walleij if (irq > 0) {
50797450796SLinus Walleij struct gpio_irq_chip *girq;
50897450796SLinus Walleij
509fe44e70dSLinus Walleij ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
510fe44e70dSLinus Walleij stmpe_gpio_irq, IRQF_ONESHOT,
511fe44e70dSLinus Walleij "stmpe-gpio", stmpe_gpio);
512c103de24SGrant Likely if (ret) {
513c103de24SGrant Likely dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
5142a9a2ccaSAlexandru Ardelean return ret;
515c103de24SGrant Likely }
516c103de24SGrant Likely
51797450796SLinus Walleij girq = &stmpe_gpio->chip.irq;
518*32585b56SLinus Walleij gpio_irq_chip_set_chip(girq, &stmpe_gpio_irq_chip);
51997450796SLinus Walleij /* This will let us handle the parent IRQ in the driver */
52097450796SLinus Walleij girq->parent_handler = NULL;
52197450796SLinus Walleij girq->num_parents = 0;
52297450796SLinus Walleij girq->parents = NULL;
52397450796SLinus Walleij girq->default_type = IRQ_TYPE_NONE;
52497450796SLinus Walleij girq->handler = handle_simple_irq;
52597450796SLinus Walleij girq->threaded = true;
5268aa16335SLinus Walleij girq->init_valid_mask = stmpe_init_irq_valid_mask;
527c103de24SGrant Likely }
528c103de24SGrant Likely
5292a9a2ccaSAlexandru Ardelean return devm_gpiochip_add_data(&pdev->dev, &stmpe_gpio->chip, stmpe_gpio);
530c103de24SGrant Likely }
531c103de24SGrant Likely
532c103de24SGrant Likely static struct platform_driver stmpe_gpio_driver = {
5333b52bb96SPaul Gortmaker .driver = {
5343b52bb96SPaul Gortmaker .suppress_bind_attrs = true,
5353b52bb96SPaul Gortmaker .name = "stmpe-gpio",
5363b52bb96SPaul Gortmaker },
537c103de24SGrant Likely .probe = stmpe_gpio_probe,
538c103de24SGrant Likely };
539c103de24SGrant Likely
stmpe_gpio_init(void)540c103de24SGrant Likely static int __init stmpe_gpio_init(void)
541c103de24SGrant Likely {
542c103de24SGrant Likely return platform_driver_register(&stmpe_gpio_driver);
543c103de24SGrant Likely }
544c103de24SGrant Likely subsys_initcall(stmpe_gpio_init);
545