blob: 4e803baf980e63fb34820238ebd3a8ceb6c8d6d8 [file] [log] [blame]
Andy Shevchenko917842f2018-11-06 14:11:42 +02001// SPDX-License-Identifier: GPL-2.0
Grant Likelyc103de22011-06-04 18:38:28 -06002/*
David Cohena0bbf032014-01-17 07:30:01 -08003 * Intel MID GPIO driver
Grant Likelyc103de22011-06-04 18:38:28 -06004 *
Andy Shevchenko3cabe872016-07-06 12:50:13 +03005 * Copyright (c) 2008-2014,2016 Intel Corporation.
Alek Du8bf02612009-09-22 16:46:36 -07006 */
7
8/* Supports:
9 * Moorestown platform Langwell chip.
Alek Du8081c842010-05-26 14:42:25 -070010 * Medfield platform Penwell chip.
David Cohenf89a7682013-10-04 13:01:42 -070011 * Clovertrail platform Cloverview chip.
Alek Du8bf02612009-09-22 16:46:36 -070012 */
13
Andy Shevchenko3cabe872016-07-06 12:50:13 +030014#include <linux/delay.h>
Andy Shevchenkoddc53c42018-09-04 14:26:25 +030015#include <linux/gpio/driver.h>
Andy Shevchenko3cabe872016-07-06 12:50:13 +030016#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
Andy Shevchenko3cabe872016-07-06 12:50:13 +030019#include <linux/kernel.h>
Alek Du8bf02612009-09-22 16:46:36 -070020#include <linux/pci.h>
Alan Cox72b43792010-10-27 15:33:23 -070021#include <linux/platform_device.h>
Kristen Carlson Accardi78128032011-05-10 14:23:45 +010022#include <linux/pm_runtime.h>
Andy Shevchenko3cabe872016-07-06 12:50:13 +030023#include <linux/slab.h>
24#include <linux/stddef.h>
Alek Du8bf02612009-09-22 16:46:36 -070025
David Cohenf89a7682013-10-04 13:01:42 -070026#define INTEL_MID_IRQ_TYPE_EDGE (1 << 0)
27#define INTEL_MID_IRQ_TYPE_LEVEL (1 << 1)
David Cohend56d6b32013-10-04 13:01:40 -070028
Alek Du8081c842010-05-26 14:42:25 -070029/*
30 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
31 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
32 * registers to control them, so we only define the order here instead of a
33 * structure, to get a bit offset for a pin (use GPDR as an example):
34 *
35 * nreg = ngpio / 32;
36 * reg = offset / 32;
37 * bit = offset % 32;
38 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
39 *
40 * so the bit of reg_addr is to control pin offset's GPDR feature
41*/
42
43enum GPIO_REG {
44 GPLR = 0, /* pin level read-only */
45 GPDR, /* pin direction */
46 GPSR, /* pin set */
47 GPCR, /* pin clear */
48 GRER, /* rising edge detect */
49 GFER, /* falling edge detect */
50 GEDR, /* edge detect result */
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030051 GAFR, /* alt function */
Alek Du8bf02612009-09-22 16:46:36 -070052};
53
David Cohenf89a7682013-10-04 13:01:42 -070054/* intel_mid gpio driver data */
55struct intel_mid_gpio_ddata {
David Cohend56d6b32013-10-04 13:01:40 -070056 u16 ngpio; /* number of gpio pins */
David Cohend56d6b32013-10-04 13:01:40 -070057 u32 chip_irq_type; /* chip interrupt type */
58};
59
David Cohenf89a7682013-10-04 13:01:42 -070060struct intel_mid_gpio {
Alek Du8bf02612009-09-22 16:46:36 -070061 struct gpio_chip chip;
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +030062 void __iomem *reg_base;
Alek Du8bf02612009-09-22 16:46:36 -070063 spinlock_t lock;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +010064 struct pci_dev *pdev;
Alek Du8bf02612009-09-22 16:46:36 -070065};
66
Alek Du8081c842010-05-26 14:42:25 -070067static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
Andy Shevchenko611a4852013-05-22 13:20:14 +030068 enum GPIO_REG reg_type)
Alek Du8bf02612009-09-22 16:46:36 -070069{
Linus Walleij5c77c022015-12-06 10:55:28 +010070 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Alek Du8081c842010-05-26 14:42:25 -070071 unsigned nreg = chip->ngpio / 32;
Alek Du8bf02612009-09-22 16:46:36 -070072 u8 reg = offset / 32;
Alek Du8bf02612009-09-22 16:46:36 -070073
David Cohenf89a7682013-10-04 13:01:42 -070074 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
Alek Du8081c842010-05-26 14:42:25 -070075}
76
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030077static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
78 enum GPIO_REG reg_type)
79{
Linus Walleij5c77c022015-12-06 10:55:28 +010080 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030081 unsigned nreg = chip->ngpio / 32;
82 u8 reg = offset / 16;
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030083
David Cohenf89a7682013-10-04 13:01:42 -070084 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030085}
86
David Cohenf89a7682013-10-04 13:01:42 -070087static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030088{
89 void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
90 u32 value = readl(gafr);
91 int shift = (offset % 16) << 1, af = (value >> shift) & 3;
92
93 if (af) {
94 value &= ~(3 << shift);
95 writel(value, gafr);
96 }
97 return 0;
98}
99
David Cohenf89a7682013-10-04 13:01:42 -0700100static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
Alek Du8081c842010-05-26 14:42:25 -0700101{
102 void __iomem *gplr = gpio_reg(chip, offset, GPLR);
103
Linus Walleij4c628f32015-12-21 11:00:56 +0100104 return !!(readl(gplr) & BIT(offset % 32));
Alek Du8bf02612009-09-22 16:46:36 -0700105}
106
David Cohenf89a7682013-10-04 13:01:42 -0700107static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
Alek Du8bf02612009-09-22 16:46:36 -0700108{
Alek Du8bf02612009-09-22 16:46:36 -0700109 void __iomem *gpsr, *gpcr;
110
111 if (value) {
Alek Du8081c842010-05-26 14:42:25 -0700112 gpsr = gpio_reg(chip, offset, GPSR);
Alek Du8bf02612009-09-22 16:46:36 -0700113 writel(BIT(offset % 32), gpsr);
114 } else {
Alek Du8081c842010-05-26 14:42:25 -0700115 gpcr = gpio_reg(chip, offset, GPCR);
Alek Du8bf02612009-09-22 16:46:36 -0700116 writel(BIT(offset % 32), gpcr);
117 }
118}
119
David Cohenf89a7682013-10-04 13:01:42 -0700120static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
Alek Du8bf02612009-09-22 16:46:36 -0700121{
Linus Walleij5c77c022015-12-06 10:55:28 +0100122 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Alek Du8081c842010-05-26 14:42:25 -0700123 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
Alek Du8bf02612009-09-22 16:46:36 -0700124 u32 value;
125 unsigned long flags;
Alek Du8bf02612009-09-22 16:46:36 -0700126
David Cohenf89a7682013-10-04 13:01:42 -0700127 if (priv->pdev)
128 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100129
David Cohenf89a7682013-10-04 13:01:42 -0700130 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700131 value = readl(gpdr);
132 value &= ~BIT(offset % 32);
133 writel(value, gpdr);
David Cohenf89a7682013-10-04 13:01:42 -0700134 spin_unlock_irqrestore(&priv->lock, flags);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100135
David Cohenf89a7682013-10-04 13:01:42 -0700136 if (priv->pdev)
137 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100138
Alek Du8bf02612009-09-22 16:46:36 -0700139 return 0;
140}
141
David Cohenf89a7682013-10-04 13:01:42 -0700142static int intel_gpio_direction_output(struct gpio_chip *chip,
Alek Du8bf02612009-09-22 16:46:36 -0700143 unsigned offset, int value)
144{
Linus Walleij5c77c022015-12-06 10:55:28 +0100145 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Alek Du8081c842010-05-26 14:42:25 -0700146 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
Alek Du8bf02612009-09-22 16:46:36 -0700147 unsigned long flags;
Alek Du8bf02612009-09-22 16:46:36 -0700148
David Cohenf89a7682013-10-04 13:01:42 -0700149 intel_gpio_set(chip, offset, value);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100150
David Cohenf89a7682013-10-04 13:01:42 -0700151 if (priv->pdev)
152 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100153
David Cohenf89a7682013-10-04 13:01:42 -0700154 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700155 value = readl(gpdr);
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700156 value |= BIT(offset % 32);
Alek Du8bf02612009-09-22 16:46:36 -0700157 writel(value, gpdr);
David Cohenf89a7682013-10-04 13:01:42 -0700158 spin_unlock_irqrestore(&priv->lock, flags);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100159
David Cohenf89a7682013-10-04 13:01:42 -0700160 if (priv->pdev)
161 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100162
Alek Du8bf02612009-09-22 16:46:36 -0700163 return 0;
164}
165
David Cohenf89a7682013-10-04 13:01:42 -0700166static int intel_mid_irq_type(struct irq_data *d, unsigned type)
Alek Du8bf02612009-09-22 16:46:36 -0700167{
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200168 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij5c77c022015-12-06 10:55:28 +0100169 struct intel_mid_gpio *priv = gpiochip_get_data(gc);
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300170 u32 gpio = irqd_to_hwirq(d);
Alek Du8bf02612009-09-22 16:46:36 -0700171 unsigned long flags;
172 u32 value;
David Cohenf89a7682013-10-04 13:01:42 -0700173 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
174 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
Alek Du8bf02612009-09-22 16:46:36 -0700175
David Cohenf89a7682013-10-04 13:01:42 -0700176 if (gpio >= priv->chip.ngpio)
Alek Du8bf02612009-09-22 16:46:36 -0700177 return -EINVAL;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100178
David Cohenf89a7682013-10-04 13:01:42 -0700179 if (priv->pdev)
180 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100181
David Cohenf89a7682013-10-04 13:01:42 -0700182 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700183 if (type & IRQ_TYPE_EDGE_RISING)
184 value = readl(grer) | BIT(gpio % 32);
185 else
186 value = readl(grer) & (~BIT(gpio % 32));
187 writel(value, grer);
188
189 if (type & IRQ_TYPE_EDGE_FALLING)
190 value = readl(gfer) | BIT(gpio % 32);
191 else
192 value = readl(gfer) & (~BIT(gpio % 32));
193 writel(value, gfer);
David Cohenf89a7682013-10-04 13:01:42 -0700194 spin_unlock_irqrestore(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700195
David Cohenf89a7682013-10-04 13:01:42 -0700196 if (priv->pdev)
197 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100198
Alek Du8bf02612009-09-22 16:46:36 -0700199 return 0;
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700200}
Alek Du8bf02612009-09-22 16:46:36 -0700201
David Cohenf89a7682013-10-04 13:01:42 -0700202static void intel_mid_irq_unmask(struct irq_data *d)
Alek Du8bf02612009-09-22 16:46:36 -0700203{
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700204}
Alek Du8bf02612009-09-22 16:46:36 -0700205
David Cohenf89a7682013-10-04 13:01:42 -0700206static void intel_mid_irq_mask(struct irq_data *d)
Alek Du8bf02612009-09-22 16:46:36 -0700207{
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700208}
Alek Du8bf02612009-09-22 16:46:36 -0700209
David Cohenf89a7682013-10-04 13:01:42 -0700210static struct irq_chip intel_mid_irqchip = {
211 .name = "INTEL_MID-GPIO",
212 .irq_mask = intel_mid_irq_mask,
213 .irq_unmask = intel_mid_irq_unmask,
214 .irq_set_type = intel_mid_irq_type,
Alek Du8bf02612009-09-22 16:46:36 -0700215};
216
David Cohenf89a7682013-10-04 13:01:42 -0700217static const struct intel_mid_gpio_ddata gpio_lincroft = {
David Cohend56d6b32013-10-04 13:01:40 -0700218 .ngpio = 64,
219};
220
David Cohenf89a7682013-10-04 13:01:42 -0700221static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
David Cohend56d6b32013-10-04 13:01:40 -0700222 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700223 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700224};
225
David Cohenf89a7682013-10-04 13:01:42 -0700226static const struct intel_mid_gpio_ddata gpio_penwell_core = {
David Cohend56d6b32013-10-04 13:01:40 -0700227 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700228 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700229};
230
David Cohenf89a7682013-10-04 13:01:42 -0700231static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
David Cohend56d6b32013-10-04 13:01:40 -0700232 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700233 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
David Cohend56d6b32013-10-04 13:01:40 -0700234};
235
David Cohenf89a7682013-10-04 13:01:42 -0700236static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
David Cohend56d6b32013-10-04 13:01:40 -0700237 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700238 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700239};
240
Jingoo Han14f4a882013-12-03 08:08:45 +0900241static const struct pci_device_id intel_gpio_ids[] = {
David Cohend56d6b32013-10-04 13:01:40 -0700242 {
243 /* Lincroft */
244 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
245 .driver_data = (kernel_ulong_t)&gpio_lincroft,
246 },
247 {
248 /* Penwell AON */
249 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
250 .driver_data = (kernel_ulong_t)&gpio_penwell_aon,
251 },
252 {
253 /* Penwell Core */
254 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
255 .driver_data = (kernel_ulong_t)&gpio_penwell_core,
256 },
257 {
258 /* Cloverview Aon */
259 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
260 .driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
261 },
262 {
263 /* Cloverview Core */
264 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
265 .driver_data = (kernel_ulong_t)&gpio_cloverview_core,
266 },
Andy Shevchenkoddc53c42018-09-04 14:26:25 +0300267 { }
Alek Du8bf02612009-09-22 16:46:36 -0700268};
Alek Du8bf02612009-09-22 16:46:36 -0700269
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200270static void intel_mid_irq_handler(struct irq_desc *desc)
Alek Du8bf02612009-09-22 16:46:36 -0700271{
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200272 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Linus Walleij5c77c022015-12-06 10:55:28 +0100273 struct intel_mid_gpio *priv = gpiochip_get_data(gc);
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000274 struct irq_data *data = irq_desc_get_irq_data(desc);
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000275 struct irq_chip *chip = irq_data_get_irq_chip(data);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000276 u32 base, gpio, mask;
Thomas Gleixner732063b2011-03-17 19:32:55 +0000277 unsigned long pending;
Alek Du8bf02612009-09-22 16:46:36 -0700278 void __iomem *gedr;
Alek Du8bf02612009-09-22 16:46:36 -0700279
280 /* check GPIO controller to check which pin triggered the interrupt */
David Cohenf89a7682013-10-04 13:01:42 -0700281 for (base = 0; base < priv->chip.ngpio; base += 32) {
282 gedr = gpio_reg(&priv->chip, base, GEDR);
Mika Westerbergc8f925b2012-05-10 13:01:22 +0300283 while ((pending = readl(gedr))) {
Mathias Nyman2345b202011-07-08 10:02:18 +0100284 gpio = __ffs(pending);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000285 mask = BIT(gpio);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000286 /* Clear before handling so we can't lose an edge */
287 writel(mask, gedr);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100288 generic_handle_irq(irq_find_mapping(gc->irq.domain,
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300289 base + gpio));
Thomas Gleixner732063b2011-03-17 19:32:55 +0000290 }
Alek Du8bf02612009-09-22 16:46:36 -0700291 }
Feng Tang0766d202011-01-25 15:07:15 -0800292
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000293 chip->irq_eoi(data);
Alek Du8bf02612009-09-22 16:46:36 -0700294}
295
David Cohenf89a7682013-10-04 13:01:42 -0700296static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
Mika Westerbergf5f93112012-04-05 12:15:17 +0300297{
298 void __iomem *reg;
299 unsigned base;
300
David Cohenf89a7682013-10-04 13:01:42 -0700301 for (base = 0; base < priv->chip.ngpio; base += 32) {
Mika Westerbergf5f93112012-04-05 12:15:17 +0300302 /* Clear the rising-edge detect register */
David Cohenf89a7682013-10-04 13:01:42 -0700303 reg = gpio_reg(&priv->chip, base, GRER);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300304 writel(0, reg);
305 /* Clear the falling-edge detect register */
David Cohenf89a7682013-10-04 13:01:42 -0700306 reg = gpio_reg(&priv->chip, base, GFER);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300307 writel(0, reg);
308 /* Clear the edge detect status register */
David Cohenf89a7682013-10-04 13:01:42 -0700309 reg = gpio_reg(&priv->chip, base, GEDR);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300310 writel(~0, reg);
311 }
312}
313
Augusto Mecking Caringifbc2a292017-01-16 14:30:41 +0000314static int __maybe_unused intel_gpio_runtime_idle(struct device *dev)
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100315{
xinhui.pan84a34572014-01-31 13:08:01 -0800316 int err = pm_schedule_suspend(dev, 500);
317 return err ?: -EBUSY;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100318}
319
David Cohenf89a7682013-10-04 13:01:42 -0700320static const struct dev_pm_ops intel_gpio_pm_ops = {
321 SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100322};
323
David Cohenf89a7682013-10-04 13:01:42 -0700324static int intel_gpio_probe(struct pci_dev *pdev,
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300325 const struct pci_device_id *id)
Alek Du8bf02612009-09-22 16:46:36 -0700326{
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300327 void __iomem *base;
David Cohenf89a7682013-10-04 13:01:42 -0700328 struct intel_mid_gpio *priv;
Alek Du8bf02612009-09-22 16:46:36 -0700329 u32 gpio_base;
David Cohen2519f9a2013-05-06 16:11:03 -0700330 u32 irq_base;
Julia Lawalld6a2b7b2012-08-05 11:52:34 +0200331 int retval;
David Cohenf89a7682013-10-04 13:01:42 -0700332 struct intel_mid_gpio_ddata *ddata =
333 (struct intel_mid_gpio_ddata *)id->driver_data;
Alek Du8bf02612009-09-22 16:46:36 -0700334
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300335 retval = pcim_enable_device(pdev);
Alek Du8bf02612009-09-22 16:46:36 -0700336 if (retval)
Mika Westerberg8302c742012-04-05 12:15:15 +0300337 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700338
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300339 retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
Alek Du8bf02612009-09-22 16:46:36 -0700340 if (retval) {
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300341 dev_err(&pdev->dev, "I/O memory mapping error\n");
342 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700343 }
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300344
345 base = pcim_iomap_table(pdev)[1];
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300346
347 irq_base = readl(base);
348 gpio_base = readl(sizeof(u32) + base);
349
Alek Du8bf02612009-09-22 16:46:36 -0700350 /* release the IO mapping, since we already get the info from bar1 */
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300351 pcim_iounmap_regions(pdev, 1 << 1);
Alek Du8bf02612009-09-22 16:46:36 -0700352
David Cohenf89a7682013-10-04 13:01:42 -0700353 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Markus Elfring75f8f5a2018-02-11 21:56:42 +0100354 if (!priv)
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300355 return -ENOMEM;
Mika Westerbergb3e35af2012-04-05 12:15:16 +0300356
David Cohenf89a7682013-10-04 13:01:42 -0700357 priv->reg_base = pcim_iomap_table(pdev)[0];
358 priv->chip.label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +0100359 priv->chip.parent = &pdev->dev;
David Cohenf89a7682013-10-04 13:01:42 -0700360 priv->chip.request = intel_gpio_request;
361 priv->chip.direction_input = intel_gpio_direction_input;
362 priv->chip.direction_output = intel_gpio_direction_output;
363 priv->chip.get = intel_gpio_get;
364 priv->chip.set = intel_gpio_set;
David Cohenf89a7682013-10-04 13:01:42 -0700365 priv->chip.base = gpio_base;
366 priv->chip.ngpio = ddata->ngpio;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100367 priv->chip.can_sleep = false;
David Cohenf89a7682013-10-04 13:01:42 -0700368 priv->pdev = pdev;
David Cohen2519f9a2013-05-06 16:11:03 -0700369
David Cohenf89a7682013-10-04 13:01:42 -0700370 spin_lock_init(&priv->lock);
Andy Shevchenkoaeb168f2013-05-22 13:20:10 +0300371
David Cohenf89a7682013-10-04 13:01:42 -0700372 pci_set_drvdata(pdev, priv);
Andy Shevchenkodd3b2042016-06-19 23:49:57 +0300373 retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
Alek Du8bf02612009-09-22 16:46:36 -0700374 if (retval) {
Andy Shevchenko8aca1192013-05-22 13:20:13 +0300375 dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300376 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700377 }
Mika Westerbergf5f93112012-04-05 12:15:17 +0300378
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200379 retval = gpiochip_irqchip_add(&priv->chip,
380 &intel_mid_irqchip,
381 irq_base,
382 handle_simple_irq,
383 IRQ_TYPE_NONE);
384 if (retval) {
385 dev_err(&pdev->dev,
386 "could not connect irqchip to gpiochip\n");
387 return retval;
388 }
389
David Cohenf89a7682013-10-04 13:01:42 -0700390 intel_mid_irq_init_hw(priv);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300391
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200392 gpiochip_set_chained_irqchip(&priv->chip,
393 &intel_mid_irqchip,
394 pdev->irq,
395 intel_mid_irq_handler);
Alek Du8bf02612009-09-22 16:46:36 -0700396
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100397 pm_runtime_put_noidle(&pdev->dev);
398 pm_runtime_allow(&pdev->dev);
399
Mika Westerberg8302c742012-04-05 12:15:15 +0300400 return 0;
Alek Du8bf02612009-09-22 16:46:36 -0700401}
402
David Cohenf89a7682013-10-04 13:01:42 -0700403static struct pci_driver intel_gpio_driver = {
404 .name = "intel_mid_gpio",
405 .id_table = intel_gpio_ids,
406 .probe = intel_gpio_probe,
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100407 .driver = {
David Cohenf89a7682013-10-04 13:01:42 -0700408 .pm = &intel_gpio_pm_ops,
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100409 },
Alek Du8bf02612009-09-22 16:46:36 -0700410};
411
Geliang Tang5261bee2016-11-14 20:52:25 +0800412builtin_pci_driver(intel_gpio_driver);