1c103de24SGrant Likely /* 2c103de24SGrant Likely * Copyright (C) ST-Ericsson SA 2010 3c103de24SGrant Likely * 4c103de24SGrant Likely * License Terms: GNU General Public License, version 2 5c103de24SGrant Likely * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson 6c103de24SGrant Likely */ 7c103de24SGrant Likely 8c103de24SGrant Likely #include <linux/init.h> 9c103de24SGrant Likely #include <linux/platform_device.h> 10c103de24SGrant Likely #include <linux/slab.h> 11c103de24SGrant Likely #include <linux/gpio.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> 16c103de24SGrant Likely 17c103de24SGrant Likely /* 18c103de24SGrant Likely * These registers are modified under the irq bus lock and cached to avoid 19c103de24SGrant Likely * unnecessary writes in bus_sync_unlock. 20c103de24SGrant Likely */ 21c103de24SGrant Likely enum { REG_RE, REG_FE, REG_IE }; 22c103de24SGrant Likely 2343db289dSPatrice Chotard enum { LSB, CSB, MSB }; 2443db289dSPatrice Chotard 25c103de24SGrant Likely #define CACHE_NR_REGS 3 269e9dc7d9SLinus Walleij /* No variant has more than 24 GPIOs */ 279e9dc7d9SLinus Walleij #define CACHE_NR_BANKS (24 / 8) 28c103de24SGrant Likely 29c103de24SGrant Likely struct stmpe_gpio { 30c103de24SGrant Likely struct gpio_chip chip; 31c103de24SGrant Likely struct stmpe *stmpe; 32c103de24SGrant Likely struct device *dev; 33c103de24SGrant Likely struct mutex irq_lock; 341dfb4a0dSLinus Walleij u32 norequest_mask; 35c103de24SGrant Likely /* Caches of interrupt control registers for bus_lock */ 36c103de24SGrant Likely u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS]; 37c103de24SGrant Likely u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS]; 38c103de24SGrant Likely }; 39c103de24SGrant Likely 40c103de24SGrant Likely static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset) 41c103de24SGrant Likely { 42b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); 43c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe; 4443db289dSPatrice Chotard u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB + (offset / 8)]; 45c103de24SGrant Likely u8 mask = 1 << (offset % 8); 46c103de24SGrant Likely int ret; 47c103de24SGrant Likely 48c103de24SGrant Likely ret = stmpe_reg_read(stmpe, reg); 49c103de24SGrant Likely if (ret < 0) 50c103de24SGrant Likely return ret; 51c103de24SGrant Likely 527535b8beSBhupesh Sharma return !!(ret & mask); 53c103de24SGrant Likely } 54c103de24SGrant Likely 55c103de24SGrant Likely static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val) 56c103de24SGrant Likely { 57b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); 58c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe; 59c103de24SGrant Likely int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB; 6043db289dSPatrice Chotard u8 reg = stmpe->regs[which + (offset / 8)]; 61c103de24SGrant Likely u8 mask = 1 << (offset % 8); 62c103de24SGrant Likely 63cccdceb9SViresh Kumar /* 64cccdceb9SViresh Kumar * Some variants have single register for gpio set/clear functionality. 65cccdceb9SViresh Kumar * For them we need to write 0 to clear and 1 to set. 66cccdceb9SViresh Kumar */ 67cccdceb9SViresh Kumar if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB]) 68cccdceb9SViresh Kumar stmpe_set_bits(stmpe, reg, mask, val ? mask : 0); 69cccdceb9SViresh Kumar else 70c103de24SGrant Likely stmpe_reg_write(stmpe, reg, mask); 71c103de24SGrant Likely } 72c103de24SGrant Likely 738e293fb0SLinus Walleij static int stmpe_gpio_get_direction(struct gpio_chip *chip, 748e293fb0SLinus Walleij unsigned offset) 758e293fb0SLinus Walleij { 768e293fb0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); 778e293fb0SLinus Walleij struct stmpe *stmpe = stmpe_gpio->stmpe; 788e293fb0SLinus Walleij u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8); 798e293fb0SLinus Walleij u8 mask = 1 << (offset % 8); 808e293fb0SLinus Walleij int ret; 818e293fb0SLinus Walleij 828e293fb0SLinus Walleij ret = stmpe_reg_read(stmpe, reg); 838e293fb0SLinus Walleij if (ret < 0) 848e293fb0SLinus Walleij return ret; 858e293fb0SLinus Walleij 868e293fb0SLinus Walleij return !(ret & mask); 878e293fb0SLinus Walleij } 888e293fb0SLinus Walleij 89c103de24SGrant Likely static int stmpe_gpio_direction_output(struct gpio_chip *chip, 90c103de24SGrant Likely unsigned offset, int val) 91c103de24SGrant Likely { 92b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); 93c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe; 9443db289dSPatrice Chotard u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)]; 95c103de24SGrant Likely u8 mask = 1 << (offset % 8); 96c103de24SGrant Likely 97c103de24SGrant Likely stmpe_gpio_set(chip, offset, val); 98c103de24SGrant Likely 99c103de24SGrant Likely return stmpe_set_bits(stmpe, reg, mask, mask); 100c103de24SGrant Likely } 101c103de24SGrant Likely 102c103de24SGrant Likely static int stmpe_gpio_direction_input(struct gpio_chip *chip, 103c103de24SGrant Likely unsigned offset) 104c103de24SGrant Likely { 105b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); 106c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe; 10743db289dSPatrice Chotard u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)]; 108c103de24SGrant Likely u8 mask = 1 << (offset % 8); 109c103de24SGrant Likely 110c103de24SGrant Likely return stmpe_set_bits(stmpe, reg, mask, 0); 111c103de24SGrant Likely } 112c103de24SGrant Likely 113c103de24SGrant Likely static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset) 114c103de24SGrant Likely { 115b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip); 116c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe; 117c103de24SGrant Likely 118c103de24SGrant Likely if (stmpe_gpio->norequest_mask & (1 << offset)) 119c103de24SGrant Likely return -EINVAL; 120c103de24SGrant Likely 121c103de24SGrant Likely return stmpe_set_altfunc(stmpe, 1 << offset, STMPE_BLOCK_GPIO); 122c103de24SGrant Likely } 123c103de24SGrant Likely 124c103de24SGrant Likely static struct gpio_chip template_chip = { 125c103de24SGrant Likely .label = "stmpe", 126c103de24SGrant Likely .owner = THIS_MODULE, 1278e293fb0SLinus Walleij .get_direction = stmpe_gpio_get_direction, 128c103de24SGrant Likely .direction_input = stmpe_gpio_direction_input, 129c103de24SGrant Likely .get = stmpe_gpio_get, 130c103de24SGrant Likely .direction_output = stmpe_gpio_direction_output, 131c103de24SGrant Likely .set = stmpe_gpio_set, 132c103de24SGrant Likely .request = stmpe_gpio_request, 1339fb1f39eSLinus Walleij .can_sleep = true, 134c103de24SGrant Likely }; 135c103de24SGrant Likely 136c103de24SGrant Likely static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type) 137c103de24SGrant Likely { 138fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 139b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); 140fc13d5a5SLee Jones int offset = d->hwirq; 141c103de24SGrant Likely int regoffset = offset / 8; 142c103de24SGrant Likely int mask = 1 << (offset % 8); 143c103de24SGrant Likely 1441fe3bd9eSLinus Walleij if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH) 145c103de24SGrant Likely return -EINVAL; 146c103de24SGrant Likely 147*c6a05a05SPatrice Chotard /* STMPE801 and STMPE 1600 don't have RE and FE registers */ 148*c6a05a05SPatrice Chotard if (stmpe_gpio->stmpe->partnum == STMPE801 || 149*c6a05a05SPatrice Chotard stmpe_gpio->stmpe->partnum == STMPE1600) 150cccdceb9SViresh Kumar return 0; 151cccdceb9SViresh Kumar 1521fe3bd9eSLinus Walleij if (type & IRQ_TYPE_EDGE_RISING) 153c103de24SGrant Likely stmpe_gpio->regs[REG_RE][regoffset] |= mask; 154c103de24SGrant Likely else 155c103de24SGrant Likely stmpe_gpio->regs[REG_RE][regoffset] &= ~mask; 156c103de24SGrant Likely 1571fe3bd9eSLinus Walleij if (type & IRQ_TYPE_EDGE_FALLING) 158c103de24SGrant Likely stmpe_gpio->regs[REG_FE][regoffset] |= mask; 159c103de24SGrant Likely else 160c103de24SGrant Likely stmpe_gpio->regs[REG_FE][regoffset] &= ~mask; 161c103de24SGrant Likely 162c103de24SGrant Likely return 0; 163c103de24SGrant Likely } 164c103de24SGrant Likely 165c103de24SGrant Likely static void stmpe_gpio_irq_lock(struct irq_data *d) 166c103de24SGrant Likely { 167fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 168b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); 169c103de24SGrant Likely 170c103de24SGrant Likely mutex_lock(&stmpe_gpio->irq_lock); 171c103de24SGrant Likely } 172c103de24SGrant Likely 173c103de24SGrant Likely static void stmpe_gpio_irq_sync_unlock(struct irq_data *d) 174c103de24SGrant Likely { 175fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 176b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); 177c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe; 178c103de24SGrant Likely int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8); 17943db289dSPatrice Chotard static const u8 regmap[CACHE_NR_REGS][CACHE_NR_BANKS] = { 18043db289dSPatrice Chotard [REG_RE][LSB] = STMPE_IDX_GPRER_LSB, 18143db289dSPatrice Chotard [REG_RE][CSB] = STMPE_IDX_GPRER_CSB, 18243db289dSPatrice Chotard [REG_RE][MSB] = STMPE_IDX_GPRER_MSB, 18343db289dSPatrice Chotard [REG_FE][LSB] = STMPE_IDX_GPFER_LSB, 18443db289dSPatrice Chotard [REG_FE][CSB] = STMPE_IDX_GPFER_CSB, 18543db289dSPatrice Chotard [REG_FE][MSB] = STMPE_IDX_GPFER_MSB, 18643db289dSPatrice Chotard [REG_IE][LSB] = STMPE_IDX_IEGPIOR_LSB, 18743db289dSPatrice Chotard [REG_IE][CSB] = STMPE_IDX_IEGPIOR_CSB, 18843db289dSPatrice Chotard [REG_IE][MSB] = STMPE_IDX_IEGPIOR_MSB, 189c103de24SGrant Likely }; 190c103de24SGrant Likely int i, j; 191c103de24SGrant Likely 192c103de24SGrant Likely for (i = 0; i < CACHE_NR_REGS; i++) { 193*c6a05a05SPatrice Chotard /* STMPE801 and STMPE1600 don't have RE and FE registers */ 194*c6a05a05SPatrice Chotard if ((stmpe->partnum == STMPE801 || 195*c6a05a05SPatrice Chotard stmpe->partnum == STMPE1600) && 196cccdceb9SViresh Kumar (i != REG_IE)) 197cccdceb9SViresh Kumar continue; 198cccdceb9SViresh Kumar 199c103de24SGrant Likely for (j = 0; j < num_banks; j++) { 200c103de24SGrant Likely u8 old = stmpe_gpio->oldregs[i][j]; 201c103de24SGrant Likely u8 new = stmpe_gpio->regs[i][j]; 202c103de24SGrant Likely 203c103de24SGrant Likely if (new == old) 204c103de24SGrant Likely continue; 205c103de24SGrant Likely 206c103de24SGrant Likely stmpe_gpio->oldregs[i][j] = new; 20743db289dSPatrice Chotard stmpe_reg_write(stmpe, stmpe->regs[regmap[i][j]], new); 208c103de24SGrant Likely } 209c103de24SGrant Likely } 210c103de24SGrant Likely 211c103de24SGrant Likely mutex_unlock(&stmpe_gpio->irq_lock); 212c103de24SGrant Likely } 213c103de24SGrant Likely 214c103de24SGrant Likely static void stmpe_gpio_irq_mask(struct irq_data *d) 215c103de24SGrant Likely { 216fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 217b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); 218fc13d5a5SLee Jones int offset = d->hwirq; 219c103de24SGrant Likely int regoffset = offset / 8; 220c103de24SGrant Likely int mask = 1 << (offset % 8); 221c103de24SGrant Likely 222c103de24SGrant Likely stmpe_gpio->regs[REG_IE][regoffset] &= ~mask; 223c103de24SGrant Likely } 224c103de24SGrant Likely 225c103de24SGrant Likely static void stmpe_gpio_irq_unmask(struct irq_data *d) 226c103de24SGrant Likely { 227fe44e70dSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 228b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); 229*c6a05a05SPatrice Chotard struct stmpe *stmpe = stmpe_gpio->stmpe; 230fc13d5a5SLee Jones int offset = d->hwirq; 231c103de24SGrant Likely int regoffset = offset / 8; 232c103de24SGrant Likely int mask = 1 << (offset % 8); 233c103de24SGrant Likely 234c103de24SGrant Likely stmpe_gpio->regs[REG_IE][regoffset] |= mask; 235*c6a05a05SPatrice Chotard 236*c6a05a05SPatrice Chotard /* 237*c6a05a05SPatrice Chotard * STMPE1600 workaround: to be able to get IRQ from pins, 238*c6a05a05SPatrice Chotard * a read must be done on GPMR register, or a write in 239*c6a05a05SPatrice Chotard * GPSR or GPCR registers 240*c6a05a05SPatrice Chotard */ 241*c6a05a05SPatrice Chotard if (stmpe->partnum == STMPE1600) 242*c6a05a05SPatrice Chotard stmpe_reg_read(stmpe, 243*c6a05a05SPatrice Chotard stmpe->regs[STMPE_IDX_GPMR_LSB + regoffset]); 244c103de24SGrant Likely } 245c103de24SGrant Likely 24627ec8a9cSLinus Walleij static void stmpe_dbg_show_one(struct seq_file *s, 24727ec8a9cSLinus Walleij struct gpio_chip *gc, 24827ec8a9cSLinus Walleij unsigned offset, unsigned gpio) 24927ec8a9cSLinus Walleij { 250b03c04a0SLinus Walleij struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); 25127ec8a9cSLinus Walleij struct stmpe *stmpe = stmpe_gpio->stmpe; 25227ec8a9cSLinus Walleij const char *label = gpiochip_is_requested(gc, offset); 25327ec8a9cSLinus Walleij bool val = !!stmpe_gpio_get(gc, offset); 25443db289dSPatrice Chotard u8 bank = offset / 8; 25543db289dSPatrice Chotard u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank]; 25627ec8a9cSLinus Walleij u8 mask = 1 << (offset % 8); 25727ec8a9cSLinus Walleij int ret; 25827ec8a9cSLinus Walleij u8 dir; 25927ec8a9cSLinus Walleij 26027ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, dir_reg); 26127ec8a9cSLinus Walleij if (ret < 0) 26227ec8a9cSLinus Walleij return; 26327ec8a9cSLinus Walleij dir = !!(ret & mask); 26427ec8a9cSLinus Walleij 26527ec8a9cSLinus Walleij if (dir) { 26627ec8a9cSLinus Walleij seq_printf(s, " gpio-%-3d (%-20.20s) out %s", 26727ec8a9cSLinus Walleij gpio, label ?: "(none)", 26827ec8a9cSLinus Walleij val ? "hi" : "lo"); 26927ec8a9cSLinus Walleij } else { 270287849cbSPatrice Chotard u8 edge_det_reg; 271287849cbSPatrice Chotard u8 rise_reg; 272287849cbSPatrice Chotard u8 fall_reg; 273287849cbSPatrice Chotard u8 irqen_reg; 274287849cbSPatrice Chotard 275287849cbSPatrice Chotard char *edge_det_values[] = {"edge-inactive", 276287849cbSPatrice Chotard "edge-asserted", 277287849cbSPatrice Chotard "not-supported"}; 278287849cbSPatrice Chotard char *rise_values[] = {"no-rising-edge-detection", 279287849cbSPatrice Chotard "rising-edge-detection", 280287849cbSPatrice Chotard "not-supported"}; 281287849cbSPatrice Chotard char *fall_values[] = {"no-falling-edge-detection", 282287849cbSPatrice Chotard "falling-edge-detection", 283287849cbSPatrice Chotard "not-supported"}; 284287849cbSPatrice Chotard #define NOT_SUPPORTED_IDX 2 285287849cbSPatrice Chotard u8 edge_det = NOT_SUPPORTED_IDX; 286287849cbSPatrice Chotard u8 rise = NOT_SUPPORTED_IDX; 287287849cbSPatrice Chotard u8 fall = NOT_SUPPORTED_IDX; 28827ec8a9cSLinus Walleij bool irqen; 28927ec8a9cSLinus Walleij 290287849cbSPatrice Chotard switch (stmpe->partnum) { 291287849cbSPatrice Chotard case STMPE610: 292287849cbSPatrice Chotard case STMPE811: 293287849cbSPatrice Chotard case STMPE1601: 294287849cbSPatrice Chotard case STMPE2401: 295287849cbSPatrice Chotard case STMPE2403: 29643db289dSPatrice Chotard edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_LSB + bank]; 29727ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, edge_det_reg); 29827ec8a9cSLinus Walleij if (ret < 0) 29927ec8a9cSLinus Walleij return; 30027ec8a9cSLinus Walleij edge_det = !!(ret & mask); 301287849cbSPatrice Chotard 302287849cbSPatrice Chotard case STMPE1801: 30343db289dSPatrice Chotard rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank]; 30443db289dSPatrice Chotard fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank]; 30543db289dSPatrice Chotard 30627ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, rise_reg); 30727ec8a9cSLinus Walleij if (ret < 0) 30827ec8a9cSLinus Walleij return; 30927ec8a9cSLinus Walleij rise = !!(ret & mask); 31027ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, fall_reg); 31127ec8a9cSLinus Walleij if (ret < 0) 31227ec8a9cSLinus Walleij return; 31327ec8a9cSLinus Walleij fall = !!(ret & mask); 314287849cbSPatrice Chotard 315287849cbSPatrice Chotard case STMPE801: 316*c6a05a05SPatrice Chotard case STMPE1600: 31743db289dSPatrice Chotard irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank]; 318287849cbSPatrice Chotard break; 319287849cbSPatrice Chotard 320287849cbSPatrice Chotard default: 321287849cbSPatrice Chotard return; 322287849cbSPatrice Chotard } 323287849cbSPatrice Chotard 32427ec8a9cSLinus Walleij ret = stmpe_reg_read(stmpe, irqen_reg); 32527ec8a9cSLinus Walleij if (ret < 0) 32627ec8a9cSLinus Walleij return; 32727ec8a9cSLinus Walleij irqen = !!(ret & mask); 32827ec8a9cSLinus Walleij 329287849cbSPatrice Chotard seq_printf(s, " gpio-%-3d (%-20.20s) in %s %13s %13s %25s %25s", 33027ec8a9cSLinus Walleij gpio, label ?: "(none)", 33127ec8a9cSLinus Walleij val ? "hi" : "lo", 332287849cbSPatrice Chotard edge_det_values[edge_det], 333287849cbSPatrice Chotard irqen ? "IRQ-enabled" : "IRQ-disabled", 334287849cbSPatrice Chotard rise_values[rise], 335287849cbSPatrice Chotard fall_values[fall]); 33627ec8a9cSLinus Walleij } 33727ec8a9cSLinus Walleij } 33827ec8a9cSLinus Walleij 33927ec8a9cSLinus Walleij static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc) 34027ec8a9cSLinus Walleij { 34127ec8a9cSLinus Walleij unsigned i; 34227ec8a9cSLinus Walleij unsigned gpio = gc->base; 34327ec8a9cSLinus Walleij 34427ec8a9cSLinus Walleij for (i = 0; i < gc->ngpio; i++, gpio++) { 34527ec8a9cSLinus Walleij stmpe_dbg_show_one(s, gc, i, gpio); 34627ec8a9cSLinus Walleij seq_printf(s, "\n"); 34727ec8a9cSLinus Walleij } 34827ec8a9cSLinus Walleij } 34927ec8a9cSLinus Walleij 350c103de24SGrant Likely static struct irq_chip stmpe_gpio_irq_chip = { 351c103de24SGrant Likely .name = "stmpe-gpio", 352c103de24SGrant Likely .irq_bus_lock = stmpe_gpio_irq_lock, 353c103de24SGrant Likely .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock, 354c103de24SGrant Likely .irq_mask = stmpe_gpio_irq_mask, 355c103de24SGrant Likely .irq_unmask = stmpe_gpio_irq_unmask, 356c103de24SGrant Likely .irq_set_type = stmpe_gpio_irq_set_type, 357c103de24SGrant Likely }; 358c103de24SGrant Likely 359c103de24SGrant Likely static irqreturn_t stmpe_gpio_irq(int irq, void *dev) 360c103de24SGrant Likely { 361c103de24SGrant Likely struct stmpe_gpio *stmpe_gpio = dev; 362c103de24SGrant Likely struct stmpe *stmpe = stmpe_gpio->stmpe; 363*c6a05a05SPatrice Chotard u8 statmsbreg; 364c103de24SGrant Likely int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8); 365c103de24SGrant Likely u8 status[num_banks]; 366c103de24SGrant Likely int ret; 367c103de24SGrant Likely int i; 368c103de24SGrant Likely 369*c6a05a05SPatrice Chotard /* 370*c6a05a05SPatrice Chotard * the stmpe_block_read() call below, imposes to set statmsbreg 371*c6a05a05SPatrice Chotard * with the register located at the lowest address. As STMPE1600 372*c6a05a05SPatrice Chotard * variant is the only one which respect registers address's order 373*c6a05a05SPatrice Chotard * (LSB regs located at lowest address than MSB ones) whereas all 374*c6a05a05SPatrice Chotard * the others have a registers layout with MSB located before the 375*c6a05a05SPatrice Chotard * LSB regs. 376*c6a05a05SPatrice Chotard */ 377*c6a05a05SPatrice Chotard if (stmpe->partnum == STMPE1600) 378*c6a05a05SPatrice Chotard statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_LSB]; 379*c6a05a05SPatrice Chotard else 380*c6a05a05SPatrice Chotard statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB]; 381*c6a05a05SPatrice Chotard 382c103de24SGrant Likely ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status); 383c103de24SGrant Likely if (ret < 0) 384c103de24SGrant Likely return IRQ_NONE; 385c103de24SGrant Likely 386c103de24SGrant Likely for (i = 0; i < num_banks; i++) { 387*c6a05a05SPatrice Chotard int bank = (stmpe_gpio->stmpe->partnum == STMPE1600) ? i : 388*c6a05a05SPatrice Chotard num_banks - i - 1; 389c103de24SGrant Likely unsigned int enabled = stmpe_gpio->regs[REG_IE][bank]; 390c103de24SGrant Likely unsigned int stat = status[i]; 391c103de24SGrant Likely 392c103de24SGrant Likely stat &= enabled; 393c103de24SGrant Likely if (!stat) 394c103de24SGrant Likely continue; 395c103de24SGrant Likely 396c103de24SGrant Likely while (stat) { 397c103de24SGrant Likely int bit = __ffs(stat); 398c103de24SGrant Likely int line = bank * 8 + bit; 399fe44e70dSLinus Walleij int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain, 400ed05e204SLinus Walleij line); 401c103de24SGrant Likely 402ed05e204SLinus Walleij handle_nested_irq(child_irq); 403c103de24SGrant Likely stat &= ~(1 << bit); 404c103de24SGrant Likely } 405c103de24SGrant Likely 4066936e1f8SPatrice Chotard /* 4076936e1f8SPatrice Chotard * interrupt status register write has no effect on 408*c6a05a05SPatrice Chotard * 801/1801/1600, bits are cleared when read. 409*c6a05a05SPatrice Chotard * Edge detect register is not present on 801/1600/1801 4106936e1f8SPatrice Chotard */ 411*c6a05a05SPatrice Chotard if (stmpe->partnum != STMPE801 || stmpe->partnum != STMPE1600 || 412*c6a05a05SPatrice Chotard stmpe->partnum != STMPE1801) { 413c103de24SGrant Likely stmpe_reg_write(stmpe, statmsbreg + i, status[i]); 41443db289dSPatrice Chotard stmpe_reg_write(stmpe, 41543db289dSPatrice Chotard stmpe->regs[STMPE_IDX_GPEDR_LSB + i], 41643db289dSPatrice Chotard status[i]); 417c103de24SGrant Likely } 4186936e1f8SPatrice Chotard } 419c103de24SGrant Likely 420c103de24SGrant Likely return IRQ_HANDLED; 421c103de24SGrant Likely } 422c103de24SGrant Likely 4233836309dSBill Pemberton static int stmpe_gpio_probe(struct platform_device *pdev) 424c103de24SGrant Likely { 425c103de24SGrant Likely struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent); 42686605cfeSVipul Kumar Samar struct device_node *np = pdev->dev.of_node; 427c103de24SGrant Likely struct stmpe_gpio *stmpe_gpio; 428c103de24SGrant Likely int ret; 42938040c85SChris Blair int irq = 0; 430c103de24SGrant Likely 431c103de24SGrant Likely irq = platform_get_irq(pdev, 0); 432c103de24SGrant Likely 433c103de24SGrant Likely stmpe_gpio = kzalloc(sizeof(struct stmpe_gpio), GFP_KERNEL); 434c103de24SGrant Likely if (!stmpe_gpio) 435c103de24SGrant Likely return -ENOMEM; 436c103de24SGrant Likely 437c103de24SGrant Likely mutex_init(&stmpe_gpio->irq_lock); 438c103de24SGrant Likely 439c103de24SGrant Likely stmpe_gpio->dev = &pdev->dev; 440c103de24SGrant Likely stmpe_gpio->stmpe = stmpe; 441c103de24SGrant Likely stmpe_gpio->chip = template_chip; 442c103de24SGrant Likely stmpe_gpio->chip.ngpio = stmpe->num_gpios; 44358383c78SLinus Walleij stmpe_gpio->chip.parent = &pdev->dev; 4449afd9b70SGabriel Fernandez stmpe_gpio->chip.of_node = np; 4459e9dc7d9SLinus Walleij stmpe_gpio->chip.base = -1; 446c103de24SGrant Likely 44727ec8a9cSLinus Walleij if (IS_ENABLED(CONFIG_DEBUG_FS)) 44827ec8a9cSLinus Walleij stmpe_gpio->chip.dbg_show = stmpe_dbg_show; 44927ec8a9cSLinus Walleij 45086605cfeSVipul Kumar Samar of_property_read_u32(np, "st,norequest-mask", 45186605cfeSVipul Kumar Samar &stmpe_gpio->norequest_mask); 45286605cfeSVipul Kumar Samar 4539e9dc7d9SLinus Walleij if (irq < 0) 45438040c85SChris Blair dev_info(&pdev->dev, 455fe44e70dSLinus Walleij "device configured in no-irq mode: " 45638040c85SChris Blair "irqs are not available\n"); 457c103de24SGrant Likely 458c103de24SGrant Likely ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO); 459c103de24SGrant Likely if (ret) 460c103de24SGrant Likely goto out_free; 461c103de24SGrant Likely 462b03c04a0SLinus Walleij ret = gpiochip_add_data(&stmpe_gpio->chip, stmpe_gpio); 4633f97d5fcSLinus Walleij if (ret) { 4643f97d5fcSLinus Walleij dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret); 4653f97d5fcSLinus Walleij goto out_disable; 4663f97d5fcSLinus Walleij } 4673f97d5fcSLinus Walleij 468fe44e70dSLinus Walleij if (irq > 0) { 469fe44e70dSLinus Walleij ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 470fe44e70dSLinus Walleij stmpe_gpio_irq, IRQF_ONESHOT, 471fe44e70dSLinus Walleij "stmpe-gpio", stmpe_gpio); 472c103de24SGrant Likely if (ret) { 473c103de24SGrant Likely dev_err(&pdev->dev, "unable to get irq: %d\n", ret); 474fc13d5a5SLee Jones goto out_disable; 475c103de24SGrant Likely } 476fe44e70dSLinus Walleij ret = gpiochip_irqchip_add(&stmpe_gpio->chip, 477fe44e70dSLinus Walleij &stmpe_gpio_irq_chip, 478fe44e70dSLinus Walleij 0, 479fe44e70dSLinus Walleij handle_simple_irq, 480fe44e70dSLinus Walleij IRQ_TYPE_NONE); 481fe44e70dSLinus Walleij if (ret) { 482fe44e70dSLinus Walleij dev_err(&pdev->dev, 483fe44e70dSLinus Walleij "could not connect irqchip to gpiochip\n"); 4843f97d5fcSLinus Walleij goto out_disable; 48538040c85SChris Blair } 486c103de24SGrant Likely 4873f97d5fcSLinus Walleij gpiochip_set_chained_irqchip(&stmpe_gpio->chip, 4883f97d5fcSLinus Walleij &stmpe_gpio_irq_chip, 4893f97d5fcSLinus Walleij irq, 4903f97d5fcSLinus Walleij NULL); 491c103de24SGrant Likely } 492c103de24SGrant Likely 493c103de24SGrant Likely platform_set_drvdata(pdev, stmpe_gpio); 494c103de24SGrant Likely 495c103de24SGrant Likely return 0; 496c103de24SGrant Likely 497c103de24SGrant Likely out_disable: 498c103de24SGrant Likely stmpe_disable(stmpe, STMPE_BLOCK_GPIO); 4993f97d5fcSLinus Walleij gpiochip_remove(&stmpe_gpio->chip); 500c103de24SGrant Likely out_free: 501c103de24SGrant Likely kfree(stmpe_gpio); 502c103de24SGrant Likely return ret; 503c103de24SGrant Likely } 504c103de24SGrant Likely 505c103de24SGrant Likely static struct platform_driver stmpe_gpio_driver = { 5063b52bb96SPaul Gortmaker .driver = { 5073b52bb96SPaul Gortmaker .suppress_bind_attrs = true, 5083b52bb96SPaul Gortmaker .name = "stmpe-gpio", 5093b52bb96SPaul Gortmaker }, 510c103de24SGrant Likely .probe = stmpe_gpio_probe, 511c103de24SGrant Likely }; 512c103de24SGrant Likely 513c103de24SGrant Likely static int __init stmpe_gpio_init(void) 514c103de24SGrant Likely { 515c103de24SGrant Likely return platform_driver_register(&stmpe_gpio_driver); 516c103de24SGrant Likely } 517c103de24SGrant Likely subsys_initcall(stmpe_gpio_init); 518