Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 1 | /* |
| 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/platform_device.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/gpio.h> |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Vipul Kumar Samar | 86605cf | 2012-11-26 17:06:51 +0530 | [diff] [blame] | 13 | #include <linux/of.h> |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 14 | #include <linux/mfd/stmpe.h> |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 15 | #include <linux/seq_file.h> |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * These registers are modified under the irq bus lock and cached to avoid |
| 19 | * unnecessary writes in bus_sync_unlock. |
| 20 | */ |
| 21 | enum { REG_RE, REG_FE, REG_IE }; |
| 22 | |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 23 | enum { LSB, CSB, MSB }; |
| 24 | |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 25 | #define CACHE_NR_REGS 3 |
Linus Walleij | 9e9dc7d | 2014-05-08 23:16:34 +0200 | [diff] [blame] | 26 | /* No variant has more than 24 GPIOs */ |
| 27 | #define CACHE_NR_BANKS (24 / 8) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 28 | |
| 29 | struct stmpe_gpio { |
| 30 | struct gpio_chip chip; |
| 31 | struct stmpe *stmpe; |
| 32 | struct device *dev; |
| 33 | struct mutex irq_lock; |
Linus Walleij | 1dfb4a0 | 2015-01-13 08:00:29 +0100 | [diff] [blame] | 34 | u32 norequest_mask; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 35 | /* 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 40 | static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 41 | { |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 42 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 43 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 44 | u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB + (offset / 8)]; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 45 | 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 Sharma | 7535b8b | 2012-02-27 11:19:43 +0530 | [diff] [blame] | 52 | return !!(ret & mask); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val) |
| 56 | { |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 57 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 58 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
| 59 | int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB; |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 60 | u8 reg = stmpe->regs[which + (offset / 8)]; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 61 | u8 mask = 1 << (offset % 8); |
| 62 | |
Viresh Kumar | cccdceb | 2011-12-14 09:28:27 +0530 | [diff] [blame] | 63 | /* |
| 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 71 | } |
| 72 | |
Linus Walleij | 8e293fb | 2016-04-28 15:00:18 +0200 | [diff] [blame] | 73 | static 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 89 | static int stmpe_gpio_direction_output(struct gpio_chip *chip, |
| 90 | unsigned offset, int val) |
| 91 | { |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 92 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 93 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 94 | u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)]; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 95 | 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 | |
| 102 | static int stmpe_gpio_direction_input(struct gpio_chip *chip, |
| 103 | unsigned offset) |
| 104 | { |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 105 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 106 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 107 | u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)]; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 108 | u8 mask = 1 << (offset % 8); |
| 109 | |
| 110 | return stmpe_set_bits(stmpe, reg, mask, 0); |
| 111 | } |
| 112 | |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 113 | static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 114 | { |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 115 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 116 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
| 117 | |
Wolfram Sang | b8e9cf0 | 2010-08-16 17:14:44 +0200 | [diff] [blame] | 118 | if (stmpe_gpio->norequest_mask & (1 << offset)) |
| 119 | return -EINVAL; |
| 120 | |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 121 | return stmpe_set_altfunc(stmpe, 1 << offset, STMPE_BLOCK_GPIO); |
| 122 | } |
| 123 | |
| 124 | static struct gpio_chip template_chip = { |
| 125 | .label = "stmpe", |
| 126 | .owner = THIS_MODULE, |
Linus Walleij | 8e293fb | 2016-04-28 15:00:18 +0200 | [diff] [blame] | 127 | .get_direction = stmpe_gpio_get_direction, |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 128 | .direction_input = stmpe_gpio_direction_input, |
| 129 | .get = stmpe_gpio_get, |
| 130 | .direction_output = stmpe_gpio_direction_output, |
| 131 | .set = stmpe_gpio_set, |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 132 | .request = stmpe_gpio_request, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 133 | .can_sleep = true, |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 134 | }; |
| 135 | |
Lennert Buytenhek | 2a866f3 | 2011-01-12 17:00:17 -0800 | [diff] [blame] | 136 | static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 137 | { |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 138 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 139 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); |
Lee Jones | fc13d5a | 2012-12-10 10:07:54 +0000 | [diff] [blame] | 140 | int offset = d->hwirq; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 141 | int regoffset = offset / 8; |
| 142 | int mask = 1 << (offset % 8); |
| 143 | |
Linus Walleij | 1fe3bd9 | 2014-10-02 07:55:27 +0200 | [diff] [blame] | 144 | if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 145 | return -EINVAL; |
| 146 | |
Patrice Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 147 | /* 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 Kumar | cccdceb | 2011-12-14 09:28:27 +0530 | [diff] [blame] | 150 | return 0; |
| 151 | |
Linus Walleij | 1fe3bd9 | 2014-10-02 07:55:27 +0200 | [diff] [blame] | 152 | if (type & IRQ_TYPE_EDGE_RISING) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 153 | stmpe_gpio->regs[REG_RE][regoffset] |= mask; |
| 154 | else |
| 155 | stmpe_gpio->regs[REG_RE][regoffset] &= ~mask; |
| 156 | |
Linus Walleij | 1fe3bd9 | 2014-10-02 07:55:27 +0200 | [diff] [blame] | 157 | if (type & IRQ_TYPE_EDGE_FALLING) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 158 | stmpe_gpio->regs[REG_FE][regoffset] |= mask; |
| 159 | else |
| 160 | stmpe_gpio->regs[REG_FE][regoffset] &= ~mask; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Lennert Buytenhek | 2a866f3 | 2011-01-12 17:00:17 -0800 | [diff] [blame] | 165 | static void stmpe_gpio_irq_lock(struct irq_data *d) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 166 | { |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 167 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 168 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 169 | |
| 170 | mutex_lock(&stmpe_gpio->irq_lock); |
| 171 | } |
| 172 | |
Lennert Buytenhek | 2a866f3 | 2011-01-12 17:00:17 -0800 | [diff] [blame] | 173 | static void stmpe_gpio_irq_sync_unlock(struct irq_data *d) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 174 | { |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 175 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 176 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 177 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
| 178 | int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8); |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 179 | 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 189 | }; |
| 190 | int i, j; |
| 191 | |
| 192 | for (i = 0; i < CACHE_NR_REGS; i++) { |
Patrice Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 193 | /* STMPE801 and STMPE1600 don't have RE and FE registers */ |
| 194 | if ((stmpe->partnum == STMPE801 || |
| 195 | stmpe->partnum == STMPE1600) && |
| 196 | (i != REG_IE)) |
Viresh Kumar | cccdceb | 2011-12-14 09:28:27 +0530 | [diff] [blame] | 197 | continue; |
| 198 | |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 199 | 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 Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 207 | stmpe_reg_write(stmpe, stmpe->regs[regmap[i][j]], new); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | mutex_unlock(&stmpe_gpio->irq_lock); |
| 212 | } |
| 213 | |
Lennert Buytenhek | 2a866f3 | 2011-01-12 17:00:17 -0800 | [diff] [blame] | 214 | static void stmpe_gpio_irq_mask(struct irq_data *d) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 215 | { |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 216 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 217 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); |
Lee Jones | fc13d5a | 2012-12-10 10:07:54 +0000 | [diff] [blame] | 218 | int offset = d->hwirq; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 219 | int regoffset = offset / 8; |
| 220 | int mask = 1 << (offset % 8); |
| 221 | |
| 222 | stmpe_gpio->regs[REG_IE][regoffset] &= ~mask; |
| 223 | } |
| 224 | |
Lennert Buytenhek | 2a866f3 | 2011-01-12 17:00:17 -0800 | [diff] [blame] | 225 | static void stmpe_gpio_irq_unmask(struct irq_data *d) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 226 | { |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 227 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 228 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); |
Patrice Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 229 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
Lee Jones | fc13d5a | 2012-12-10 10:07:54 +0000 | [diff] [blame] | 230 | int offset = d->hwirq; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 231 | int regoffset = offset / 8; |
| 232 | int mask = 1 << (offset % 8); |
| 233 | |
| 234 | stmpe_gpio->regs[REG_IE][regoffset] |= mask; |
Patrice Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 235 | |
| 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 244 | } |
| 245 | |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 246 | static void stmpe_dbg_show_one(struct seq_file *s, |
| 247 | struct gpio_chip *gc, |
| 248 | unsigned offset, unsigned gpio) |
| 249 | { |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 250 | struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 251 | struct stmpe *stmpe = stmpe_gpio->stmpe; |
| 252 | const char *label = gpiochip_is_requested(gc, offset); |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 253 | bool val = !!stmpe_gpio_get(gc, offset); |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 254 | u8 bank = offset / 8; |
| 255 | u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank]; |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 256 | 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 Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 270 | 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 Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 288 | bool irqen; |
| 289 | |
Patrice Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 290 | switch (stmpe->partnum) { |
| 291 | case STMPE610: |
| 292 | case STMPE811: |
| 293 | case STMPE1601: |
| 294 | case STMPE2401: |
| 295 | case STMPE2403: |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 296 | edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_LSB + bank]; |
Patrice Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 297 | ret = stmpe_reg_read(stmpe, edge_det_reg); |
| 298 | if (ret < 0) |
| 299 | return; |
| 300 | edge_det = !!(ret & mask); |
| 301 | |
| 302 | case STMPE1801: |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 303 | rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank]; |
| 304 | fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank]; |
| 305 | |
Patrice Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 306 | 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 Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 316 | case STMPE1600: |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 317 | irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank]; |
Patrice Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 318 | break; |
| 319 | |
| 320 | default: |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 321 | return; |
Patrice Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 324 | ret = stmpe_reg_read(stmpe, irqen_reg); |
| 325 | if (ret < 0) |
| 326 | return; |
| 327 | irqen = !!(ret & mask); |
| 328 | |
Patrice Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 329 | seq_printf(s, " gpio-%-3d (%-20.20s) in %s %13s %13s %25s %25s", |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 330 | gpio, label ?: "(none)", |
| 331 | val ? "hi" : "lo", |
Patrice Chotard | 287849c | 2016-08-10 09:39:08 +0200 | [diff] [blame] | 332 | edge_det_values[edge_det], |
| 333 | irqen ? "IRQ-enabled" : "IRQ-disabled", |
| 334 | rise_values[rise], |
| 335 | fall_values[fall]); |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | static 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 350 | static struct irq_chip stmpe_gpio_irq_chip = { |
| 351 | .name = "stmpe-gpio", |
Lennert Buytenhek | 2a866f3 | 2011-01-12 17:00:17 -0800 | [diff] [blame] | 352 | .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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | static 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 Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 363 | u8 statmsbreg; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 364 | int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8); |
| 365 | u8 status[num_banks]; |
| 366 | int ret; |
| 367 | int i; |
| 368 | |
Patrice Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 369 | /* |
| 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 382 | 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 Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 387 | int bank = (stmpe_gpio->stmpe->partnum == STMPE1600) ? i : |
| 388 | num_banks - i - 1; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 389 | 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 Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 399 | int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain, |
Linus Walleij | ed05e20 | 2013-10-11 19:51:38 +0200 | [diff] [blame] | 400 | line); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 401 | |
Linus Walleij | ed05e20 | 2013-10-11 19:51:38 +0200 | [diff] [blame] | 402 | handle_nested_irq(child_irq); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 403 | stat &= ~(1 << bit); |
| 404 | } |
| 405 | |
Patrice Chotard | 6936e1f | 2016-08-10 09:39:09 +0200 | [diff] [blame] | 406 | /* |
| 407 | * interrupt status register write has no effect on |
Patrice Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 408 | * 801/1801/1600, bits are cleared when read. |
| 409 | * Edge detect register is not present on 801/1600/1801 |
Patrice Chotard | 6936e1f | 2016-08-10 09:39:09 +0200 | [diff] [blame] | 410 | */ |
Patrice Chotard | c6a05a0 | 2016-08-10 09:39:15 +0200 | [diff] [blame] | 411 | if (stmpe->partnum != STMPE801 || stmpe->partnum != STMPE1600 || |
| 412 | stmpe->partnum != STMPE1801) { |
Patrice Chotard | 6936e1f | 2016-08-10 09:39:09 +0200 | [diff] [blame] | 413 | stmpe_reg_write(stmpe, statmsbreg + i, status[i]); |
Patrice Chotard | 43db289 | 2016-08-10 09:39:12 +0200 | [diff] [blame] | 414 | stmpe_reg_write(stmpe, |
| 415 | stmpe->regs[STMPE_IDX_GPEDR_LSB + i], |
| 416 | status[i]); |
Patrice Chotard | 6936e1f | 2016-08-10 09:39:09 +0200 | [diff] [blame] | 417 | } |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return IRQ_HANDLED; |
| 421 | } |
| 422 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 423 | static int stmpe_gpio_probe(struct platform_device *pdev) |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 424 | { |
| 425 | struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent); |
Vipul Kumar Samar | 86605cf | 2012-11-26 17:06:51 +0530 | [diff] [blame] | 426 | struct device_node *np = pdev->dev.of_node; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 427 | struct stmpe_gpio *stmpe_gpio; |
| 428 | int ret; |
Chris Blair | 38040c8 | 2012-01-26 22:17:15 +0100 | [diff] [blame] | 429 | int irq = 0; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 430 | |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 431 | irq = platform_get_irq(pdev, 0); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 432 | |
| 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 Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 441 | stmpe_gpio->chip = template_chip; |
| 442 | stmpe_gpio->chip.ngpio = stmpe->num_gpios; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 443 | stmpe_gpio->chip.parent = &pdev->dev; |
Gabriel Fernandez | 9afd9b7 | 2013-03-18 11:45:05 +0100 | [diff] [blame] | 444 | stmpe_gpio->chip.of_node = np; |
Linus Walleij | 9e9dc7d | 2014-05-08 23:16:34 +0200 | [diff] [blame] | 445 | stmpe_gpio->chip.base = -1; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 446 | |
Linus Walleij | 27ec8a9 | 2014-10-02 07:55:41 +0200 | [diff] [blame] | 447 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
| 448 | stmpe_gpio->chip.dbg_show = stmpe_dbg_show; |
| 449 | |
Linus Walleij | 1dfb4a0 | 2015-01-13 08:00:29 +0100 | [diff] [blame] | 450 | of_property_read_u32(np, "st,norequest-mask", |
| 451 | &stmpe_gpio->norequest_mask); |
Vipul Kumar Samar | 86605cf | 2012-11-26 17:06:51 +0530 | [diff] [blame] | 452 | |
Linus Walleij | 9e9dc7d | 2014-05-08 23:16:34 +0200 | [diff] [blame] | 453 | if (irq < 0) |
Chris Blair | 38040c8 | 2012-01-26 22:17:15 +0100 | [diff] [blame] | 454 | dev_info(&pdev->dev, |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 455 | "device configured in no-irq mode: " |
Chris Blair | 38040c8 | 2012-01-26 22:17:15 +0100 | [diff] [blame] | 456 | "irqs are not available\n"); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 457 | |
| 458 | ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO); |
| 459 | if (ret) |
Vasiliy Kulikov | 02bf074 | 2010-09-12 22:57:19 +0400 | [diff] [blame] | 460 | goto out_free; |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 461 | |
Linus Walleij | b03c04a | 2015-12-07 14:32:13 +0100 | [diff] [blame] | 462 | ret = gpiochip_add_data(&stmpe_gpio->chip, stmpe_gpio); |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 463 | if (ret) { |
| 464 | dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret); |
| 465 | goto out_disable; |
| 466 | } |
| 467 | |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 468 | 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 Blair | 38040c8 | 2012-01-26 22:17:15 +0100 | [diff] [blame] | 472 | if (ret) { |
| 473 | dev_err(&pdev->dev, "unable to get irq: %d\n", ret); |
Lee Jones | fc13d5a | 2012-12-10 10:07:54 +0000 | [diff] [blame] | 474 | goto out_disable; |
Chris Blair | 38040c8 | 2012-01-26 22:17:15 +0100 | [diff] [blame] | 475 | } |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 476 | 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 Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 484 | goto out_disable; |
Linus Walleij | fe44e70 | 2014-04-15 23:38:56 +0200 | [diff] [blame] | 485 | } |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 486 | |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 487 | gpiochip_set_chained_irqchip(&stmpe_gpio->chip, |
| 488 | &stmpe_gpio_irq_chip, |
| 489 | irq, |
| 490 | NULL); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 491 | } |
| 492 | |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 493 | platform_set_drvdata(pdev, stmpe_gpio); |
| 494 | |
| 495 | return 0; |
| 496 | |
Vasiliy Kulikov | 02bf074 | 2010-09-12 22:57:19 +0400 | [diff] [blame] | 497 | out_disable: |
| 498 | stmpe_disable(stmpe, STMPE_BLOCK_GPIO); |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 499 | gpiochip_remove(&stmpe_gpio->chip); |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 500 | out_free: |
| 501 | kfree(stmpe_gpio); |
| 502 | return ret; |
| 503 | } |
| 504 | |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 505 | static struct platform_driver stmpe_gpio_driver = { |
Paul Gortmaker | 3b52bb9 | 2016-05-09 19:59:56 -0400 | [diff] [blame] | 506 | .driver = { |
| 507 | .suppress_bind_attrs = true, |
| 508 | .name = "stmpe-gpio", |
Paul Gortmaker | 3b52bb9 | 2016-05-09 19:59:56 -0400 | [diff] [blame] | 509 | }, |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 510 | .probe = stmpe_gpio_probe, |
Rabin Vincent | 03f822f | 2010-07-02 16:52:09 +0530 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | static int __init stmpe_gpio_init(void) |
| 514 | { |
| 515 | return platform_driver_register(&stmpe_gpio_driver); |
| 516 | } |
| 517 | subsys_initcall(stmpe_gpio_init); |