| Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 1 | /* | 
|  | 2 | * Moorestown platform Langwell chip GPIO driver | 
|  | 3 | * | 
| Andy Shevchenko | 611a485 | 2013-05-22 13:20:14 +0300 | [diff] [blame] | 4 | * Copyright (c) 2008, 2009, 2013, Intel Corporation. | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 5 | * | 
|  | 6 | * This program is free software; you can redistribute it and/or modify | 
|  | 7 | * it under the terms of the GNU General Public License version 2 as | 
|  | 8 | * published by the Free Software Foundation. | 
|  | 9 | * | 
|  | 10 | * This program is distributed in the hope that it will be useful, | 
|  | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 13 | * GNU General Public License for more details. | 
|  | 14 | * | 
|  | 15 | * You should have received a copy of the GNU General Public License | 
|  | 16 | * along with this program; if not, write to the Free Software | 
|  | 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 18 | */ | 
|  | 19 |  | 
|  | 20 | /* Supports: | 
|  | 21 | * Moorestown platform Langwell chip. | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 22 | * Medfield platform Penwell chip. | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 23 | */ | 
|  | 24 |  | 
|  | 25 | #include <linux/module.h> | 
|  | 26 | #include <linux/pci.h> | 
| Alan Cox | 72b4379 | 2010-10-27 15:33:23 -0700 | [diff] [blame] | 27 | #include <linux/platform_device.h> | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 28 | #include <linux/kernel.h> | 
|  | 29 | #include <linux/delay.h> | 
|  | 30 | #include <linux/stddef.h> | 
|  | 31 | #include <linux/interrupt.h> | 
|  | 32 | #include <linux/init.h> | 
|  | 33 | #include <linux/irq.h> | 
|  | 34 | #include <linux/io.h> | 
|  | 35 | #include <linux/gpio.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> | 
| Mika Westerberg | 465f2bd | 2012-05-02 11:15:50 +0300 | [diff] [blame] | 38 | #include <linux/irqdomain.h> | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 39 |  | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 40 | /* | 
|  | 41 | * Langwell chip has 64 pins and thus there are 2 32bit registers to control | 
|  | 42 | * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit | 
|  | 43 | * registers to control them, so we only define the order here instead of a | 
|  | 44 | * structure, to get a bit offset for a pin (use GPDR as an example): | 
|  | 45 | * | 
|  | 46 | * nreg = ngpio / 32; | 
|  | 47 | * reg = offset / 32; | 
|  | 48 | * bit = offset % 32; | 
|  | 49 | * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4; | 
|  | 50 | * | 
|  | 51 | * so the bit of reg_addr is to control pin offset's GPDR feature | 
|  | 52 | */ | 
|  | 53 |  | 
|  | 54 | enum GPIO_REG { | 
|  | 55 | GPLR = 0,	/* pin level read-only */ | 
|  | 56 | GPDR,		/* pin direction */ | 
|  | 57 | GPSR,		/* pin set */ | 
|  | 58 | GPCR,		/* pin clear */ | 
|  | 59 | GRER,		/* rising edge detect */ | 
|  | 60 | GFER,		/* falling edge detect */ | 
|  | 61 | GEDR,		/* edge detect result */ | 
| Adrian Hunter | 8c0f7b1 | 2011-10-03 14:36:07 +0300 | [diff] [blame] | 62 | GAFR,		/* alt function */ | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 63 | }; | 
|  | 64 |  | 
|  | 65 | struct lnw_gpio { | 
|  | 66 | struct gpio_chip		chip; | 
| Andy Shevchenko | 64c8cbc | 2013-05-22 13:20:11 +0300 | [diff] [blame] | 67 | void __iomem			*reg_base; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 68 | spinlock_t			lock; | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 69 | struct pci_dev			*pdev; | 
| Mika Westerberg | 465f2bd | 2012-05-02 11:15:50 +0300 | [diff] [blame] | 70 | struct irq_domain		*domain; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 71 | }; | 
|  | 72 |  | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 73 | #define to_lnw_priv(chip)	container_of(chip, struct lnw_gpio, chip) | 
|  | 74 |  | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 75 | static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset, | 
| Andy Shevchenko | 611a485 | 2013-05-22 13:20:14 +0300 | [diff] [blame] | 76 | enum GPIO_REG reg_type) | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 77 | { | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 78 | struct lnw_gpio *lnw = to_lnw_priv(chip); | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 79 | unsigned nreg = chip->ngpio / 32; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 80 | u8 reg = offset / 32; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 81 |  | 
| Andy Shevchenko | 611a485 | 2013-05-22 13:20:14 +0300 | [diff] [blame] | 82 | return lnw->reg_base + reg_type * nreg * 4 + reg * 4; | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Adrian Hunter | 8c0f7b1 | 2011-10-03 14:36:07 +0300 | [diff] [blame] | 85 | static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset, | 
|  | 86 | enum GPIO_REG reg_type) | 
|  | 87 | { | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 88 | struct lnw_gpio *lnw = to_lnw_priv(chip); | 
| Adrian Hunter | 8c0f7b1 | 2011-10-03 14:36:07 +0300 | [diff] [blame] | 89 | unsigned nreg = chip->ngpio / 32; | 
|  | 90 | u8 reg = offset / 16; | 
| Adrian Hunter | 8c0f7b1 | 2011-10-03 14:36:07 +0300 | [diff] [blame] | 91 |  | 
| Andy Shevchenko | 611a485 | 2013-05-22 13:20:14 +0300 | [diff] [blame] | 92 | return lnw->reg_base + reg_type * nreg * 4 + reg * 4; | 
| Adrian Hunter | 8c0f7b1 | 2011-10-03 14:36:07 +0300 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | static int lnw_gpio_request(struct gpio_chip *chip, unsigned offset) | 
|  | 96 | { | 
|  | 97 | void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR); | 
|  | 98 | u32 value = readl(gafr); | 
|  | 99 | int shift = (offset % 16) << 1, af = (value >> shift) & 3; | 
|  | 100 |  | 
|  | 101 | if (af) { | 
|  | 102 | value &= ~(3 << shift); | 
|  | 103 | writel(value, gafr); | 
|  | 104 | } | 
|  | 105 | return 0; | 
|  | 106 | } | 
|  | 107 |  | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 108 | static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset) | 
|  | 109 | { | 
|  | 110 | void __iomem *gplr = gpio_reg(chip, offset, GPLR); | 
|  | 111 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 112 | return readl(gplr) & BIT(offset % 32); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value) | 
|  | 116 | { | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 117 | void __iomem *gpsr, *gpcr; | 
|  | 118 |  | 
|  | 119 | if (value) { | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 120 | gpsr = gpio_reg(chip, offset, GPSR); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 121 | writel(BIT(offset % 32), gpsr); | 
|  | 122 | } else { | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 123 | gpcr = gpio_reg(chip, offset, GPCR); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 124 | writel(BIT(offset % 32), gpcr); | 
|  | 125 | } | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset) | 
|  | 129 | { | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 130 | struct lnw_gpio *lnw = to_lnw_priv(chip); | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 131 | void __iomem *gpdr = gpio_reg(chip, offset, GPDR); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 132 | u32 value; | 
|  | 133 | unsigned long flags; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 134 |  | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 135 | if (lnw->pdev) | 
|  | 136 | pm_runtime_get(&lnw->pdev->dev); | 
|  | 137 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 138 | spin_lock_irqsave(&lnw->lock, flags); | 
|  | 139 | value = readl(gpdr); | 
|  | 140 | value &= ~BIT(offset % 32); | 
|  | 141 | writel(value, gpdr); | 
|  | 142 | spin_unlock_irqrestore(&lnw->lock, flags); | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 143 |  | 
|  | 144 | if (lnw->pdev) | 
|  | 145 | pm_runtime_put(&lnw->pdev->dev); | 
|  | 146 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 147 | return 0; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static int lnw_gpio_direction_output(struct gpio_chip *chip, | 
|  | 151 | unsigned offset, int value) | 
|  | 152 | { | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 153 | struct lnw_gpio *lnw = to_lnw_priv(chip); | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 154 | void __iomem *gpdr = gpio_reg(chip, offset, GPDR); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 155 | unsigned long flags; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | lnw_gpio_set(chip, offset, value); | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 158 |  | 
|  | 159 | if (lnw->pdev) | 
|  | 160 | pm_runtime_get(&lnw->pdev->dev); | 
|  | 161 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 162 | spin_lock_irqsave(&lnw->lock, flags); | 
|  | 163 | value = readl(gpdr); | 
| Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 164 | value |= BIT(offset % 32); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 165 | writel(value, gpdr); | 
|  | 166 | spin_unlock_irqrestore(&lnw->lock, flags); | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 167 |  | 
|  | 168 | if (lnw->pdev) | 
|  | 169 | pm_runtime_put(&lnw->pdev->dev); | 
|  | 170 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 171 | return 0; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset) | 
|  | 175 | { | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 176 | struct lnw_gpio *lnw = to_lnw_priv(chip); | 
| Mika Westerberg | 465f2bd | 2012-05-02 11:15:50 +0300 | [diff] [blame] | 177 | return irq_create_mapping(lnw->domain, offset); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
| Lennert Buytenhek | 5ffd72c | 2011-01-12 17:00:13 -0800 | [diff] [blame] | 180 | static int lnw_irq_type(struct irq_data *d, unsigned type) | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 181 | { | 
| Lennert Buytenhek | 5ffd72c | 2011-01-12 17:00:13 -0800 | [diff] [blame] | 182 | struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d); | 
| Mika Westerberg | 465f2bd | 2012-05-02 11:15:50 +0300 | [diff] [blame] | 183 | u32 gpio = irqd_to_hwirq(d); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 184 | unsigned long flags; | 
|  | 185 | u32 value; | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 186 | void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER); | 
|  | 187 | void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 188 |  | 
| Roel Kluin | 4efec62 | 2009-12-15 16:46:18 -0800 | [diff] [blame] | 189 | if (gpio >= lnw->chip.ngpio) | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 190 | return -EINVAL; | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 191 |  | 
|  | 192 | if (lnw->pdev) | 
|  | 193 | pm_runtime_get(&lnw->pdev->dev); | 
|  | 194 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 195 | spin_lock_irqsave(&lnw->lock, flags); | 
|  | 196 | if (type & IRQ_TYPE_EDGE_RISING) | 
|  | 197 | value = readl(grer) | BIT(gpio % 32); | 
|  | 198 | else | 
|  | 199 | value = readl(grer) & (~BIT(gpio % 32)); | 
|  | 200 | writel(value, grer); | 
|  | 201 |  | 
|  | 202 | if (type & IRQ_TYPE_EDGE_FALLING) | 
|  | 203 | value = readl(gfer) | BIT(gpio % 32); | 
|  | 204 | else | 
|  | 205 | value = readl(gfer) & (~BIT(gpio % 32)); | 
|  | 206 | writel(value, gfer); | 
|  | 207 | spin_unlock_irqrestore(&lnw->lock, flags); | 
|  | 208 |  | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 209 | if (lnw->pdev) | 
|  | 210 | pm_runtime_put(&lnw->pdev->dev); | 
|  | 211 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 212 | return 0; | 
| Andrew Morton | fd0574c | 2010-10-27 15:33:22 -0700 | [diff] [blame] | 213 | } | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 214 |  | 
| Lennert Buytenhek | 5ffd72c | 2011-01-12 17:00:13 -0800 | [diff] [blame] | 215 | static void lnw_irq_unmask(struct irq_data *d) | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 216 | { | 
| Andrew Morton | fd0574c | 2010-10-27 15:33:22 -0700 | [diff] [blame] | 217 | } | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 218 |  | 
| Lennert Buytenhek | 5ffd72c | 2011-01-12 17:00:13 -0800 | [diff] [blame] | 219 | static void lnw_irq_mask(struct irq_data *d) | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 220 | { | 
| Andrew Morton | fd0574c | 2010-10-27 15:33:22 -0700 | [diff] [blame] | 221 | } | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 222 |  | 
|  | 223 | static struct irq_chip lnw_irqchip = { | 
|  | 224 | .name		= "LNW-GPIO", | 
| Lennert Buytenhek | 5ffd72c | 2011-01-12 17:00:13 -0800 | [diff] [blame] | 225 | .irq_mask	= lnw_irq_mask, | 
|  | 226 | .irq_unmask	= lnw_irq_unmask, | 
|  | 227 | .irq_set_type	= lnw_irq_type, | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 228 | }; | 
|  | 229 |  | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 230 | static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = {   /* pin number */ | 
|  | 231 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f), .driver_data = 64 }, | 
|  | 232 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f), .driver_data = 96 }, | 
|  | 233 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a), .driver_data = 96 }, | 
| David Cohen | 936cb1b | 2012-12-18 17:52:12 -0800 | [diff] [blame] | 234 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb), .driver_data = 96 }, | 
|  | 235 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7), .driver_data = 96 }, | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 236 | { 0, } | 
|  | 237 | }; | 
|  | 238 | MODULE_DEVICE_TABLE(pci, lnw_gpio_ids); | 
|  | 239 |  | 
|  | 240 | static void lnw_irq_handler(unsigned irq, struct irq_desc *desc) | 
|  | 241 | { | 
| Thomas Gleixner | 20e2aa9 | 2011-03-17 19:32:49 +0000 | [diff] [blame] | 242 | struct irq_data *data = irq_desc_get_irq_data(desc); | 
|  | 243 | struct lnw_gpio *lnw = irq_data_get_irq_handler_data(data); | 
|  | 244 | struct irq_chip *chip = irq_data_get_irq_chip(data); | 
| Thomas Gleixner | 84bead6 | 2011-03-17 19:32:58 +0000 | [diff] [blame] | 245 | u32 base, gpio, mask; | 
| Thomas Gleixner | 732063b | 2011-03-17 19:32:55 +0000 | [diff] [blame] | 246 | unsigned long pending; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 247 | void __iomem *gedr; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 248 |  | 
|  | 249 | /* check GPIO controller to check which pin triggered the interrupt */ | 
| Alek Du | 8081c84 | 2010-05-26 14:42:25 -0700 | [diff] [blame] | 250 | for (base = 0; base < lnw->chip.ngpio; base += 32) { | 
|  | 251 | gedr = gpio_reg(&lnw->chip, base, GEDR); | 
| Mika Westerberg | c8f925b | 2012-05-10 13:01:22 +0300 | [diff] [blame] | 252 | while ((pending = readl(gedr))) { | 
| Mathias Nyman | 2345b20 | 2011-07-08 10:02:18 +0100 | [diff] [blame] | 253 | gpio = __ffs(pending); | 
| Thomas Gleixner | 84bead6 | 2011-03-17 19:32:58 +0000 | [diff] [blame] | 254 | mask = BIT(gpio); | 
| Thomas Gleixner | 84bead6 | 2011-03-17 19:32:58 +0000 | [diff] [blame] | 255 | /* Clear before handling so we can't lose an edge */ | 
|  | 256 | writel(mask, gedr); | 
| Mika Westerberg | 465f2bd | 2012-05-02 11:15:50 +0300 | [diff] [blame] | 257 | generic_handle_irq(irq_find_mapping(lnw->domain, | 
|  | 258 | base + gpio)); | 
| Thomas Gleixner | 732063b | 2011-03-17 19:32:55 +0000 | [diff] [blame] | 259 | } | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 260 | } | 
| Feng Tang | 0766d20 | 2011-01-25 15:07:15 -0800 | [diff] [blame] | 261 |  | 
| Thomas Gleixner | 20e2aa9 | 2011-03-17 19:32:49 +0000 | [diff] [blame] | 262 | chip->irq_eoi(data); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 263 | } | 
|  | 264 |  | 
| Mika Westerberg | f5f9311 | 2012-04-05 12:15:17 +0300 | [diff] [blame] | 265 | static void lnw_irq_init_hw(struct lnw_gpio *lnw) | 
|  | 266 | { | 
|  | 267 | void __iomem *reg; | 
|  | 268 | unsigned base; | 
|  | 269 |  | 
|  | 270 | for (base = 0; base < lnw->chip.ngpio; base += 32) { | 
|  | 271 | /* Clear the rising-edge detect register */ | 
|  | 272 | reg = gpio_reg(&lnw->chip, base, GRER); | 
|  | 273 | writel(0, reg); | 
|  | 274 | /* Clear the falling-edge detect register */ | 
|  | 275 | reg = gpio_reg(&lnw->chip, base, GFER); | 
|  | 276 | writel(0, reg); | 
|  | 277 | /* Clear the edge detect status register */ | 
|  | 278 | reg = gpio_reg(&lnw->chip, base, GEDR); | 
|  | 279 | writel(~0, reg); | 
|  | 280 | } | 
|  | 281 | } | 
|  | 282 |  | 
| Mika Westerberg | 465f2bd | 2012-05-02 11:15:50 +0300 | [diff] [blame] | 283 | static int lnw_gpio_irq_map(struct irq_domain *d, unsigned int virq, | 
|  | 284 | irq_hw_number_t hw) | 
|  | 285 | { | 
|  | 286 | struct lnw_gpio *lnw = d->host_data; | 
|  | 287 |  | 
|  | 288 | irq_set_chip_and_handler_name(virq, &lnw_irqchip, handle_simple_irq, | 
|  | 289 | "demux"); | 
|  | 290 | irq_set_chip_data(virq, lnw); | 
|  | 291 | irq_set_irq_type(virq, IRQ_TYPE_NONE); | 
|  | 292 |  | 
|  | 293 | return 0; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | static const struct irq_domain_ops lnw_gpio_irq_ops = { | 
|  | 297 | .map = lnw_gpio_irq_map, | 
|  | 298 | .xlate = irq_domain_xlate_twocell, | 
|  | 299 | }; | 
|  | 300 |  | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 301 | static int lnw_gpio_runtime_idle(struct device *dev) | 
|  | 302 | { | 
| Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 303 | pm_schedule_suspend(dev, 500); | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 304 | return -EBUSY; | 
|  | 305 | } | 
|  | 306 |  | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 307 | static const struct dev_pm_ops lnw_gpio_pm_ops = { | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 308 | SET_RUNTIME_PM_OPS(NULL, NULL, lnw_gpio_runtime_idle) | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 309 | }; | 
|  | 310 |  | 
| Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 311 | static int lnw_gpio_probe(struct pci_dev *pdev, | 
| Andy Shevchenko | 64c8cbc | 2013-05-22 13:20:11 +0300 | [diff] [blame] | 312 | const struct pci_device_id *id) | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 313 | { | 
| Andy Shevchenko | 64c8cbc | 2013-05-22 13:20:11 +0300 | [diff] [blame] | 314 | void __iomem *base; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 315 | struct lnw_gpio *lnw; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 316 | u32 gpio_base; | 
| David Cohen | 2519f9a | 2013-05-06 16:11:03 -0700 | [diff] [blame] | 317 | u32 irq_base; | 
| Julia Lawall | d6a2b7b | 2012-08-05 11:52:34 +0200 | [diff] [blame] | 318 | int retval; | 
| Mika Westerberg | b3e35af | 2012-04-05 12:15:16 +0300 | [diff] [blame] | 319 | int ngpio = id->driver_data; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 320 |  | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 321 | retval = pcim_enable_device(pdev); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 322 | if (retval) | 
| Mika Westerberg | 8302c74 | 2012-04-05 12:15:15 +0300 | [diff] [blame] | 323 | return retval; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 324 |  | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 325 | retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev)); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 326 | if (retval) { | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 327 | dev_err(&pdev->dev, "I/O memory mapping error\n"); | 
|  | 328 | return retval; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 329 | } | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 330 |  | 
|  | 331 | base = pcim_iomap_table(pdev)[1]; | 
| Andy Shevchenko | 64c8cbc | 2013-05-22 13:20:11 +0300 | [diff] [blame] | 332 |  | 
|  | 333 | irq_base = readl(base); | 
|  | 334 | gpio_base = readl(sizeof(u32) + base); | 
|  | 335 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 336 | /* release the IO mapping, since we already get the info from bar1 */ | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 337 | pcim_iounmap_regions(pdev, 1 << 1); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 338 |  | 
| David Cohen | 46ebfbc | 2012-12-20 14:45:51 -0800 | [diff] [blame] | 339 | lnw = devm_kzalloc(&pdev->dev, sizeof(*lnw), GFP_KERNEL); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 340 | if (!lnw) { | 
| Andy Shevchenko | 8aca119 | 2013-05-22 13:20:13 +0300 | [diff] [blame] | 341 | dev_err(&pdev->dev, "can't allocate chip data\n"); | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 342 | return -ENOMEM; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 343 | } | 
| Mika Westerberg | b3e35af | 2012-04-05 12:15:16 +0300 | [diff] [blame] | 344 |  | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 345 | lnw->reg_base = pcim_iomap_table(pdev)[0]; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 346 | lnw->chip.label = dev_name(&pdev->dev); | 
| Adrian Hunter | 8c0f7b1 | 2011-10-03 14:36:07 +0300 | [diff] [blame] | 347 | lnw->chip.request = lnw_gpio_request; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 348 | lnw->chip.direction_input = lnw_gpio_direction_input; | 
|  | 349 | lnw->chip.direction_output = lnw_gpio_direction_output; | 
|  | 350 | lnw->chip.get = lnw_gpio_get; | 
|  | 351 | lnw->chip.set = lnw_gpio_set; | 
|  | 352 | lnw->chip.to_irq = lnw_gpio_to_irq; | 
|  | 353 | lnw->chip.base = gpio_base; | 
| Mika Westerberg | b3e35af | 2012-04-05 12:15:16 +0300 | [diff] [blame] | 354 | lnw->chip.ngpio = ngpio; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 355 | lnw->chip.can_sleep = 0; | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 356 | lnw->pdev = pdev; | 
| David Cohen | 2519f9a | 2013-05-06 16:11:03 -0700 | [diff] [blame] | 357 |  | 
| Andy Shevchenko | aeb168f | 2013-05-22 13:20:10 +0300 | [diff] [blame] | 358 | spin_lock_init(&lnw->lock); | 
|  | 359 |  | 
| David Cohen | 2519f9a | 2013-05-06 16:11:03 -0700 | [diff] [blame] | 360 | lnw->domain = irq_domain_add_simple(pdev->dev.of_node, ngpio, irq_base, | 
|  | 361 | &lnw_gpio_irq_ops, lnw); | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 362 | if (!lnw->domain) | 
|  | 363 | return -ENOMEM; | 
| David Cohen | 2519f9a | 2013-05-06 16:11:03 -0700 | [diff] [blame] | 364 |  | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 365 | pci_set_drvdata(pdev, lnw); | 
|  | 366 | retval = gpiochip_add(&lnw->chip); | 
|  | 367 | if (retval) { | 
| Andy Shevchenko | 8aca119 | 2013-05-22 13:20:13 +0300 | [diff] [blame] | 368 | dev_err(&pdev->dev, "gpiochip_add error %d\n", retval); | 
| Andy Shevchenko | 786e07e | 2013-05-22 13:20:12 +0300 | [diff] [blame] | 369 | return retval; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 370 | } | 
| Mika Westerberg | f5f9311 | 2012-04-05 12:15:17 +0300 | [diff] [blame] | 371 |  | 
|  | 372 | lnw_irq_init_hw(lnw); | 
|  | 373 |  | 
| Thomas Gleixner | 674db90 | 2011-03-17 19:32:52 +0000 | [diff] [blame] | 374 | irq_set_handler_data(pdev->irq, lnw); | 
|  | 375 | irq_set_chained_handler(pdev->irq, lnw_irq_handler); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 376 |  | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 377 | pm_runtime_put_noidle(&pdev->dev); | 
|  | 378 | pm_runtime_allow(&pdev->dev); | 
|  | 379 |  | 
| Mika Westerberg | 8302c74 | 2012-04-05 12:15:15 +0300 | [diff] [blame] | 380 | return 0; | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 381 | } | 
|  | 382 |  | 
|  | 383 | static struct pci_driver lnw_gpio_driver = { | 
|  | 384 | .name		= "langwell_gpio", | 
|  | 385 | .id_table	= lnw_gpio_ids, | 
|  | 386 | .probe		= lnw_gpio_probe, | 
| Kristen Carlson Accardi | 7812803 | 2011-05-10 14:23:45 +0100 | [diff] [blame] | 387 | .driver		= { | 
|  | 388 | .pm	= &lnw_gpio_pm_ops, | 
|  | 389 | }, | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 390 | }; | 
|  | 391 |  | 
|  | 392 | static int __init lnw_gpio_init(void) | 
|  | 393 | { | 
| Andy Shevchenko | 10b20a9 | 2013-06-17 16:57:06 +0300 | [diff] [blame] | 394 | return pci_register_driver(&lnw_gpio_driver); | 
| Alek Du | 8bf0261 | 2009-09-22 16:46:36 -0700 | [diff] [blame] | 395 | } | 
|  | 396 |  | 
|  | 397 | device_initcall(lnw_gpio_init); |