Blame view

drivers/gpio/gpio-intel-mid.c 10.5 KB
c103de240   Grant Likely   gpio: reorganize ...
1
  /*
a0bbf0327   David Cohen   gpio: intel-mid: ...
2
   * Intel MID GPIO driver
c103de240   Grant Likely   gpio: reorganize ...
3
   *
3cabe87b5   Andy Shevchenko   gpio: intel-mid: ...
4
   * Copyright (c) 2008-2014,2016 Intel Corporation.
8bf026177   Alek Du   gpio: add Intel M...
5
6
7
8
9
10
11
12
13
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
8bf026177   Alek Du   gpio: add Intel M...
14
15
16
17
   */
  
  /* Supports:
   * Moorestown platform Langwell chip.
8081c84c9   Alek Du   gpio: add Penwell...
18
   * Medfield platform Penwell chip.
f89a768f1   David Cohen   gpio-intel-mid: u...
19
   * Clovertrail platform Cloverview chip.
8bf026177   Alek Du   gpio: add Intel M...
20
   */
8bf026177   Alek Du   gpio: add Intel M...
21
  #include <linux/delay.h>
8bf026177   Alek Du   gpio: add Intel M...
22
  #include <linux/init.h>
3cabe87b5   Andy Shevchenko   gpio: intel-mid: ...
23
  #include <linux/interrupt.h>
8bf026177   Alek Du   gpio: add Intel M...
24
  #include <linux/io.h>
3f7dbfd8e   Linus Walleij   gpio: intel-mid: ...
25
  #include <linux/gpio/driver.h>
3cabe87b5   Andy Shevchenko   gpio: intel-mid: ...
26
27
28
29
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/pci.h>
  #include <linux/platform_device.h>
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
30
  #include <linux/pm_runtime.h>
3cabe87b5   Andy Shevchenko   gpio: intel-mid: ...
31
32
  #include <linux/slab.h>
  #include <linux/stddef.h>
8bf026177   Alek Du   gpio: add Intel M...
33

f89a768f1   David Cohen   gpio-intel-mid: u...
34
35
  #define INTEL_MID_IRQ_TYPE_EDGE		(1 << 0)
  #define INTEL_MID_IRQ_TYPE_LEVEL	(1 << 1)
d56d6b3d7   David Cohen   gpio: langwell: a...
36

