blob: be803af658acf2e95f4782ff40ae2a560ec03f11 [file] [log] [blame]
Grant Likelyc103de22011-06-04 18:38:28 -06001/*
2 * Moorestown platform Langwell chip GPIO driver
3 *
Andy Shevchenko611a4852013-05-22 13:20:14 +03004 * Copyright (c) 2008, 2009, 2013, 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.
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 Du8081c842010-05-26 14:42:25 -070022 * Medfield platform Penwell chip.
David Cohenf89a7682013-10-04 13:01:42 -070023 * Clovertrail platform Cloverview chip.
24 * Merrifield platform Tangier chip.
Alek Du8bf02612009-09-22 16:46:36 -070025 */
26
27#include <linux/module.h>
28#include <linux/pci.h>
Alan Cox72b43792010-10-27 15:33:23 -070029#include <linux/platform_device.h>
Alek Du8bf02612009-09-22 16:46:36 -070030#include <linux/kernel.h>
31#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/interrupt.h>
34#include <linux/init.h>
35#include <linux/irq.h>
36#include <linux/io.h>
37#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Kristen Carlson Accardi78128032011-05-10 14:23:45 +010039#include <linux/pm_runtime.h>
Mika Westerberg465f2bd2012-05-02 11:15:50 +030040#include <linux/irqdomain.h>
Alek Du8bf02612009-09-22 16:46:36 -070041
David Cohenf89a7682013-10-04 13:01:42 -070042#define INTEL_MID_IRQ_TYPE_EDGE (1 << 0)
43#define INTEL_MID_IRQ_TYPE_LEVEL (1 << 1)
David Cohend56d6b32013-10-04 13:01:40 -070044
Alek Du8081c842010-05-26 14:42:25 -070045/*
46 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
47 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
48 * registers to control them, so we only define the order here instead of a
49 * structure, to get a bit offset for a pin (use GPDR as an example):
50 *
51 * nreg = ngpio / 32;
52 * reg = offset / 32;
53 * bit = offset % 32;
54 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
55 *
56 * so the bit of reg_addr is to control pin offset's GPDR feature
57*/
58
59enum GPIO_REG {
60 GPLR = 0, /* pin level read-only */
61 GPDR, /* pin direction */
62 GPSR, /* pin set */
63 GPCR, /* pin clear */
64 GRER, /* rising edge detect */
65 GFER, /* falling edge detect */
66 GEDR, /* edge detect result */
Adrian Hunter8c0f7b12011-10-03 14:36:07 +030067 GAFR, /* alt function */
Alek Du8bf02612009-09-22 16:46:36 -070068};
69
David Cohenf89a7682013-10-04 13:01:42 -070070/* intel_mid gpio driver data */
71struct intel_mid_gpio_ddata {
David Cohend56d6b32013-10-04 13:01:40 -070072 u16 ngpio; /* number of gpio pins */
73 u32 gplr_offset; /* offset of first GPLR register from base */
74 u32 flis_base; /* base address of FLIS registers */
75 u32 flis_len; /* length of FLIS registers */
76 u32 (*get_flis_offset)(int gpio);
77 u32 chip_irq_type; /* chip interrupt type */
78};
79
David Cohenf89a7682013-10-04 13:01:42 -070080struct intel_mid_gpio {
Alek Du8bf02612009-09-22 16:46:36 -070081 struct gpio_chip chip;
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +030082 void __iomem *reg_base;
Alek Du8bf02612009-09-22 16:46:36 -070083 spinlock_t lock;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +010084 struct pci_dev *pdev;
Mika Westerberg465f2bd2012-05-02 11:15:50 +030085 struct irq_domain *domain;
Alek Du8bf02612009-09-22 16:46:36 -070086};
87
David Cohenf89a7682013-10-04 13:01:42 -070088#define to_intel_gpio_priv(chip) container_of(chip, struct intel_mid_gpio, chip)
David Cohen46ebfbc2012-12-20 14:45:51 -080089
Alek Du8081c842010-05-26 14:42:25 -070090static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
Andy Shevchenko611a4852013-05-22 13:20:14 +030091 enum GPIO_REG reg_type)
Alek Du8bf02612009-09-22 16:46:36 -070092{
David Cohenf89a7682013-10-04 13:01:42 -070093 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
Alek Du8081c842010-05-26 14:42:25 -070094 unsigned nreg = chip->ngpio / 32;
Alek Du8bf02612009-09-22 16:46:36 -070095 u8 reg = offset / 32;
Alek Du8bf02612009-09-22 16:46:36 -070096
David Cohenf89a7682013-10-04 13:01:42 -070097 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
Alek Du8081c842010-05-26 14:42:25 -070098}
99
Adrian Hunter8c0f7b12011-10-03 14:36:07 +0300100static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
101 enum GPIO_REG reg_type)
102{
David Cohenf89a7682013-10-04 13:01:42 -0700103 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
Adrian Hunter8c0f7b12011-10-03 14:36:07 +0300104 unsigned nreg = chip->ngpio / 32;
105 u8 reg = offset / 16;
Adrian Hunter8c0f7b12011-10-03 14:36:07 +0300106
David Cohenf89a7682013-10-04 13:01:42 -0700107 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
Adrian Hunter8c0f7b12011-10-03 14:36:07 +0300108}
109
David Cohenf89a7682013-10-04 13:01:42 -0700110static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
Adrian Hunter8c0f7b12011-10-03 14:36:07 +0300111{
112 void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
113 u32 value = readl(gafr);
114 int shift = (offset % 16) << 1, af = (value >> shift) & 3;
115
116 if (af) {
117 value &= ~(3 << shift);
118 writel(value, gafr);
119 }
120 return 0;
121}
122
David Cohenf89a7682013-10-04 13:01:42 -0700123static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
Alek Du8081c842010-05-26 14:42:25 -0700124{
125 void __iomem *gplr = gpio_reg(chip, offset, GPLR);
126
Alek Du8bf02612009-09-22 16:46:36 -0700127 return readl(gplr) & BIT(offset % 32);
128}
129
David Cohenf89a7682013-10-04 13:01:42 -0700130static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
Alek Du8bf02612009-09-22 16:46:36 -0700131{
Alek Du8bf02612009-09-22 16:46:36 -0700132 void __iomem *gpsr, *gpcr;
133
134 if (value) {
Alek Du8081c842010-05-26 14:42:25 -0700135 gpsr = gpio_reg(chip, offset, GPSR);
Alek Du8bf02612009-09-22 16:46:36 -0700136 writel(BIT(offset % 32), gpsr);
137 } else {
Alek Du8081c842010-05-26 14:42:25 -0700138 gpcr = gpio_reg(chip, offset, GPCR);
Alek Du8bf02612009-09-22 16:46:36 -0700139 writel(BIT(offset % 32), gpcr);
140 }
141}
142
David Cohenf89a7682013-10-04 13:01:42 -0700143static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
Alek Du8bf02612009-09-22 16:46:36 -0700144{
David Cohenf89a7682013-10-04 13:01:42 -0700145 struct intel_mid_gpio *priv = to_intel_gpio_priv(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 u32 value;
148 unsigned long flags;
Alek Du8bf02612009-09-22 16:46:36 -0700149
David Cohenf89a7682013-10-04 13:01:42 -0700150 if (priv->pdev)
151 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100152
David Cohenf89a7682013-10-04 13:01:42 -0700153 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700154 value = readl(gpdr);
155 value &= ~BIT(offset % 32);
156 writel(value, gpdr);
David Cohenf89a7682013-10-04 13:01:42 -0700157 spin_unlock_irqrestore(&priv->lock, flags);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100158
David Cohenf89a7682013-10-04 13:01:42 -0700159 if (priv->pdev)
160 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100161
Alek Du8bf02612009-09-22 16:46:36 -0700162 return 0;
163}
164
David Cohenf89a7682013-10-04 13:01:42 -0700165static int intel_gpio_direction_output(struct gpio_chip *chip,
Alek Du8bf02612009-09-22 16:46:36 -0700166 unsigned offset, int value)
167{
David Cohenf89a7682013-10-04 13:01:42 -0700168 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
Alek Du8081c842010-05-26 14:42:25 -0700169 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
Alek Du8bf02612009-09-22 16:46:36 -0700170 unsigned long flags;
Alek Du8bf02612009-09-22 16:46:36 -0700171
David Cohenf89a7682013-10-04 13:01:42 -0700172 intel_gpio_set(chip, offset, value);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100173
David Cohenf89a7682013-10-04 13:01:42 -0700174 if (priv->pdev)
175 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100176
David Cohenf89a7682013-10-04 13:01:42 -0700177 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700178 value = readl(gpdr);
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700179 value |= BIT(offset % 32);
Alek Du8bf02612009-09-22 16:46:36 -0700180 writel(value, gpdr);
David Cohenf89a7682013-10-04 13:01:42 -0700181 spin_unlock_irqrestore(&priv->lock, flags);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100182
David Cohenf89a7682013-10-04 13:01:42 -0700183 if (priv->pdev)
184 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100185
Alek Du8bf02612009-09-22 16:46:36 -0700186 return 0;
187}
188
David Cohenf89a7682013-10-04 13:01:42 -0700189static int intel_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
Alek Du8bf02612009-09-22 16:46:36 -0700190{
David Cohenf89a7682013-10-04 13:01:42 -0700191 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
192 return irq_create_mapping(priv->domain, offset);
Alek Du8bf02612009-09-22 16:46:36 -0700193}
194
David Cohenf89a7682013-10-04 13:01:42 -0700195static int intel_mid_irq_type(struct irq_data *d, unsigned type)
Alek Du8bf02612009-09-22 16:46:36 -0700196{
David Cohenf89a7682013-10-04 13:01:42 -0700197 struct intel_mid_gpio *priv = irq_data_get_irq_chip_data(d);
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300198 u32 gpio = irqd_to_hwirq(d);
Alek Du8bf02612009-09-22 16:46:36 -0700199 unsigned long flags;
200 u32 value;
David Cohenf89a7682013-10-04 13:01:42 -0700201 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
202 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
Alek Du8bf02612009-09-22 16:46:36 -0700203
David Cohenf89a7682013-10-04 13:01:42 -0700204 if (gpio >= priv->chip.ngpio)
Alek Du8bf02612009-09-22 16:46:36 -0700205 return -EINVAL;
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100206
David Cohenf89a7682013-10-04 13:01:42 -0700207 if (priv->pdev)
208 pm_runtime_get(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100209
David Cohenf89a7682013-10-04 13:01:42 -0700210 spin_lock_irqsave(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700211 if (type & IRQ_TYPE_EDGE_RISING)
212 value = readl(grer) | BIT(gpio % 32);
213 else
214 value = readl(grer) & (~BIT(gpio % 32));
215 writel(value, grer);
216
217 if (type & IRQ_TYPE_EDGE_FALLING)
218 value = readl(gfer) | BIT(gpio % 32);
219 else
220 value = readl(gfer) & (~BIT(gpio % 32));
221 writel(value, gfer);
David Cohenf89a7682013-10-04 13:01:42 -0700222 spin_unlock_irqrestore(&priv->lock, flags);
Alek Du8bf02612009-09-22 16:46:36 -0700223
David Cohenf89a7682013-10-04 13:01:42 -0700224 if (priv->pdev)
225 pm_runtime_put(&priv->pdev->dev);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100226
Alek Du8bf02612009-09-22 16:46:36 -0700227 return 0;
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700228}
Alek Du8bf02612009-09-22 16:46:36 -0700229
David Cohenf89a7682013-10-04 13:01:42 -0700230static void intel_mid_irq_unmask(struct irq_data *d)
Alek Du8bf02612009-09-22 16:46:36 -0700231{
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700232}
Alek Du8bf02612009-09-22 16:46:36 -0700233
David Cohenf89a7682013-10-04 13:01:42 -0700234static void intel_mid_irq_mask(struct irq_data *d)
Alek Du8bf02612009-09-22 16:46:36 -0700235{
Andrew Mortonfd0574c2010-10-27 15:33:22 -0700236}
Alek Du8bf02612009-09-22 16:46:36 -0700237
David Cohenf89a7682013-10-04 13:01:42 -0700238static struct irq_chip intel_mid_irqchip = {
239 .name = "INTEL_MID-GPIO",
240 .irq_mask = intel_mid_irq_mask,
241 .irq_unmask = intel_mid_irq_unmask,
242 .irq_set_type = intel_mid_irq_type,
Alek Du8bf02612009-09-22 16:46:36 -0700243};
244
David Cohenf89a7682013-10-04 13:01:42 -0700245static const struct intel_mid_gpio_ddata gpio_lincroft = {
David Cohend56d6b32013-10-04 13:01:40 -0700246 .ngpio = 64,
247};
248
David Cohenf89a7682013-10-04 13:01:42 -0700249static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
David Cohend56d6b32013-10-04 13:01:40 -0700250 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700251 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700252};
253
David Cohenf89a7682013-10-04 13:01:42 -0700254static const struct intel_mid_gpio_ddata gpio_penwell_core = {
David Cohend56d6b32013-10-04 13:01:40 -0700255 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700256 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700257};
258
David Cohenf89a7682013-10-04 13:01:42 -0700259static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
David Cohend56d6b32013-10-04 13:01:40 -0700260 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700261 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
David Cohend56d6b32013-10-04 13:01:40 -0700262};
263
David Cohenf89a7682013-10-04 13:01:42 -0700264static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
David Cohend56d6b32013-10-04 13:01:40 -0700265 .ngpio = 96,
David Cohenf89a7682013-10-04 13:01:42 -0700266 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700267};
268
David Cohenf89a7682013-10-04 13:01:42 -0700269static const struct intel_mid_gpio_ddata gpio_tangier = {
David Cohend56d6b32013-10-04 13:01:40 -0700270 .ngpio = 192,
271 .gplr_offset = 4,
272 .flis_base = 0xff0c0000,
273 .flis_len = 0x8000,
274 .get_flis_offset = NULL,
David Cohenf89a7682013-10-04 13:01:42 -0700275 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
David Cohend56d6b32013-10-04 13:01:40 -0700276};
277
David Cohenf89a7682013-10-04 13:01:42 -0700278static DEFINE_PCI_DEVICE_TABLE(intel_gpio_ids) = {
David Cohend56d6b32013-10-04 13:01:40 -0700279 {
280 /* Lincroft */
281 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
282 .driver_data = (kernel_ulong_t)&gpio_lincroft,
283 },
284 {
285 /* Penwell AON */
286 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
287 .driver_data = (kernel_ulong_t)&gpio_penwell_aon,
288 },
289 {
290 /* Penwell Core */
291 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
292 .driver_data = (kernel_ulong_t)&gpio_penwell_core,
293 },
294 {
295 /* Cloverview Aon */
296 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
297 .driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
298 },
299 {
300 /* Cloverview Core */
301 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
302 .driver_data = (kernel_ulong_t)&gpio_cloverview_core,
303 },
304 {
305 /* Tangier */
306 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x1199),
307 .driver_data = (kernel_ulong_t)&gpio_tangier,
308 },
309 { 0 }
Alek Du8bf02612009-09-22 16:46:36 -0700310};
David Cohenf89a7682013-10-04 13:01:42 -0700311MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
Alek Du8bf02612009-09-22 16:46:36 -0700312
David Cohenf89a7682013-10-04 13:01:42 -0700313static void intel_mid_irq_handler(unsigned irq, struct irq_desc *desc)
Alek Du8bf02612009-09-22 16:46:36 -0700314{
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000315 struct irq_data *data = irq_desc_get_irq_data(desc);
David Cohenf89a7682013-10-04 13:01:42 -0700316 struct intel_mid_gpio *priv = irq_data_get_irq_handler_data(data);
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000317 struct irq_chip *chip = irq_data_get_irq_chip(data);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000318 u32 base, gpio, mask;
Thomas Gleixner732063b2011-03-17 19:32:55 +0000319 unsigned long pending;
Alek Du8bf02612009-09-22 16:46:36 -0700320 void __iomem *gedr;
Alek Du8bf02612009-09-22 16:46:36 -0700321
322 /* check GPIO controller to check which pin triggered the interrupt */
David Cohenf89a7682013-10-04 13:01:42 -0700323 for (base = 0; base < priv->chip.ngpio; base += 32) {
324 gedr = gpio_reg(&priv->chip, base, GEDR);
Mika Westerbergc8f925b2012-05-10 13:01:22 +0300325 while ((pending = readl(gedr))) {
Mathias Nyman2345b202011-07-08 10:02:18 +0100326 gpio = __ffs(pending);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000327 mask = BIT(gpio);
Thomas Gleixner84bead62011-03-17 19:32:58 +0000328 /* Clear before handling so we can't lose an edge */
329 writel(mask, gedr);
David Cohenf89a7682013-10-04 13:01:42 -0700330 generic_handle_irq(irq_find_mapping(priv->domain,
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300331 base + gpio));
Thomas Gleixner732063b2011-03-17 19:32:55 +0000332 }
Alek Du8bf02612009-09-22 16:46:36 -0700333 }
Feng Tang0766d202011-01-25 15:07:15 -0800334
Thomas Gleixner20e2aa92011-03-17 19:32:49 +0000335 chip->irq_eoi(data);
Alek Du8bf02612009-09-22 16:46:36 -0700336}
337
David Cohenf89a7682013-10-04 13:01:42 -0700338static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
Mika Westerbergf5f93112012-04-05 12:15:17 +0300339{
340 void __iomem *reg;
341 unsigned base;
342
David Cohenf89a7682013-10-04 13:01:42 -0700343 for (base = 0; base < priv->chip.ngpio; base += 32) {
Mika Westerbergf5f93112012-04-05 12:15:17 +0300344 /* Clear the rising-edge detect register */
David Cohenf89a7682013-10-04 13:01:42 -0700345 reg = gpio_reg(&priv->chip, base, GRER);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300346 writel(0, reg);
347 /* Clear the falling-edge detect register */
David Cohenf89a7682013-10-04 13:01:42 -0700348 reg = gpio_reg(&priv->chip, base, GFER);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300349 writel(0, reg);
350 /* Clear the edge detect status register */
David Cohenf89a7682013-10-04 13:01:42 -0700351 reg = gpio_reg(&priv->chip, base, GEDR);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300352 writel(~0, reg);
353 }
354}
355
Linus Walleijba519dd2013-10-11 19:27:02 +0200356static int intel_gpio_irq_map(struct irq_domain *d, unsigned int irq,
357 irq_hw_number_t hwirq)
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300358{
David Cohenf89a7682013-10-04 13:01:42 -0700359 struct intel_mid_gpio *priv = d->host_data;
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300360
Linus Walleijba519dd2013-10-11 19:27:02 +0200361 irq_set_chip_and_handler_name(irq, &intel_mid_irqchip,
David Cohenf89a7682013-10-04 13:01:42 -0700362 handle_simple_irq, "demux");
Linus Walleijba519dd2013-10-11 19:27:02 +0200363 irq_set_chip_data(irq, priv);
364 irq_set_irq_type(irq, IRQ_TYPE_NONE);
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300365
366 return 0;
367}
368
David Cohenf89a7682013-10-04 13:01:42 -0700369static const struct irq_domain_ops intel_gpio_irq_ops = {
370 .map = intel_gpio_irq_map,
Mika Westerberg465f2bd2012-05-02 11:15:50 +0300371 .xlate = irq_domain_xlate_twocell,
372};
373
David Cohenf89a7682013-10-04 13:01:42 -0700374static int intel_gpio_runtime_idle(struct device *dev)
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100375{
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200376 pm_schedule_suspend(dev, 500);
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100377 return -EBUSY;
378}
379
David Cohenf89a7682013-10-04 13:01:42 -0700380static const struct dev_pm_ops intel_gpio_pm_ops = {
381 SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100382};
383
David Cohenf89a7682013-10-04 13:01:42 -0700384static int intel_gpio_probe(struct pci_dev *pdev,
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300385 const struct pci_device_id *id)
Alek Du8bf02612009-09-22 16:46:36 -0700386{
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300387 void __iomem *base;
David Cohenf89a7682013-10-04 13:01:42 -0700388 struct intel_mid_gpio *priv;
Alek Du8bf02612009-09-22 16:46:36 -0700389 u32 gpio_base;
David Cohen2519f9a2013-05-06 16:11:03 -0700390 u32 irq_base;
Julia Lawalld6a2b7b2012-08-05 11:52:34 +0200391 int retval;
David Cohenf89a7682013-10-04 13:01:42 -0700392 struct intel_mid_gpio_ddata *ddata =
393 (struct intel_mid_gpio_ddata *)id->driver_data;
Alek Du8bf02612009-09-22 16:46:36 -0700394
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300395 retval = pcim_enable_device(pdev);
Alek Du8bf02612009-09-22 16:46:36 -0700396 if (retval)
Mika Westerberg8302c742012-04-05 12:15:15 +0300397 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700398
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300399 retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
Alek Du8bf02612009-09-22 16:46:36 -0700400 if (retval) {
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300401 dev_err(&pdev->dev, "I/O memory mapping error\n");
402 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700403 }
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300404
405 base = pcim_iomap_table(pdev)[1];
Andy Shevchenko64c8cbc2013-05-22 13:20:11 +0300406
407 irq_base = readl(base);
408 gpio_base = readl(sizeof(u32) + base);
409
Alek Du8bf02612009-09-22 16:46:36 -0700410 /* release the IO mapping, since we already get the info from bar1 */
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300411 pcim_iounmap_regions(pdev, 1 << 1);
Alek Du8bf02612009-09-22 16:46:36 -0700412
David Cohenf89a7682013-10-04 13:01:42 -0700413 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
414 if (!priv) {
Andy Shevchenko8aca1192013-05-22 13:20:13 +0300415 dev_err(&pdev->dev, "can't allocate chip data\n");
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300416 return -ENOMEM;
Alek Du8bf02612009-09-22 16:46:36 -0700417 }
Mika Westerbergb3e35af2012-04-05 12:15:16 +0300418
David Cohenf89a7682013-10-04 13:01:42 -0700419 priv->reg_base = pcim_iomap_table(pdev)[0];
420 priv->chip.label = dev_name(&pdev->dev);
421 priv->chip.request = intel_gpio_request;
422 priv->chip.direction_input = intel_gpio_direction_input;
423 priv->chip.direction_output = intel_gpio_direction_output;
424 priv->chip.get = intel_gpio_get;
425 priv->chip.set = intel_gpio_set;
426 priv->chip.to_irq = intel_gpio_to_irq;
427 priv->chip.base = gpio_base;
428 priv->chip.ngpio = ddata->ngpio;
429 priv->chip.can_sleep = 0;
430 priv->pdev = pdev;
David Cohen2519f9a2013-05-06 16:11:03 -0700431
David Cohenf89a7682013-10-04 13:01:42 -0700432 spin_lock_init(&priv->lock);
Andy Shevchenkoaeb168f2013-05-22 13:20:10 +0300433
David Cohenf89a7682013-10-04 13:01:42 -0700434 priv->domain = irq_domain_add_simple(pdev->dev.of_node, ddata->ngpio,
435 irq_base, &intel_gpio_irq_ops, priv);
436 if (!priv->domain)
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300437 return -ENOMEM;
David Cohen2519f9a2013-05-06 16:11:03 -0700438
David Cohenf89a7682013-10-04 13:01:42 -0700439 pci_set_drvdata(pdev, priv);
440 retval = gpiochip_add(&priv->chip);
Alek Du8bf02612009-09-22 16:46:36 -0700441 if (retval) {
Andy Shevchenko8aca1192013-05-22 13:20:13 +0300442 dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
Andy Shevchenko786e07e2013-05-22 13:20:12 +0300443 return retval;
Alek Du8bf02612009-09-22 16:46:36 -0700444 }
Mika Westerbergf5f93112012-04-05 12:15:17 +0300445
David Cohenf89a7682013-10-04 13:01:42 -0700446 intel_mid_irq_init_hw(priv);
Mika Westerbergf5f93112012-04-05 12:15:17 +0300447
David Cohenf89a7682013-10-04 13:01:42 -0700448 irq_set_handler_data(pdev->irq, priv);
449 irq_set_chained_handler(pdev->irq, intel_mid_irq_handler);
Alek Du8bf02612009-09-22 16:46:36 -0700450
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100451 pm_runtime_put_noidle(&pdev->dev);
452 pm_runtime_allow(&pdev->dev);
453
Mika Westerberg8302c742012-04-05 12:15:15 +0300454 return 0;
Alek Du8bf02612009-09-22 16:46:36 -0700455}
456
David Cohenf89a7682013-10-04 13:01:42 -0700457static struct pci_driver intel_gpio_driver = {
458 .name = "intel_mid_gpio",
459 .id_table = intel_gpio_ids,
460 .probe = intel_gpio_probe,
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100461 .driver = {
David Cohenf89a7682013-10-04 13:01:42 -0700462 .pm = &intel_gpio_pm_ops,
Kristen Carlson Accardi78128032011-05-10 14:23:45 +0100463 },
Alek Du8bf02612009-09-22 16:46:36 -0700464};
465
David Cohenf89a7682013-10-04 13:01:42 -0700466static int __init intel_gpio_init(void)
Alek Du8bf02612009-09-22 16:46:36 -0700467{
David Cohenf89a7682013-10-04 13:01:42 -0700468 return pci_register_driver(&intel_gpio_driver);
Alek Du8bf02612009-09-22 16:46:36 -0700469}
470
David Cohenf89a7682013-10-04 13:01:42 -0700471device_initcall(intel_gpio_init);