blob: cdaba13cb8e825a3355325f9f6fd5e1663a2bd1d [file] [log] [blame]
Grant Likelyc103de22011-06-04 18:38:28 -06001/*
David Cohena0bbf032014-01-17 07:30:01 -08002 * Intel MID GPIO driver
Grant Likelyc103de22011-06-04 18:38:28 -06003 *
David Cohena0bbf032014-01-17 07:30:01 -08004 * Copyright (c) 2008-2014 Intel Corporation.
Alek Du8bf02612009-09-22 16:46:36 -07005 *
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.
Alek Du8bf02612009-09-22 16:46:36 -070014 */
15
16/* Supports:
17 * Moorestown platform Langwell chip.
Alek Du8081c842010-05-26 14:42:25 -070018 * Medfield platform Penwell chip.
David Cohenf89a7682013-10-04 13:01:42 -070019 * Clovertrail platform Cloverview chip.
20 * Merrifield platform Tangier chip.
Alek Du8bf02612009-09-22 16:46:36 -070021 */
22
23#include <linux/module.h>
24#include <linux/pci.h>
Alan Cox72b43792010-10-27 15:33:23 -070025#include <linux/platform_device.h>
Alek Du8bf02612009-09-22 16:46:36 -070026#include <linux/kernel.h>
27#include <linux/delay.h>
28#include <linux/stddef.h>
29#include <linux/interrupt.h>
30#include <linux/init.h>
Alek Du8bf02612009-09-22 16:46:36 -070031#include <linux/io.h>
Linus Walleij3f7dbfd2014-05-29 16:55:55 +020032#include <linux/gpio/driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Kristen Carlson Accardi78128032011-05-10 14:23:45 +010034#include <linux/pm_runtime.h>
Alek Du8bf02612009-09-22 16:46:36 -070035
David Cohenf89a7682013-10-04 13:01:42 -070036#define INTEL_MID_IRQ_TYPE_EDGE (1 << 0)
37#define INTEL_MID_IRQ_TYPE_LEVEL (1 << 1)
David Cohend56d6b32013-10-04 13:01:40 -070038
Alek Du8081c842010-05-26 14:42:25 -070039/*
40 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
41 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
42 * registers to control them, so we only define the order here instead of a
43 * structure, to get a bit offset for a pin (use GPDR as an example):
44 *
45 * nreg = ngpio / 32;
46 * reg = offset / 32;
47 * bit = offset % 32;
48 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
49 *
50 * so the bit of reg_addr is to control pin offset's GPDR feature
51*/
52
53enum GPIO_REG {
54 GPLR = 0, /* pin level read-only */
55 GPDR, /* pin direction */
56 GPSR, /* pin set */
57 GPCR, /* pin clear */
58 GRER, /* rising edge detect */
59 GFER, /* falling edge detect */
60 GEDR, /* edge detect result */
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030061 GAFR, /* alt function */
Alek Du8bf02612009-09-22 16:46:36 -070062};
63
David Cohenf89a7682013-10-04 13:01:42 -070064/* intel_mid gpio driver data */
65struct intel_mid_gpio_ddata {
David Cohend56d6b32013-10-04 13:01:40 -070066 u16 ngpio; /* number of gpio pins */
67 u32 gplr_offset; /* offset of first GPLR register from base */
68 u32 flis_base; /* base address of FLIS registers */
69 u32 flis_len; /* length of FLIS registers */
70 u32 (*get_flis_offset)(int gpio);
71 u32 chip_irq_type; /* chip interrupt type */
72};
73
David Cohenf89a7682013-10-04 13:01:42 -070074struct intel_mid_gpio {
Alek Du8bf02612009-09-22 16:46:36 -070075 struct gpio_chip chip;
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +030076 void __iomem *reg_base;
Alek Du8bf02612009-09-22 16:46:36 -070077 spinlock_t lock;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +010078 struct pci_dev *pdev;
Alek Du8bf02612009-09-22 16:46:36 -070079};
80
Alek Du8081c842010-05-26 14:42:25 -070081static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
Andy Shevchenko611a4852013-05-22 13:20:14 +030082 enum GPIO_REG reg_type)
Alek Du8bf02612009-09-22 16:46:36 -070083{
Linus Walleij5c77c022015-12-06 10:55:28 +010084 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Alek Du8081c842010-05-26 14:42:25 -070085 unsigned nreg = chip->ngpio / 32;
Alek Du8bf02612009-09-22 16:46:36 -070086 u8 reg = offset / 32;
Alek Du8bf02612009-09-22 16:46:36 -070087
David Cohenf89a7682013-10-04 13:01:42 -070088 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
Alek Du8081c842010-05-26 14:42:25 -070089}
90
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030091static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
92 enum GPIO_REG reg_type)
93{
Linus Walleij5c77c022015-12-06 10:55:28 +010094 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030095 unsigned nreg = chip->ngpio / 32;
96 u8 reg = offset / 16;
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030097
David Cohenf89a7682013-10-04 13:01:42 -070098 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030099}
100
David Cohenf89a7682013-10-04 13:01:42 -0700101static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
Adrian Hunter8c0f7b12011-10-03 14:36:07 +0300102{
103 void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
104 u32 value = readl(gafr);
105 int shift = (offset % 16) << 1, af = (value >> shift) & 3;
106
107 if (af) {
108 value &= ~(3 << shift);
109 writel(value, gafr);
110 }
111 return 0;
112}
113
David Cohenf89a7682013-10-04 13:01:42 -0700114static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
Alek Du8081c842010-05-26 14:42:25 -0700115{
116 void __iomem *gplr = gpio_reg(chip, offset, GPLR);
117
Linus Walleij4c628f32015-12-21 11:00:56 +0100118 return !!(readl(gplr) & BIT(offset % 32));
Alek Du8bf02612009-09-22 16:46:36 -0700119}
120
David Cohenf89a7682013-10-04 13:01:42 -0700121static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
Alek Du8bf02612009-09-22 16:46:36 -0700122{
Alek Du8bf02612009-09-22 16:46:36 -0700123 void __iomem *gpsr, *gpcr;
124
125 if (value) {
Alek Du8081c842010-05-26 14:42:25 -0700126 gpsr = gpio_reg(chip, offset, GPSR);
Alek Du8bf02612009-09-22 16:46:36 -0700127 writel(BIT(offset % 32), gpsr);
128 } else {
Alek Du8081c842010-05-26 14:42:25 -0700129 gpcr = gpio_reg(chip, offset, GPCR);
Alek Du8bf02612009-09-22 16:46:36 -0700130 writel(BIT(offset % 32), gpcr);
131 }
132}
133
David Cohenf89a7682013-10-04 13:01:42 -0700134static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
Alek Du8bf02612009-09-22 16:46:36 -0700135{
Linus Walleij5c77c022015-12-06 10:55:28 +0100136 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Alek Du8081c842010-05-26 14:42:25 -0700137 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
Alek Du8bf02612009-09-22 16:46:36 -0700138 u32 value;
139 unsigned long flags;
Alek Du8bf02612009-09-22 16:46:36 -0700140
David Cohenf89a7682013-10-04 13:01:42 -0700141 if (priv->pdev)
142 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100143
David Cohenf89a7682013-10-04 13:01:42 -0700144 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700145 value = readl(gpdr);
146 value &= ~BIT(offset % 32);
147 writel(value, gpdr);
David Cohenf89a7682013-10-04 13:01:42 -0700148 spin_unlock_irqrestore(&priv->lock, flags);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100149
David Cohenf89a7682013-10-04 13:01:42 -0700150 if (priv->pdev)
151 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100152
Alek Du8bf02612009-09-22 16:46:36 -0700153 return 0;
154}
155
David Cohenf89a7682013-10-04 13:01:42 -0700156static int intel_gpio_direction_output(struct gpio_chip *chip,
Alek Du8bf02612009-09-22 16:46:36 -0700157 unsigned offset, int value)
158{
Linus Walleij5c77c022015-12-06 10:55:28 +0100159 struct intel_mid_gpio *priv = gpiochip_get_data(chip);
Alek Du8081c842010-05-26 14:42:25 -0700160 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
Alek Du8bf02612009-09-22 16:46:36 -0700161 unsigned long flags;
Alek Du8bf02612009-09-22 16:46:36 -0700162
David Cohenf89a7682013-10-04 13:01:42 -0700163 intel_gpio_set(chip, offset, value);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100164
David Cohenf89a7682013-10-04 13:01:42 -0700165 if (priv->pdev)
166 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100167
David Cohenf89a7682013-10-04 13:01:42 -0700168 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700169 value = readl(gpdr);
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700170 value |= BIT(offset % 32);
Alek Du8bf02612009-09-22 16:46:36 -0700171 writel(value, gpdr);
David Cohenf89a7682013-10-04 13:01:42 -0700172 spin_unlock_irqrestore(&priv->lock, flags);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100173
David Cohenf89a7682013-10-04 13:01:42 -0700174 if (priv->pdev)
175 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100176
Alek Du8bf02612009-09-22 16:46:36 -0700177 return 0;
178}
179
David Cohenf89a7682013-10-04 13:01:42 -0700180static int intel_mid_irq_type(struct irq_data *d, unsigned type)
Alek Du8bf02612009-09-22 16:46:36 -0700181{
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200182 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij5c77c022015-12-06 10:55:28 +0100183 struct intel_mid_gpio *priv = gpiochip_get_data(gc);
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300184 u32 gpio = irqd_to_hwirq(d);
Alek Du8bf02612009-09-22 16:46:36 -0700185 unsigned long flags;
186 u32 value;
David Cohenf89a7682013-10-04 13:01:42 -0700187 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
188 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
Alek Du8bf02612009-09-22 16:46:36 -0700189
David Cohenf89a7682013-10-04 13:01:42 -0700190 if (gpio >= priv->chip.ngpio)
Alek Du8bf02612009-09-22 16:46:36 -0700191 return -EINVAL;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100192
David Cohenf89a7682013-10-04 13:01:42 -0700193 if (priv->pdev)
194 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100195
David Cohenf89a7682013-10-04 13:01:42 -0700196 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700197 if (type & IRQ_TYPE_EDGE_RISING)
198 value = readl(grer) | BIT(gpio % 32);
199 else
200 value = readl(grer) & (~BIT(gpio % 32));
201 writel(value, grer);
202
203 if (type & IRQ_TYPE_EDGE_FALLING)
204 value = readl(gfer) | BIT(gpio % 32);
205 else
206 value = readl(gfer) & (~BIT(gpio % 32));
207 writel(value, gfer);
David Cohenf89a7682013-10-04 13:01:42 -0700208 spin_unlock_irqrestore(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700209
David Cohenf89a7682013-10-04 13:01:42 -0700210 if (priv->pdev)
211 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100212
Alek Du8bf02612009-09-22 16:46:36 -0700213 return 0;
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700214}
Alek Du8bf02612009-09-22 16:46:36 -0700215
David Cohenf89a7682013-10-04 13:01:42 -0700216static void intel_mid_irq_unmask(struct irq_data *d)
Alek Du8bf02612009-09-22 16:46:36 -0700217{
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700218}
Alek Du8bf02612009-09-22 16:46:36 -0700219
David Cohenf89a7682013-10-04 13:01:42 -0700220static void intel_mid_irq_mask(struct irq_data *d)
Alek Du8bf02612009-09-22 16:46:36 -0700221{
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700222}
Alek Du8bf02612009-09-22 16:46:36 -0700223
David Cohenf89a7682013-10-04 13:01:42 -0700224static struct irq_chip intel_mid_irqchip = {
225 .name = "INTEL_MID-GPIO",
226 .irq_mask = intel_mid_irq_mask,
227 .irq_unmask = intel_mid_irq_unmask,
228 .irq_set_type = intel_mid_irq_type,
Alek Du8bf02612009-09-22 16:46:36 -0700229};
230
David Cohenf89a7682013-10-04 13:01:42 -0700231static const struct intel_mid_gpio_ddata gpio_lincroft = {
David Cohend56d6b32013-10-04 13:01:40 -0700232 .ngpio = 64,
233};
234
David Cohenf89a7682013-10-04 13:01:42 -0700235static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
David Cohend56d6b32013-10-04 13:01:40 -0700236 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700237 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700238};
239
David Cohenf89a7682013-10-04 13:01:42 -0700240static const struct intel_mid_gpio_ddata gpio_penwell_core = {
David Cohend56d6b32013-10-04 13:01:40 -0700241 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700242 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700243};
244
David Cohenf89a7682013-10-04 13:01:42 -0700245static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
David Cohend56d6b32013-10-04 13:01:40 -0700246 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700247 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
David Cohend56d6b32013-10-04 13:01:40 -0700248};
249
David Cohenf89a7682013-10-04 13:01:42 -0700250static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
David Cohend56d6b32013-10-04 13:01:40 -0700251 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700252 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700253};
254
David Cohenf89a7682013-10-04 13:01:42 -0700255static const struct intel_mid_gpio_ddata gpio_tangier = {
David Cohend56d6b32013-10-04 13:01:40 -0700256 .ngpio = 192,
257 .gplr_offset = 4,
258 .flis_base = 0xff0c0000,
259 .flis_len = 0x8000,
260 .get_flis_offset = NULL,
David Cohenf89a7682013-10-04 13:01:42 -0700261 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700262};
263
Jingoo Han14f4a882013-12-03 08:08:45 +0900264static const struct pci_device_id intel_gpio_ids[] = {
David Cohend56d6b32013-10-04 13:01:40 -0700265 {
266 /* Lincroft */
267 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
268 .driver_data = (kernel_ulong_t)&gpio_lincroft,
269 },
270 {
271 /* Penwell AON */
272 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
273 .driver_data = (kernel_ulong_t)&gpio_penwell_aon,
274 },
275 {
276 /* Penwell Core */
277 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
278 .driver_data = (kernel_ulong_t)&gpio_penwell_core,
279 },
280 {
281 /* Cloverview Aon */
282 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
283 .driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
284 },
285 {
286 /* Cloverview Core */
287 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
288 .driver_data = (kernel_ulong_t)&gpio_cloverview_core,
289 },
290 {
291 /* Tangier */
292 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x1199),
293 .driver_data = (kernel_ulong_t)&gpio_tangier,
294 },
295 { 0 }
Alek Du8bf02612009-09-22 16:46:36 -0700296};
David Cohenf89a7682013-10-04 13:01:42 -0700297MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
Alek Du8bf02612009-09-22 16:46:36 -0700298
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200299static void intel_mid_irq_handler(struct irq_desc *desc)
Alek Du8bf02612009-09-22 16:46:36 -0700300{
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200301 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Linus Walleij5c77c022015-12-06 10:55:28 +0100302 struct intel_mid_gpio *priv = gpiochip_get_data(gc);
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000303 struct irq_data *data = irq_desc_get_irq_data(desc);
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000304 struct irq_chip *chip = irq_data_get_irq_chip(data);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000305 u32 base, gpio, mask;
Thomas Gleixner732063b2011-03-17 19:32:55 +0000306 unsigned long pending;
Alek Du8bf02612009-09-22 16:46:36 -0700307 void __iomem *gedr;
Alek Du8bf02612009-09-22 16:46:36 -0700308
309 /* check GPIO controller to check which pin triggered the interrupt */
David Cohenf89a7682013-10-04 13:01:42 -0700310 for (base = 0; base < priv->chip.ngpio; base += 32) {
311 gedr = gpio_reg(&priv->chip, base, GEDR);
Mika Westerbergc8f925b2012-05-10 13:01:22 +0300312 while ((pending = readl(gedr))) {
Mathias Nyman2345b202011-07-08 10:02:18 +0100313 gpio = __ffs(pending);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000314 mask = BIT(gpio);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000315 /* Clear before handling so we can't lose an edge */
316 writel(mask, gedr);
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200317 generic_handle_irq(irq_find_mapping(gc->irqdomain,
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300318 base + gpio));
Thomas Gleixner732063b2011-03-17 19:32:55 +0000319 }
Alek Du8bf02612009-09-22 16:46:36 -0700320 }
Feng Tang0766d202011-01-25 15:07:15 -0800321
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000322 chip->irq_eoi(data);
Alek Du8bf02612009-09-22 16:46:36 -0700323}
324
David Cohenf89a7682013-10-04 13:01:42 -0700325static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
Mika Westerbergf5f93112012-04-05 12:15:17 +0300326{
327 void __iomem *reg;
328 unsigned base;
329
David Cohenf89a7682013-10-04 13:01:42 -0700330 for (base = 0; base < priv->chip.ngpio; base += 32) {
Mika Westerbergf5f93112012-04-05 12:15:17 +0300331 /* Clear the rising-edge detect register */
David Cohenf89a7682013-10-04 13:01:42 -0700332 reg = gpio_reg(&priv->chip, base, GRER);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300333 writel(0, reg);
334 /* Clear the falling-edge detect register */
David Cohenf89a7682013-10-04 13:01:42 -0700335 reg = gpio_reg(&priv->chip, base, GFER);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300336 writel(0, reg);
337 /* Clear the edge detect status register */
David Cohenf89a7682013-10-04 13:01:42 -0700338 reg = gpio_reg(&priv->chip, base, GEDR);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300339 writel(~0, reg);
340 }
341}
342
David Cohenf89a7682013-10-04 13:01:42 -0700343static int intel_gpio_runtime_idle(struct device *dev)
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100344{
xinhui.pan84a34572014-01-31 13:08:01 -0800345 int err = pm_schedule_suspend(dev, 500);
346 return err ?: -EBUSY;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100347}
348
David Cohenf89a7682013-10-04 13:01:42 -0700349static const struct dev_pm_ops intel_gpio_pm_ops = {
350 SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100351};
352
David Cohenf89a7682013-10-04 13:01:42 -0700353static int intel_gpio_probe(struct pci_dev *pdev,
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300354 const struct pci_device_id *id)
Alek Du8bf02612009-09-22 16:46:36 -0700355{
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300356 void __iomem *base;
David Cohenf89a7682013-10-04 13:01:42 -0700357 struct intel_mid_gpio *priv;
Alek Du8bf02612009-09-22 16:46:36 -0700358 u32 gpio_base;
David Cohen2519f9a2013-05-06 16:11:03 -0700359 u32 irq_base;
Julia Lawalld6a2b7b2012-08-05 11:52:34 +0200360 int retval;
David Cohenf89a7682013-10-04 13:01:42 -0700361 struct intel_mid_gpio_ddata *ddata =
362 (struct intel_mid_gpio_ddata *)id->driver_data;
Alek Du8bf02612009-09-22 16:46:36 -0700363
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300364 retval = pcim_enable_device(pdev);
Alek Du8bf02612009-09-22 16:46:36 -0700365 if (retval)
Mika Westerberg8302c742012-04-05 12:15:15 +0300366 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700367
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300368 retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
Alek Du8bf02612009-09-22 16:46:36 -0700369 if (retval) {
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300370 dev_err(&pdev->dev, "I/O memory mapping error\n");
371 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700372 }
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300373
374 base = pcim_iomap_table(pdev)[1];
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300375
376 irq_base = readl(base);
377 gpio_base = readl(sizeof(u32) + base);
378
Alek Du8bf02612009-09-22 16:46:36 -0700379 /* release the IO mapping, since we already get the info from bar1 */
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300380 pcim_iounmap_regions(pdev, 1 << 1);
Alek Du8bf02612009-09-22 16:46:36 -0700381
David Cohenf89a7682013-10-04 13:01:42 -0700382 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
383 if (!priv) {
Andy Shevchenko8aca1192013-05-22 13:20:13 +0300384 dev_err(&pdev->dev, "can't allocate chip data\n");
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300385 return -ENOMEM;
Alek Du8bf02612009-09-22 16:46:36 -0700386 }
Mika Westerbergb3e35af2012-04-05 12:15:16 +0300387
David Cohenf89a7682013-10-04 13:01:42 -0700388 priv->reg_base = pcim_iomap_table(pdev)[0];
389 priv->chip.label = dev_name(&pdev->dev);
Linus Walleij58383c72015-11-04 09:56:26 +0100390 priv->chip.parent = &pdev->dev;
David Cohenf89a7682013-10-04 13:01:42 -0700391 priv->chip.request = intel_gpio_request;
392 priv->chip.direction_input = intel_gpio_direction_input;
393 priv->chip.direction_output = intel_gpio_direction_output;
394 priv->chip.get = intel_gpio_get;
395 priv->chip.set = intel_gpio_set;
David Cohenf89a7682013-10-04 13:01:42 -0700396 priv->chip.base = gpio_base;
397 priv->chip.ngpio = ddata->ngpio;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100398 priv->chip.can_sleep = false;
David Cohenf89a7682013-10-04 13:01:42 -0700399 priv->pdev = pdev;
David Cohen2519f9a2013-05-06 16:11:03 -0700400
David Cohenf89a7682013-10-04 13:01:42 -0700401 spin_lock_init(&priv->lock);
Andy Shevchenkoaeb168f2013-05-22 13:20:10 +0300402
David Cohenf89a7682013-10-04 13:01:42 -0700403 pci_set_drvdata(pdev, priv);
Linus Walleij5c77c022015-12-06 10:55:28 +0100404 retval = gpiochip_add_data(&priv->chip, priv);
Alek Du8bf02612009-09-22 16:46:36 -0700405 if (retval) {
Andy Shevchenko8aca1192013-05-22 13:20:13 +0300406 dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300407 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700408 }
Mika Westerbergf5f93112012-04-05 12:15:17 +0300409
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200410 retval = gpiochip_irqchip_add(&priv->chip,
411 &intel_mid_irqchip,
412 irq_base,
413 handle_simple_irq,
414 IRQ_TYPE_NONE);
415 if (retval) {
416 dev_err(&pdev->dev,
417 "could not connect irqchip to gpiochip\n");
418 return retval;
419 }
420
David Cohenf89a7682013-10-04 13:01:42 -0700421 intel_mid_irq_init_hw(priv);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300422
Linus Walleij3f7dbfd2014-05-29 16:55:55 +0200423 gpiochip_set_chained_irqchip(&priv->chip,
424 &intel_mid_irqchip,
425 pdev->irq,
426 intel_mid_irq_handler);
Alek Du8bf02612009-09-22 16:46:36 -0700427
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100428 pm_runtime_put_noidle(&pdev->dev);
429 pm_runtime_allow(&pdev->dev);
430
Mika Westerberg8302c742012-04-05 12:15:15 +0300431 return 0;
Alek Du8bf02612009-09-22 16:46:36 -0700432}
433
David Cohenf89a7682013-10-04 13:01:42 -0700434static struct pci_driver intel_gpio_driver = {
435 .name = "intel_mid_gpio",
436 .id_table = intel_gpio_ids,
437 .probe = intel_gpio_probe,
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100438 .driver = {
David Cohenf89a7682013-10-04 13:01:42 -0700439 .pm = &intel_gpio_pm_ops,
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100440 },
Alek Du8bf02612009-09-22 16:46:36 -0700441};
442
David Cohenf89a7682013-10-04 13:01:42 -0700443static int __init intel_gpio_init(void)
Alek Du8bf02612009-09-22 16:46:36 -0700444{
David Cohenf89a7682013-10-04 13:01:42 -0700445 return pci_register_driver(&intel_gpio_driver);
Alek Du8bf02612009-09-22 16:46:36 -0700446}
447
David Cohenf89a7682013-10-04 13:01:42 -0700448device_initcall(intel_gpio_init);