1ccf6fd6dSAndy Shevchenko /* 2ccf6fd6dSAndy Shevchenko * Intel Merrifield SoC GPIO driver 3ccf6fd6dSAndy Shevchenko * 4ccf6fd6dSAndy Shevchenko * Copyright (c) 2016 Intel Corporation. 5ccf6fd6dSAndy Shevchenko * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com> 6ccf6fd6dSAndy Shevchenko * 7ccf6fd6dSAndy Shevchenko * This program is free software; you can redistribute it and/or modify 8ccf6fd6dSAndy Shevchenko * it under the terms of the GNU General Public License version 2 as 9ccf6fd6dSAndy Shevchenko * published by the Free Software Foundation. 10ccf6fd6dSAndy Shevchenko */ 11ccf6fd6dSAndy Shevchenko 12ccf6fd6dSAndy Shevchenko #include <linux/bitops.h> 13ccf6fd6dSAndy Shevchenko #include <linux/gpio/driver.h> 14ccf6fd6dSAndy Shevchenko #include <linux/init.h> 15ccf6fd6dSAndy Shevchenko #include <linux/interrupt.h> 16ccf6fd6dSAndy Shevchenko #include <linux/io.h> 17ccf6fd6dSAndy Shevchenko #include <linux/module.h> 18ccf6fd6dSAndy Shevchenko #include <linux/pci.h> 19ccf6fd6dSAndy Shevchenko #include <linux/pinctrl/consumer.h> 20ccf6fd6dSAndy Shevchenko 21ccf6fd6dSAndy Shevchenko #define GCCR 0x000 /* controller configuration */ 22ccf6fd6dSAndy Shevchenko #define GPLR 0x004 /* pin level r/o */ 23ccf6fd6dSAndy Shevchenko #define GPDR 0x01c /* pin direction */ 24ccf6fd6dSAndy Shevchenko #define GPSR 0x034 /* pin set w/o */ 25ccf6fd6dSAndy Shevchenko #define GPCR 0x04c /* pin clear w/o */ 26ccf6fd6dSAndy Shevchenko #define GRER 0x064 /* rising edge detect */ 27ccf6fd6dSAndy Shevchenko #define GFER 0x07c /* falling edge detect */ 28ccf6fd6dSAndy Shevchenko #define GFBR 0x094 /* glitch filter bypass */ 29ccf6fd6dSAndy Shevchenko #define GIMR 0x0ac /* interrupt mask */ 30ccf6fd6dSAndy Shevchenko #define GISR 0x0c4 /* interrupt source */ 31ccf6fd6dSAndy Shevchenko #define GITR 0x300 /* input type */ 32ccf6fd6dSAndy Shevchenko #define GLPR 0x318 /* level input polarity */ 33ccf6fd6dSAndy Shevchenko #define GWMR 0x400 /* wake mask */ 34ccf6fd6dSAndy Shevchenko #define GWSR 0x418 /* wake source */ 35ccf6fd6dSAndy Shevchenko #define GSIR 0xc00 /* secure input */ 36ccf6fd6dSAndy Shevchenko 37ccf6fd6dSAndy Shevchenko /* Intel Merrifield has 192 GPIO pins */ 38ccf6fd6dSAndy Shevchenko #define MRFLD_NGPIO 192 39ccf6fd6dSAndy Shevchenko 40ccf6fd6dSAndy Shevchenko struct mrfld_gpio_pinrange { 41ccf6fd6dSAndy Shevchenko unsigned int gpio_base; 42ccf6fd6dSAndy Shevchenko unsigned int pin_base; 43ccf6fd6dSAndy Shevchenko unsigned int npins; 44ccf6fd6dSAndy Shevchenko }; 45ccf6fd6dSAndy Shevchenko 46ccf6fd6dSAndy Shevchenko #define GPIO_PINRANGE(gstart, gend, pstart) \ 47ccf6fd6dSAndy Shevchenko { \ 48ccf6fd6dSAndy Shevchenko .gpio_base = (gstart), \ 49ccf6fd6dSAndy Shevchenko .pin_base = (pstart), \ 50ccf6fd6dSAndy Shevchenko .npins = (gend) - (gstart) + 1, \ 51ccf6fd6dSAndy Shevchenko } 52ccf6fd6dSAndy Shevchenko 53ccf6fd6dSAndy Shevchenko struct mrfld_gpio { 54ccf6fd6dSAndy Shevchenko struct gpio_chip chip; 55ccf6fd6dSAndy Shevchenko void __iomem *reg_base; 56ccf6fd6dSAndy Shevchenko raw_spinlock_t lock; 57ccf6fd6dSAndy Shevchenko struct device *dev; 58ccf6fd6dSAndy Shevchenko }; 59ccf6fd6dSAndy Shevchenko 60ccf6fd6dSAndy Shevchenko static const struct mrfld_gpio_pinrange mrfld_gpio_ranges[] = { 61ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(0, 11, 146), 62ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(12, 13, 144), 63ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(14, 15, 35), 64ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(16, 16, 164), 65ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(17, 18, 105), 66ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(19, 22, 101), 67ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(23, 30, 107), 68ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(32, 43, 67), 69ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(44, 63, 195), 70ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(64, 67, 140), 71ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(68, 69, 165), 72ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(70, 71, 65), 73ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(72, 76, 228), 74ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(77, 86, 37), 75ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(87, 87, 48), 76ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(88, 88, 47), 77ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(89, 96, 49), 78ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(97, 97, 34), 79ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(102, 119, 83), 80ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(120, 123, 79), 81ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(124, 135, 115), 82ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(137, 142, 158), 83ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(154, 163, 24), 84ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(164, 176, 215), 85ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(177, 189, 127), 86ccf6fd6dSAndy Shevchenko GPIO_PINRANGE(190, 191, 178), 87ccf6fd6dSAndy Shevchenko }; 88ccf6fd6dSAndy Shevchenko 89ccf6fd6dSAndy Shevchenko static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset, 90ccf6fd6dSAndy Shevchenko unsigned int reg_type_offset) 91ccf6fd6dSAndy Shevchenko { 92ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = gpiochip_get_data(chip); 93ccf6fd6dSAndy Shevchenko u8 reg = offset / 32; 94ccf6fd6dSAndy Shevchenko 95ccf6fd6dSAndy Shevchenko return priv->reg_base + reg_type_offset + reg * 4; 96ccf6fd6dSAndy Shevchenko } 97ccf6fd6dSAndy Shevchenko 98ccf6fd6dSAndy Shevchenko static int mrfld_gpio_get(struct gpio_chip *chip, unsigned int offset) 99ccf6fd6dSAndy Shevchenko { 100ccf6fd6dSAndy Shevchenko void __iomem *gplr = gpio_reg(chip, offset, GPLR); 101ccf6fd6dSAndy Shevchenko 102ccf6fd6dSAndy Shevchenko return !!(readl(gplr) & BIT(offset % 32)); 103ccf6fd6dSAndy Shevchenko } 104ccf6fd6dSAndy Shevchenko 105ccf6fd6dSAndy Shevchenko static void mrfld_gpio_set(struct gpio_chip *chip, unsigned int offset, 106ccf6fd6dSAndy Shevchenko int value) 107ccf6fd6dSAndy Shevchenko { 108*fcce9f14SAndy Shevchenko struct mrfld_gpio *priv = gpiochip_get_data(chip); 109ccf6fd6dSAndy Shevchenko void __iomem *gpsr, *gpcr; 110*fcce9f14SAndy Shevchenko unsigned long flags; 111*fcce9f14SAndy Shevchenko 112*fcce9f14SAndy Shevchenko raw_spin_lock_irqsave(&priv->lock, flags); 113ccf6fd6dSAndy Shevchenko 114ccf6fd6dSAndy Shevchenko if (value) { 115ccf6fd6dSAndy Shevchenko gpsr = gpio_reg(chip, offset, GPSR); 116ccf6fd6dSAndy Shevchenko writel(BIT(offset % 32), gpsr); 117ccf6fd6dSAndy Shevchenko } else { 118ccf6fd6dSAndy Shevchenko gpcr = gpio_reg(chip, offset, GPCR); 119ccf6fd6dSAndy Shevchenko writel(BIT(offset % 32), gpcr); 120ccf6fd6dSAndy Shevchenko } 121*fcce9f14SAndy Shevchenko 122*fcce9f14SAndy Shevchenko raw_spin_unlock_irqrestore(&priv->lock, flags); 123ccf6fd6dSAndy Shevchenko } 124ccf6fd6dSAndy Shevchenko 125ccf6fd6dSAndy Shevchenko static int mrfld_gpio_direction_input(struct gpio_chip *chip, 126ccf6fd6dSAndy Shevchenko unsigned int offset) 127ccf6fd6dSAndy Shevchenko { 128ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = gpiochip_get_data(chip); 129ccf6fd6dSAndy Shevchenko void __iomem *gpdr = gpio_reg(chip, offset, GPDR); 130ccf6fd6dSAndy Shevchenko unsigned long flags; 131ccf6fd6dSAndy Shevchenko u32 value; 132ccf6fd6dSAndy Shevchenko 133ccf6fd6dSAndy Shevchenko raw_spin_lock_irqsave(&priv->lock, flags); 134ccf6fd6dSAndy Shevchenko 135ccf6fd6dSAndy Shevchenko value = readl(gpdr); 136ccf6fd6dSAndy Shevchenko value &= ~BIT(offset % 32); 137ccf6fd6dSAndy Shevchenko writel(value, gpdr); 138ccf6fd6dSAndy Shevchenko 139ccf6fd6dSAndy Shevchenko raw_spin_unlock_irqrestore(&priv->lock, flags); 140ccf6fd6dSAndy Shevchenko 141ccf6fd6dSAndy Shevchenko return 0; 142ccf6fd6dSAndy Shevchenko } 143ccf6fd6dSAndy Shevchenko 144ccf6fd6dSAndy Shevchenko static int mrfld_gpio_direction_output(struct gpio_chip *chip, 145ccf6fd6dSAndy Shevchenko unsigned int offset, int value) 146ccf6fd6dSAndy Shevchenko { 147ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = gpiochip_get_data(chip); 148ccf6fd6dSAndy Shevchenko void __iomem *gpdr = gpio_reg(chip, offset, GPDR); 149ccf6fd6dSAndy Shevchenko unsigned long flags; 150ccf6fd6dSAndy Shevchenko 151ccf6fd6dSAndy Shevchenko mrfld_gpio_set(chip, offset, value); 152ccf6fd6dSAndy Shevchenko 153ccf6fd6dSAndy Shevchenko raw_spin_lock_irqsave(&priv->lock, flags); 154ccf6fd6dSAndy Shevchenko 155ccf6fd6dSAndy Shevchenko value = readl(gpdr); 156ccf6fd6dSAndy Shevchenko value |= BIT(offset % 32); 157ccf6fd6dSAndy Shevchenko writel(value, gpdr); 158ccf6fd6dSAndy Shevchenko 159ccf6fd6dSAndy Shevchenko raw_spin_unlock_irqrestore(&priv->lock, flags); 160ccf6fd6dSAndy Shevchenko 161ccf6fd6dSAndy Shevchenko return 0; 162ccf6fd6dSAndy Shevchenko } 163ccf6fd6dSAndy Shevchenko 164ccf6fd6dSAndy Shevchenko static void mrfld_irq_ack(struct irq_data *d) 165ccf6fd6dSAndy Shevchenko { 166ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d); 167ccf6fd6dSAndy Shevchenko u32 gpio = irqd_to_hwirq(d); 168ccf6fd6dSAndy Shevchenko void __iomem *gisr = gpio_reg(&priv->chip, gpio, GISR); 169*fcce9f14SAndy Shevchenko unsigned long flags; 170*fcce9f14SAndy Shevchenko 171*fcce9f14SAndy Shevchenko raw_spin_lock_irqsave(&priv->lock, flags); 172ccf6fd6dSAndy Shevchenko 173ccf6fd6dSAndy Shevchenko writel(BIT(gpio % 32), gisr); 174*fcce9f14SAndy Shevchenko 175*fcce9f14SAndy Shevchenko raw_spin_unlock_irqrestore(&priv->lock, flags); 176ccf6fd6dSAndy Shevchenko } 177ccf6fd6dSAndy Shevchenko 178ccf6fd6dSAndy Shevchenko static void mrfld_irq_unmask_mask(struct irq_data *d, bool unmask) 179ccf6fd6dSAndy Shevchenko { 180ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d); 181ccf6fd6dSAndy Shevchenko u32 gpio = irqd_to_hwirq(d); 182ccf6fd6dSAndy Shevchenko void __iomem *gimr = gpio_reg(&priv->chip, gpio, GIMR); 183ccf6fd6dSAndy Shevchenko unsigned long flags; 184ccf6fd6dSAndy Shevchenko u32 value; 185ccf6fd6dSAndy Shevchenko 186ccf6fd6dSAndy Shevchenko raw_spin_lock_irqsave(&priv->lock, flags); 187ccf6fd6dSAndy Shevchenko 188ccf6fd6dSAndy Shevchenko if (unmask) 189ccf6fd6dSAndy Shevchenko value = readl(gimr) | BIT(gpio % 32); 190ccf6fd6dSAndy Shevchenko else 191ccf6fd6dSAndy Shevchenko value = readl(gimr) & ~BIT(gpio % 32); 192ccf6fd6dSAndy Shevchenko writel(value, gimr); 193ccf6fd6dSAndy Shevchenko 194ccf6fd6dSAndy Shevchenko raw_spin_unlock_irqrestore(&priv->lock, flags); 195ccf6fd6dSAndy Shevchenko } 196ccf6fd6dSAndy Shevchenko 197ccf6fd6dSAndy Shevchenko static void mrfld_irq_mask(struct irq_data *d) 198ccf6fd6dSAndy Shevchenko { 199ccf6fd6dSAndy Shevchenko mrfld_irq_unmask_mask(d, false); 200ccf6fd6dSAndy Shevchenko } 201ccf6fd6dSAndy Shevchenko 202ccf6fd6dSAndy Shevchenko static void mrfld_irq_unmask(struct irq_data *d) 203ccf6fd6dSAndy Shevchenko { 204ccf6fd6dSAndy Shevchenko mrfld_irq_unmask_mask(d, true); 205ccf6fd6dSAndy Shevchenko } 206ccf6fd6dSAndy Shevchenko 207ccf6fd6dSAndy Shevchenko static int mrfld_irq_set_type(struct irq_data *d, unsigned int type) 208ccf6fd6dSAndy Shevchenko { 209ccf6fd6dSAndy Shevchenko struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 210ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = gpiochip_get_data(gc); 211ccf6fd6dSAndy Shevchenko u32 gpio = irqd_to_hwirq(d); 212ccf6fd6dSAndy Shevchenko void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER); 213ccf6fd6dSAndy Shevchenko void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER); 214ccf6fd6dSAndy Shevchenko void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR); 215ccf6fd6dSAndy Shevchenko void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR); 216ccf6fd6dSAndy Shevchenko unsigned long flags; 217ccf6fd6dSAndy Shevchenko u32 value; 218ccf6fd6dSAndy Shevchenko 219ccf6fd6dSAndy Shevchenko raw_spin_lock_irqsave(&priv->lock, flags); 220ccf6fd6dSAndy Shevchenko 221ccf6fd6dSAndy Shevchenko if (type & IRQ_TYPE_EDGE_RISING) 222ccf6fd6dSAndy Shevchenko value = readl(grer) | BIT(gpio % 32); 223ccf6fd6dSAndy Shevchenko else 224ccf6fd6dSAndy Shevchenko value = readl(grer) & ~BIT(gpio % 32); 225ccf6fd6dSAndy Shevchenko writel(value, grer); 226ccf6fd6dSAndy Shevchenko 227ccf6fd6dSAndy Shevchenko if (type & IRQ_TYPE_EDGE_FALLING) 228ccf6fd6dSAndy Shevchenko value = readl(gfer) | BIT(gpio % 32); 229ccf6fd6dSAndy Shevchenko else 230ccf6fd6dSAndy Shevchenko value = readl(gfer) & ~BIT(gpio % 32); 231ccf6fd6dSAndy Shevchenko writel(value, gfer); 232ccf6fd6dSAndy Shevchenko 233ccf6fd6dSAndy Shevchenko /* 234ccf6fd6dSAndy Shevchenko * To prevent glitches from triggering an unintended level interrupt, 235ccf6fd6dSAndy Shevchenko * configure GLPR register first and then configure GITR. 236ccf6fd6dSAndy Shevchenko */ 237ccf6fd6dSAndy Shevchenko if (type & IRQ_TYPE_LEVEL_LOW) 238ccf6fd6dSAndy Shevchenko value = readl(glpr) | BIT(gpio % 32); 239ccf6fd6dSAndy Shevchenko else 240ccf6fd6dSAndy Shevchenko value = readl(glpr) & ~BIT(gpio % 32); 241ccf6fd6dSAndy Shevchenko writel(value, glpr); 242ccf6fd6dSAndy Shevchenko 243ccf6fd6dSAndy Shevchenko if (type & IRQ_TYPE_LEVEL_MASK) { 244ccf6fd6dSAndy Shevchenko value = readl(gitr) | BIT(gpio % 32); 245ccf6fd6dSAndy Shevchenko writel(value, gitr); 246ccf6fd6dSAndy Shevchenko 247ccf6fd6dSAndy Shevchenko irq_set_handler_locked(d, handle_level_irq); 248ccf6fd6dSAndy Shevchenko } else if (type & IRQ_TYPE_EDGE_BOTH) { 249ccf6fd6dSAndy Shevchenko value = readl(gitr) & ~BIT(gpio % 32); 250ccf6fd6dSAndy Shevchenko writel(value, gitr); 251ccf6fd6dSAndy Shevchenko 252ccf6fd6dSAndy Shevchenko irq_set_handler_locked(d, handle_edge_irq); 253ccf6fd6dSAndy Shevchenko } 254ccf6fd6dSAndy Shevchenko 255ccf6fd6dSAndy Shevchenko raw_spin_unlock_irqrestore(&priv->lock, flags); 256ccf6fd6dSAndy Shevchenko 257ccf6fd6dSAndy Shevchenko return 0; 258ccf6fd6dSAndy Shevchenko } 259ccf6fd6dSAndy Shevchenko 260ccf6fd6dSAndy Shevchenko static int mrfld_irq_set_wake(struct irq_data *d, unsigned int on) 261ccf6fd6dSAndy Shevchenko { 262ccf6fd6dSAndy Shevchenko struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 263ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = gpiochip_get_data(gc); 264ccf6fd6dSAndy Shevchenko u32 gpio = irqd_to_hwirq(d); 265ccf6fd6dSAndy Shevchenko void __iomem *gwmr = gpio_reg(&priv->chip, gpio, GWMR); 266ccf6fd6dSAndy Shevchenko void __iomem *gwsr = gpio_reg(&priv->chip, gpio, GWSR); 267ccf6fd6dSAndy Shevchenko unsigned long flags; 268ccf6fd6dSAndy Shevchenko u32 value; 269ccf6fd6dSAndy Shevchenko 270ccf6fd6dSAndy Shevchenko raw_spin_lock_irqsave(&priv->lock, flags); 271ccf6fd6dSAndy Shevchenko 272ccf6fd6dSAndy Shevchenko /* Clear the existing wake status */ 273ccf6fd6dSAndy Shevchenko writel(BIT(gpio % 32), gwsr); 274ccf6fd6dSAndy Shevchenko 275ccf6fd6dSAndy Shevchenko if (on) 276ccf6fd6dSAndy Shevchenko value = readl(gwmr) | BIT(gpio % 32); 277ccf6fd6dSAndy Shevchenko else 278ccf6fd6dSAndy Shevchenko value = readl(gwmr) & ~BIT(gpio % 32); 279ccf6fd6dSAndy Shevchenko writel(value, gwmr); 280ccf6fd6dSAndy Shevchenko 281ccf6fd6dSAndy Shevchenko raw_spin_unlock_irqrestore(&priv->lock, flags); 282ccf6fd6dSAndy Shevchenko 283ccf6fd6dSAndy Shevchenko dev_dbg(priv->dev, "%sable wake for gpio %u\n", on ? "en" : "dis", gpio); 284ccf6fd6dSAndy Shevchenko return 0; 285ccf6fd6dSAndy Shevchenko } 286ccf6fd6dSAndy Shevchenko 287ccf6fd6dSAndy Shevchenko static struct irq_chip mrfld_irqchip = { 288ccf6fd6dSAndy Shevchenko .name = "gpio-merrifield", 289ccf6fd6dSAndy Shevchenko .irq_ack = mrfld_irq_ack, 290ccf6fd6dSAndy Shevchenko .irq_mask = mrfld_irq_mask, 291ccf6fd6dSAndy Shevchenko .irq_unmask = mrfld_irq_unmask, 292ccf6fd6dSAndy Shevchenko .irq_set_type = mrfld_irq_set_type, 293ccf6fd6dSAndy Shevchenko .irq_set_wake = mrfld_irq_set_wake, 294ccf6fd6dSAndy Shevchenko }; 295ccf6fd6dSAndy Shevchenko 296ccf6fd6dSAndy Shevchenko static void mrfld_irq_handler(struct irq_desc *desc) 297ccf6fd6dSAndy Shevchenko { 298ccf6fd6dSAndy Shevchenko struct gpio_chip *gc = irq_desc_get_handler_data(desc); 299ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv = gpiochip_get_data(gc); 300ccf6fd6dSAndy Shevchenko struct irq_chip *irqchip = irq_desc_get_chip(desc); 301ccf6fd6dSAndy Shevchenko unsigned long base, gpio; 302ccf6fd6dSAndy Shevchenko 303ccf6fd6dSAndy Shevchenko chained_irq_enter(irqchip, desc); 304ccf6fd6dSAndy Shevchenko 305ccf6fd6dSAndy Shevchenko /* Check GPIO controller to check which pin triggered the interrupt */ 306ccf6fd6dSAndy Shevchenko for (base = 0; base < priv->chip.ngpio; base += 32) { 307ccf6fd6dSAndy Shevchenko void __iomem *gisr = gpio_reg(&priv->chip, base, GISR); 308ccf6fd6dSAndy Shevchenko void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR); 309ccf6fd6dSAndy Shevchenko unsigned long pending, enabled; 310ccf6fd6dSAndy Shevchenko 311ccf6fd6dSAndy Shevchenko pending = readl(gisr); 312ccf6fd6dSAndy Shevchenko enabled = readl(gimr); 313ccf6fd6dSAndy Shevchenko 314ccf6fd6dSAndy Shevchenko /* Only interrupts that are enabled */ 315ccf6fd6dSAndy Shevchenko pending &= enabled; 316ccf6fd6dSAndy Shevchenko 317ccf6fd6dSAndy Shevchenko for_each_set_bit(gpio, &pending, 32) { 318ccf6fd6dSAndy Shevchenko unsigned int irq; 319ccf6fd6dSAndy Shevchenko 320ccf6fd6dSAndy Shevchenko irq = irq_find_mapping(gc->irqdomain, base + gpio); 321ccf6fd6dSAndy Shevchenko generic_handle_irq(irq); 322ccf6fd6dSAndy Shevchenko } 323ccf6fd6dSAndy Shevchenko } 324ccf6fd6dSAndy Shevchenko 325ccf6fd6dSAndy Shevchenko chained_irq_exit(irqchip, desc); 326ccf6fd6dSAndy Shevchenko } 327ccf6fd6dSAndy Shevchenko 328ccf6fd6dSAndy Shevchenko static void mrfld_irq_init_hw(struct mrfld_gpio *priv) 329ccf6fd6dSAndy Shevchenko { 330ccf6fd6dSAndy Shevchenko void __iomem *reg; 331ccf6fd6dSAndy Shevchenko unsigned int base; 332ccf6fd6dSAndy Shevchenko 333ccf6fd6dSAndy Shevchenko for (base = 0; base < priv->chip.ngpio; base += 32) { 334ccf6fd6dSAndy Shevchenko /* Clear the rising-edge detect register */ 335ccf6fd6dSAndy Shevchenko reg = gpio_reg(&priv->chip, base, GRER); 336ccf6fd6dSAndy Shevchenko writel(0, reg); 337ccf6fd6dSAndy Shevchenko /* Clear the falling-edge detect register */ 338ccf6fd6dSAndy Shevchenko reg = gpio_reg(&priv->chip, base, GFER); 339ccf6fd6dSAndy Shevchenko writel(0, reg); 340ccf6fd6dSAndy Shevchenko } 341ccf6fd6dSAndy Shevchenko } 342ccf6fd6dSAndy Shevchenko 343ccf6fd6dSAndy Shevchenko static int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id) 344ccf6fd6dSAndy Shevchenko { 345ccf6fd6dSAndy Shevchenko const struct mrfld_gpio_pinrange *range; 346ccf6fd6dSAndy Shevchenko struct mrfld_gpio *priv; 347ccf6fd6dSAndy Shevchenko u32 gpio_base, irq_base; 348ccf6fd6dSAndy Shevchenko void __iomem *base; 349ccf6fd6dSAndy Shevchenko unsigned int i; 350ccf6fd6dSAndy Shevchenko int retval; 351ccf6fd6dSAndy Shevchenko 352ccf6fd6dSAndy Shevchenko retval = pcim_enable_device(pdev); 353ccf6fd6dSAndy Shevchenko if (retval) 354ccf6fd6dSAndy Shevchenko return retval; 355ccf6fd6dSAndy Shevchenko 356ccf6fd6dSAndy Shevchenko retval = pcim_iomap_regions(pdev, BIT(1) | BIT(0), pci_name(pdev)); 357ccf6fd6dSAndy Shevchenko if (retval) { 358ccf6fd6dSAndy Shevchenko dev_err(&pdev->dev, "I/O memory mapping error\n"); 359ccf6fd6dSAndy Shevchenko return retval; 360ccf6fd6dSAndy Shevchenko } 361ccf6fd6dSAndy Shevchenko 362ccf6fd6dSAndy Shevchenko base = pcim_iomap_table(pdev)[1]; 363ccf6fd6dSAndy Shevchenko 364ccf6fd6dSAndy Shevchenko irq_base = readl(base); 365ccf6fd6dSAndy Shevchenko gpio_base = readl(sizeof(u32) + base); 366ccf6fd6dSAndy Shevchenko 367ccf6fd6dSAndy Shevchenko /* Release the IO mapping, since we already get the info from BAR1 */ 368ccf6fd6dSAndy Shevchenko pcim_iounmap_regions(pdev, BIT(1)); 369ccf6fd6dSAndy Shevchenko 370ccf6fd6dSAndy Shevchenko priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 371ccf6fd6dSAndy Shevchenko if (!priv) { 372ccf6fd6dSAndy Shevchenko dev_err(&pdev->dev, "can't allocate chip data\n"); 373ccf6fd6dSAndy Shevchenko return -ENOMEM; 374ccf6fd6dSAndy Shevchenko } 375ccf6fd6dSAndy Shevchenko 376ccf6fd6dSAndy Shevchenko priv->dev = &pdev->dev; 377ccf6fd6dSAndy Shevchenko priv->reg_base = pcim_iomap_table(pdev)[0]; 378ccf6fd6dSAndy Shevchenko 379ccf6fd6dSAndy Shevchenko priv->chip.label = dev_name(&pdev->dev); 380ccf6fd6dSAndy Shevchenko priv->chip.parent = &pdev->dev; 381ccf6fd6dSAndy Shevchenko priv->chip.request = gpiochip_generic_request; 382ccf6fd6dSAndy Shevchenko priv->chip.free = gpiochip_generic_free; 383ccf6fd6dSAndy Shevchenko priv->chip.direction_input = mrfld_gpio_direction_input; 384ccf6fd6dSAndy Shevchenko priv->chip.direction_output = mrfld_gpio_direction_output; 385ccf6fd6dSAndy Shevchenko priv->chip.get = mrfld_gpio_get; 386ccf6fd6dSAndy Shevchenko priv->chip.set = mrfld_gpio_set; 387ccf6fd6dSAndy Shevchenko priv->chip.base = gpio_base; 388ccf6fd6dSAndy Shevchenko priv->chip.ngpio = MRFLD_NGPIO; 389ccf6fd6dSAndy Shevchenko priv->chip.can_sleep = false; 390ccf6fd6dSAndy Shevchenko 391ccf6fd6dSAndy Shevchenko raw_spin_lock_init(&priv->lock); 392ccf6fd6dSAndy Shevchenko 393ccf6fd6dSAndy Shevchenko pci_set_drvdata(pdev, priv); 394ccf6fd6dSAndy Shevchenko retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv); 395ccf6fd6dSAndy Shevchenko if (retval) { 396ccf6fd6dSAndy Shevchenko dev_err(&pdev->dev, "gpiochip_add error %d\n", retval); 397ccf6fd6dSAndy Shevchenko return retval; 398ccf6fd6dSAndy Shevchenko } 399ccf6fd6dSAndy Shevchenko 400ccf6fd6dSAndy Shevchenko for (i = 0; i < ARRAY_SIZE(mrfld_gpio_ranges); i++) { 401ccf6fd6dSAndy Shevchenko range = &mrfld_gpio_ranges[i]; 402ccf6fd6dSAndy Shevchenko retval = gpiochip_add_pin_range(&priv->chip, 403ccf6fd6dSAndy Shevchenko "pinctrl-merrifield", 404ccf6fd6dSAndy Shevchenko range->gpio_base, 405ccf6fd6dSAndy Shevchenko range->pin_base, 406ccf6fd6dSAndy Shevchenko range->npins); 407ccf6fd6dSAndy Shevchenko if (retval) { 408ccf6fd6dSAndy Shevchenko dev_err(&pdev->dev, "failed to add GPIO pin range\n"); 409ccf6fd6dSAndy Shevchenko return retval; 410ccf6fd6dSAndy Shevchenko } 411ccf6fd6dSAndy Shevchenko } 412ccf6fd6dSAndy Shevchenko 413ccf6fd6dSAndy Shevchenko retval = gpiochip_irqchip_add(&priv->chip, &mrfld_irqchip, irq_base, 414ccf6fd6dSAndy Shevchenko handle_simple_irq, IRQ_TYPE_NONE); 415ccf6fd6dSAndy Shevchenko if (retval) { 416ccf6fd6dSAndy Shevchenko dev_err(&pdev->dev, "could not connect irqchip to gpiochip\n"); 417ccf6fd6dSAndy Shevchenko return retval; 418ccf6fd6dSAndy Shevchenko } 419ccf6fd6dSAndy Shevchenko 420ccf6fd6dSAndy Shevchenko mrfld_irq_init_hw(priv); 421ccf6fd6dSAndy Shevchenko 422ccf6fd6dSAndy Shevchenko gpiochip_set_chained_irqchip(&priv->chip, &mrfld_irqchip, pdev->irq, 423ccf6fd6dSAndy Shevchenko mrfld_irq_handler); 424ccf6fd6dSAndy Shevchenko 425ccf6fd6dSAndy Shevchenko return 0; 426ccf6fd6dSAndy Shevchenko } 427ccf6fd6dSAndy Shevchenko 428ccf6fd6dSAndy Shevchenko static const struct pci_device_id mrfld_gpio_ids[] = { 429ccf6fd6dSAndy Shevchenko { PCI_VDEVICE(INTEL, 0x1199) }, 430ccf6fd6dSAndy Shevchenko { } 431ccf6fd6dSAndy Shevchenko }; 432ccf6fd6dSAndy Shevchenko MODULE_DEVICE_TABLE(pci, mrfld_gpio_ids); 433ccf6fd6dSAndy Shevchenko 434ccf6fd6dSAndy Shevchenko static struct pci_driver mrfld_gpio_driver = { 435ccf6fd6dSAndy Shevchenko .name = "gpio-merrifield", 436ccf6fd6dSAndy Shevchenko .id_table = mrfld_gpio_ids, 437ccf6fd6dSAndy Shevchenko .probe = mrfld_gpio_probe, 438ccf6fd6dSAndy Shevchenko }; 439ccf6fd6dSAndy Shevchenko 440ccf6fd6dSAndy Shevchenko module_pci_driver(mrfld_gpio_driver); 441ccf6fd6dSAndy Shevchenko 442ccf6fd6dSAndy Shevchenko MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>"); 443ccf6fd6dSAndy Shevchenko MODULE_DESCRIPTION("Intel Merrifield SoC GPIO driver"); 444ccf6fd6dSAndy Shevchenko MODULE_LICENSE("GPL v2"); 445