8081c84c9   Alek Du   gpio: add Penwell...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  /*
   * Langwell chip has 64 pins and thus there are 2 32bit registers to control
   * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
   * registers to control them, so we only define the order here instead of a
   * structure, to get a bit offset for a pin (use GPDR as an example):
   *
   * nreg = ngpio / 32;
   * reg = offset / 32;
   * bit = offset % 32;
   * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
   *
   * so the bit of reg_addr is to control pin offset's GPDR feature
  */
  
  enum GPIO_REG {
  	GPLR = 0,	/* pin level read-only */
  	GPDR,		/* pin direction */
  	GPSR,		/* pin set */
  	GPCR,		/* pin clear */
  	GRER,		/* rising edge detect */
  	GFER,		/* falling edge detect */
  	GEDR,		/* edge detect result */
8c0f7b10f   Adrian Hunter   gpio: langwell: e...
59
  	GAFR,		/* alt function */
8bf026177   Alek Du   gpio: add Intel M...
60
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
61
62
  /* intel_mid gpio driver data */
  struct intel_mid_gpio_ddata {
d56d6b3d7   David Cohen   gpio: langwell: a...
63
  	u16 ngpio;		/* number of gpio pins */
d56d6b3d7   David Cohen   gpio: langwell: a...
64
65
  	u32 chip_irq_type;	/* chip interrupt type */
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
66
  struct intel_mid_gpio {
8bf026177   Alek Du   gpio: add Intel M...
67
  	struct gpio_chip		chip;
64c8cbc17   Andy Shevchenko   gpio-langwell: do...
68
  	void __iomem			*reg_base;
8bf026177   Alek Du   gpio: add Intel M...
69
  	spinlock_t			lock;
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
70
  	struct pci_dev			*pdev;
8bf026177   Alek Du   gpio: add Intel M...
71
  };
8081c84c9   Alek Du   gpio: add Penwell...
72
  static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
611a485b5   Andy Shevchenko   gpio-langwell: dr...
73
  			      enum GPIO_REG reg_type)
8bf026177   Alek Du   gpio: add Intel M...
74
  {
5c77c0212   Linus Walleij   gpio: intel-mid: ...
75
  	struct intel_mid_gpio *priv = gpiochip_get_data(chip);
8081c84c9   Alek Du   gpio: add Penwell...
76
  	unsigned nreg = chip->ngpio / 32;
8bf026177   Alek Du   gpio: add Intel M...
77
  	u8 reg = offset / 32;
8081c84c9   Alek Du   gpio: add Penwell...
78

f89a768f1   David Cohen   gpio-intel-mid: u...
79
  	return priv->reg_base + reg_type * nreg * 4 + reg * 4;
8081c84c9   Alek Du   gpio: add Penwell...
80
  }
8c0f7b10f   Adrian Hunter   gpio: langwell: e...
81
82
83
  static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
  				   enum GPIO_REG reg_type)
  {
5c77c0212   Linus Walleij   gpio: intel-mid: ...
84
  	struct intel_mid_gpio *priv = gpiochip_get_data(chip);
8c0f7b10f   Adrian Hunter   gpio: langwell: e...
85
86
  	unsigned nreg = chip->ngpio / 32;
  	u8 reg = offset / 16;
8c0f7b10f   Adrian Hunter   gpio: langwell: e...
87

f89a768f1   David Cohen   gpio-intel-mid: u...
88
  	return priv->reg_base + reg_type * nreg * 4 + reg * 4;
8c0f7b10f   Adrian Hunter   gpio: langwell: e...
89
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
90
  static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
8c0f7b10f   Adrian Hunter   gpio: langwell: e...
91
92
93
94
95
96
97
98
99
100
101
  {
  	void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
  	u32 value = readl(gafr);
  	int shift = (offset % 16) << 1, af = (value >> shift) & 3;
  
  	if (af) {
  		value &= ~(3 << shift);
  		writel(value, gafr);
  	}
  	return 0;
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
102
  static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
8081c84c9   Alek Du   gpio: add Penwell...
103
104
  {
  	void __iomem *gplr = gpio_reg(chip, offset, GPLR);
8bf026177   Alek Du   gpio: add Intel M...
105

4c628f3de   Linus Walleij   gpio: intel-mid: ...
106
  	return !!(readl(gplr) & BIT(offset % 32));
8bf026177   Alek Du   gpio: add Intel M...
107
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
108
  static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
8bf026177   Alek Du   gpio: add Intel M...
109
  {
8bf026177   Alek Du   gpio: add Intel M...
110
111
112
  	void __iomem *gpsr, *gpcr;
  
  	if (value) {
8081c84c9   Alek Du   gpio: add Penwell...
113
  		gpsr = gpio_reg(chip, offset, GPSR);
8bf026177   Alek Du   gpio: add Intel M...
114
115
  		writel(BIT(offset % 32), gpsr);
  	} else {
8081c84c9   Alek Du   gpio: add Penwell...
116
  		gpcr = gpio_reg(chip, offset, GPCR);
8bf026177   Alek Du   gpio: add Intel M...
117
118
119
  		writel(BIT(offset % 32), gpcr);
  	}
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
120
  static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
8bf026177   Alek Du   gpio: add Intel M...
121
  {
5c77c0212   Linus Walleij   gpio: intel-mid: ...
122
  	struct intel_mid_gpio *priv = gpiochip_get_data(chip);
8081c84c9   Alek Du   gpio: add Penwell...
123
  	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
8bf026177   Alek Du   gpio: add Intel M...
124
125
  	u32 value;
  	unsigned long flags;
8bf026177   Alek Du   gpio: add Intel M...
126

f89a768f1   David Cohen   gpio-intel-mid: u...
127
128
  	if (priv->pdev)
  		pm_runtime_get(&priv->pdev->dev);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
129

f89a768f1   David Cohen   gpio-intel-mid: u...
130
  	spin_lock_irqsave(&priv->lock, flags);
8bf026177   Alek Du   gpio: add Intel M...
131
132
133
  	value = readl(gpdr);
  	value &= ~BIT(offset % 32);
  	writel(value, gpdr);
f89a768f1   David Cohen   gpio-intel-mid: u...
134
  	spin_unlock_irqrestore(&priv->lock, flags);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
135

f89a768f1   David Cohen   gpio-intel-mid: u...
136
137
  	if (priv->pdev)
  		pm_runtime_put(&priv->pdev->dev);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
138

8bf026177   Alek Du   gpio: add Intel M...
139
140
  	return 0;
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
141
  static int intel_gpio_direction_output(struct gpio_chip *chip,
8bf026177   Alek Du   gpio: add Intel M...
142
143
  			unsigned offset, int value)
  {
5c77c0212   Linus Walleij   gpio: intel-mid: ...
144
  	struct intel_mid_gpio *priv = gpiochip_get_data(chip);
8081c84c9   Alek Du   gpio: add Penwell...
145
  	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
8bf026177   Alek Du   gpio: add Intel M...
146
  	unsigned long flags;
8bf026177   Alek Du   gpio: add Intel M...
147

f89a768f1   David Cohen   gpio-intel-mid: u...
148
  	intel_gpio_set(chip, offset, value);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
149

f89a768f1   David Cohen   gpio-intel-mid: u...
150
151
  	if (priv->pdev)
  		pm_runtime_get(&priv->pdev->dev);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
152

f89a768f1   David Cohen   gpio-intel-mid: u...
153
  	spin_lock_irqsave(&priv->lock, flags);
8bf026177   Alek Du   gpio: add Intel M...
154
  	value = readl(gpdr);
6eab04a87   Justin P. Mattock   treewide: remove ...
155
  	value |= BIT(offset % 32);
8bf026177   Alek Du   gpio: add Intel M...
156
  	writel(value, gpdr);
f89a768f1   David Cohen   gpio-intel-mid: u...
157
  	spin_unlock_irqrestore(&priv->lock, flags);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
158

f89a768f1   David Cohen   gpio-intel-mid: u...
159
160
  	if (priv->pdev)
  		pm_runtime_put(&priv->pdev->dev);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
161

8bf026177   Alek Du   gpio: add Intel M...
162
163
  	return 0;
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
164
  static int intel_mid_irq_type(struct irq_data *d, unsigned type)
8bf026177   Alek Du   gpio: add Intel M...
165
  {
3f7dbfd8e   Linus Walleij   gpio: intel-mid: ...
166
  	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
5c77c0212   Linus Walleij   gpio: intel-mid: ...
167
  	struct intel_mid_gpio *priv = gpiochip_get_data(gc);
465f2bd45   Mika Westerberg   gpio/langwell: co...
168
  	u32 gpio = irqd_to_hwirq(d);
8bf026177   Alek Du   gpio: add Intel M...
169
170
  	unsigned long flags;
  	u32 value;
f89a768f1   David Cohen   gpio-intel-mid: u...
171
172
  	void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
  	void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
8bf026177   Alek Du   gpio: add Intel M...
173

f89a768f1   David Cohen   gpio-intel-mid: u...
174
  	if (gpio >= priv->chip.ngpio)
8bf026177   Alek Du   gpio: add Intel M...
175
  		return -EINVAL;
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
176

f89a768f1   David Cohen   gpio-intel-mid: u...
177
178
  	if (priv->pdev)
  		pm_runtime_get(&priv->pdev->dev);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
179

f89a768f1   David Cohen   gpio-intel-mid: u...
180
  	spin_lock_irqsave(&priv->lock, flags);
8bf026177   Alek Du   gpio: add Intel M...
181
182
183
184
185
186
187
188
189
190
191
  	if (type & IRQ_TYPE_EDGE_RISING)
  		value = readl(grer) | BIT(gpio % 32);
  	else
  		value = readl(grer) & (~BIT(gpio % 32));
  	writel(value, grer);
  
  	if (type & IRQ_TYPE_EDGE_FALLING)
  		value = readl(gfer) | BIT(gpio % 32);
  	else
  		value = readl(gfer) & (~BIT(gpio % 32));
  	writel(value, gfer);
f89a768f1   David Cohen   gpio-intel-mid: u...
192
  	spin_unlock_irqrestore(&priv->lock, flags);
8bf026177   Alek Du   gpio: add Intel M...
193

f89a768f1   David Cohen   gpio-intel-mid: u...
194
195
  	if (priv->pdev)
  		pm_runtime_put(&priv->pdev->dev);
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
196

8bf026177   Alek Du   gpio: add Intel M...
197
  	return 0;
fd0574cb5   Andrew Morton   drivers/gpio/lang...
198
  }
8bf026177   Alek Du   gpio: add Intel M...
199

f89a768f1   David Cohen   gpio-intel-mid: u...
200
  static void intel_mid_irq_unmask(struct irq_data *d)
8bf026177   Alek Du   gpio: add Intel M...
201
  {
fd0574cb5   Andrew Morton   drivers/gpio/lang...
202
  }
8bf026177   Alek Du   gpio: add Intel M...
203

f89a768f1   David Cohen   gpio-intel-mid: u...
204
  static void intel_mid_irq_mask(struct irq_data *d)
8bf026177   Alek Du   gpio: add Intel M...
205
  {
fd0574cb5   Andrew Morton   drivers/gpio/lang...
206
  }
8bf026177   Alek Du   gpio: add Intel M...
207

f89a768f1   David Cohen   gpio-intel-mid: u...
208
209
210
211
212
  static struct irq_chip intel_mid_irqchip = {
  	.name		= "INTEL_MID-GPIO",
  	.irq_mask	= intel_mid_irq_mask,
  	.irq_unmask	= intel_mid_irq_unmask,
  	.irq_set_type	= intel_mid_irq_type,
8bf026177   Alek Du   gpio: add Intel M...
213
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
214
  static const struct intel_mid_gpio_ddata gpio_lincroft = {
d56d6b3d7   David Cohen   gpio: langwell: a...
215
216
  	.ngpio = 64,
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
217
  static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
d56d6b3d7   David Cohen   gpio: langwell: a...
218
  	.ngpio = 96,
f89a768f1   David Cohen   gpio-intel-mid: u...
219
  	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
d56d6b3d7   David Cohen   gpio: langwell: a...
220
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
221
  static const struct intel_mid_gpio_ddata gpio_penwell_core = {
d56d6b3d7   David Cohen   gpio: langwell: a...
222
  	.ngpio = 96,
f89a768f1   David Cohen   gpio-intel-mid: u...
223
  	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
d56d6b3d7   David Cohen   gpio: langwell: a...
224
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
225
  static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
d56d6b3d7   David Cohen   gpio: langwell: a...
226
  	.ngpio = 96,
f89a768f1   David Cohen   gpio-intel-mid: u...
227
  	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
d56d6b3d7   David Cohen   gpio: langwell: a...
228
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
229
  static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
d56d6b3d7   David Cohen   gpio: langwell: a...
230
  	.ngpio = 96,
f89a768f1   David Cohen   gpio-intel-mid: u...
231
  	.chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
d56d6b3d7   David Cohen   gpio: langwell: a...
232
  };
14f4a8838   Jingoo Han   gpio: remove DEFI...
233
  static const struct pci_device_id intel_gpio_ids[] = {
d56d6b3d7   David Cohen   gpio: langwell: a...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  	{
  		/* Lincroft */
  		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
  		.driver_data = (kernel_ulong_t)&gpio_lincroft,
  	},
  	{
  		/* Penwell AON */
  		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
  		.driver_data = (kernel_ulong_t)&gpio_penwell_aon,
  	},
  	{
  		/* Penwell Core */
  		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
  		.driver_data = (kernel_ulong_t)&gpio_penwell_core,
  	},
  	{
  		/* Cloverview Aon */
  		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
  		.driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
  	},
  	{
  		/* Cloverview Core */
  		PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
  		.driver_data = (kernel_ulong_t)&gpio_cloverview_core,
  	},
d56d6b3d7   David Cohen   gpio: langwell: a...
259
  	{ 0 }
8bf026177   Alek Du   gpio: add Intel M...
260
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
261
  MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
8bf026177   Alek Du   gpio: add Intel M...
262

bd0b9ac40   Thomas Gleixner   genirq: Remove ir...
263
  static void intel_mid_irq_handler(struct irq_desc *desc)
8bf026177   Alek Du   gpio: add Intel M...
264
  {
3f7dbfd8e   Linus Walleij   gpio: intel-mid: ...
265
  	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
5c77c0212   Linus Walleij   gpio: intel-mid: ...
266
  	struct intel_mid_gpio *priv = gpiochip_get_data(gc);
20e2aa916   Thomas Gleixner   gpio/langwell: Fi...
267
  	struct irq_data *data = irq_desc_get_irq_data(desc);
20e2aa916   Thomas Gleixner   gpio/langwell: Fi...
268
  	struct irq_chip *chip = irq_data_get_irq_chip(data);
84bead6c3   Thomas Gleixner   gpio/langwell: Cl...
269
  	u32 base, gpio, mask;
732063b92   Thomas Gleixner   gpio/langwell: Si...
270
  	unsigned long pending;
8bf026177   Alek Du   gpio: add Intel M...
271
  	void __iomem *gedr;
8bf026177   Alek Du   gpio: add Intel M...
272
273
  
  	/* check GPIO controller to check which pin triggered the interrupt */
f89a768f1   David Cohen   gpio-intel-mid: u...
274
275
  	for (base = 0; base < priv->chip.ngpio; base += 32) {
  		gedr = gpio_reg(&priv->chip, base, GEDR);
c8f925b69   Mika Westerberg   gpio/langwell: re...
276
  		while ((pending = readl(gedr))) {
2345b20fd   Mathias Nyman   gpio/langwell_gpi...
277
  			gpio = __ffs(pending);
84bead6c3   Thomas Gleixner   gpio/langwell: Cl...
278
  			mask = BIT(gpio);
84bead6c3   Thomas Gleixner   gpio/langwell: Cl...
279
280
  			/* Clear before handling so we can't lose an edge */
  			writel(mask, gedr);
3f7dbfd8e   Linus Walleij   gpio: intel-mid: ...
281
  			generic_handle_irq(irq_find_mapping(gc->irqdomain,
465f2bd45   Mika Westerberg   gpio/langwell: co...
282
  							    base + gpio));
732063b92   Thomas Gleixner   gpio/langwell: Si...
283
  		}
8bf026177   Alek Du   gpio: add Intel M...
284
  	}
0766d20fd   Feng Tang   langwell_gpio: mo...
285

20e2aa916   Thomas Gleixner   gpio/langwell: Fi...
286
  	chip->irq_eoi(data);
8bf026177   Alek Du   gpio: add Intel M...
287
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
288
  static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
f5f93117f   Mika Westerberg   gpio/langwell: cl...
289
290
291
  {
  	void __iomem *reg;
  	unsigned base;
f89a768f1   David Cohen   gpio-intel-mid: u...
292
  	for (base = 0; base < priv->chip.ngpio; base += 32) {
f5f93117f   Mika Westerberg   gpio/langwell: cl...
293
  		/* Clear the rising-edge detect register */
f89a768f1   David Cohen   gpio-intel-mid: u...
294
  		reg = gpio_reg(&priv->chip, base, GRER);
f5f93117f   Mika Westerberg   gpio/langwell: cl...
295
296
  		writel(0, reg);
  		/* Clear the falling-edge detect register */
f89a768f1   David Cohen   gpio-intel-mid: u...
297
  		reg = gpio_reg(&priv->chip, base, GFER);
f5f93117f   Mika Westerberg   gpio/langwell: cl...
298
299
  		writel(0, reg);
  		/* Clear the edge detect status register */
f89a768f1   David Cohen   gpio-intel-mid: u...
300
  		reg = gpio_reg(&priv->chip, base, GEDR);
f5f93117f   Mika Westerberg   gpio/langwell: cl...
301
302
303
  		writel(~0, reg);
  	}
  }
fbc2a294f   Augusto Mecking Caringi   gpio: intel-mid: ...
304
  static int __maybe_unused intel_gpio_runtime_idle(struct device *dev)
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
305
  {
84a34575f   xinhui.pan   gpio: intel-mid: ...
306
307
  	int err = pm_schedule_suspend(dev, 500);
  	return err ?: -EBUSY;
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
308
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
309
310
  static const struct dev_pm_ops intel_gpio_pm_ops = {
  	SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
311
  };
f89a768f1   David Cohen   gpio-intel-mid: u...
312
  static int intel_gpio_probe(struct pci_dev *pdev,
64c8cbc17   Andy Shevchenko   gpio-langwell: do...
313
  			  const struct pci_device_id *id)
8bf026177   Alek Du   gpio: add Intel M...
314
  {
64c8cbc17   Andy Shevchenko   gpio-langwell: do...
315
  	void __iomem *base;
f89a768f1   David Cohen   gpio-intel-mid: u...
316
  	struct intel_mid_gpio *priv;
8bf026177   Alek Du   gpio: add Intel M...
317
  	u32 gpio_base;
2519f9abc   David Cohen   gpio-langwell: fi...
318
  	u32 irq_base;
d6a2b7ba6   Julia Lawall   drivers/gpio/gpio...
319
  	int retval;
f89a768f1   David Cohen   gpio-intel-mid: u...
320
321
  	struct intel_mid_gpio_ddata *ddata =
  				(struct intel_mid_gpio_ddata *)id->driver_data;
8bf026177   Alek Du   gpio: add Intel M...
322

786e07ecb   Andy Shevchenko   gpio-langwell: us...
323
  	retval = pcim_enable_device(pdev);
8bf026177   Alek Du   gpio: add Intel M...
324
  	if (retval)
8302c7413   Mika Westerberg   gpio/langwell: us...
325
  		return retval;
8bf026177   Alek Du   gpio: add Intel M...
326

786e07ecb   Andy Shevchenko   gpio-langwell: us...
327
  	retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
8bf026177   Alek Du   gpio: add Intel M...
328
  	if (retval) {
786e07ecb   Andy Shevchenko   gpio-langwell: us...
329
330
331
  		dev_err(&pdev->dev, "I/O memory mapping error
  ");
  		return retval;
8bf026177   Alek Du   gpio: add Intel M...
332
  	}
64c8cbc17   Andy Shevchenko   gpio-langwell: do...
333

786e07ecb   Andy Shevchenko   gpio-langwell: us...
334
  	base = pcim_iomap_table(pdev)[1];
64c8cbc17   Andy Shevchenko   gpio-langwell: do...
335
336
  	irq_base = readl(base);
  	gpio_base = readl(sizeof(u32) + base);
8bf026177   Alek Du   gpio: add Intel M...
337
  	/* release the IO mapping, since we already get the info from bar1 */
786e07ecb   Andy Shevchenko   gpio-langwell: us...
338
  	pcim_iounmap_regions(pdev, 1 << 1);
8bf026177   Alek Du   gpio: add Intel M...
339

f89a768f1   David Cohen   gpio-intel-mid: u...
340
341
  	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  	if (!priv) {
8aca119f5   Andy Shevchenko   gpio-langwell: am...
342
343
  		dev_err(&pdev->dev, "can't allocate chip data
  ");
786e07ecb   Andy Shevchenko   gpio-langwell: us...
344
  		return -ENOMEM;
8bf026177   Alek Du   gpio: add Intel M...
345
  	}
b3e35af2b   Mika Westerberg   gpio/langwell: al...
346

f89a768f1   David Cohen   gpio-intel-mid: u...
347
348
  	priv->reg_base = pcim_iomap_table(pdev)[0];
  	priv->chip.label = dev_name(&pdev->dev);
58383c784   Linus Walleij   gpio: change memb...
349
  	priv->chip.parent = &pdev->dev;
f89a768f1   David Cohen   gpio-intel-mid: u...
350
351
352
353
354
  	priv->chip.request = intel_gpio_request;
  	priv->chip.direction_input = intel_gpio_direction_input;
  	priv->chip.direction_output = intel_gpio_direction_output;
  	priv->chip.get = intel_gpio_get;
  	priv->chip.set = intel_gpio_set;
f89a768f1   David Cohen   gpio-intel-mid: u...
355
356
  	priv->chip.base = gpio_base;
  	priv->chip.ngpio = ddata->ngpio;
9fb1f39eb   Linus Walleij   gpio/pinctrl: mak...
357
  	priv->chip.can_sleep = false;
f89a768f1   David Cohen   gpio-intel-mid: u...
358
359
360
  	priv->pdev = pdev;
  
  	spin_lock_init(&priv->lock);
f89a768f1   David Cohen   gpio-intel-mid: u...
361
  	pci_set_drvdata(pdev, priv);
dd3b204af   Andy Shevchenko   gpio: intel-mid: ...
362
  	retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
8bf026177   Alek Du   gpio: add Intel M...
363
  	if (retval) {
8aca119f5   Andy Shevchenko   gpio-langwell: am...
364
365
  		dev_err(&pdev->dev, "gpiochip_add error %d
  ", retval);
786e07ecb   Andy Shevchenko   gpio-langwell: us...
366
  		return retval;
8bf026177   Alek Du   gpio: add Intel M...
367
  	}
f5f93117f   Mika Westerberg   gpio/langwell: cl...
368

3f7dbfd8e   Linus Walleij   gpio: intel-mid: ...
369
370
371
372
373
374
375
376
377
378
379
  	retval = gpiochip_irqchip_add(&priv->chip,
  				      &intel_mid_irqchip,
  				      irq_base,
  				      handle_simple_irq,
  				      IRQ_TYPE_NONE);
  	if (retval) {
  		dev_err(&pdev->dev,
  			"could not connect irqchip to gpiochip
  ");
  		return retval;
  	}
f89a768f1   David Cohen   gpio-intel-mid: u...
380
  	intel_mid_irq_init_hw(priv);
f5f93117f   Mika Westerberg   gpio/langwell: cl...
381

3f7dbfd8e   Linus Walleij   gpio: intel-mid: ...
382
383
384
385
  	gpiochip_set_chained_irqchip(&priv->chip,
  				     &intel_mid_irqchip,
  				     pdev->irq,
  				     intel_mid_irq_handler);
8bf026177   Alek Du   gpio: add Intel M...
386

7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
387
388
  	pm_runtime_put_noidle(&pdev->dev);
  	pm_runtime_allow(&pdev->dev);
8302c7413   Mika Westerberg   gpio/langwell: us...
389
  	return 0;
8bf026177   Alek Du   gpio: add Intel M...
390
  }
f89a768f1   David Cohen   gpio-intel-mid: u...
391
392
393
394
  static struct pci_driver intel_gpio_driver = {
  	.name		= "intel_mid_gpio",
  	.id_table	= intel_gpio_ids,
  	.probe		= intel_gpio_probe,
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
395
  	.driver		= {
f89a768f1   David Cohen   gpio-intel-mid: u...
396
  		.pm	= &intel_gpio_pm_ops,
7812803a3   Kristen Carlson Accardi   langwell_gpio: ad...
397
  	},
8bf026177   Alek Du   gpio: add Intel M...
398
  };
5261bee8f   Geliang Tang   gpio: intel-mid: ...
399
  builtin_pci_driver(intel_gpio_driver);