Blame view

drivers/gpio/gpio-ml-ioh.c 14.8 KB
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; version 2 of the License.
   *
   * 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.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
   */
bb207ef1e   Paul Gortmaker   drivers/gpio: Fix...
17
  #include <linux/module.h>
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
18
  #include <linux/kernel.h>
545554e7c   Andrew Morton   drivers/gpio/ml_i...
19
  #include <linux/slab.h>
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
20
21
  #include <linux/pci.h>
  #include <linux/gpio.h>
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
22
23
24
25
26
27
28
29
30
31
32
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  
  #define IOH_EDGE_FALLING	0
  #define IOH_EDGE_RISING		BIT(0)
  #define IOH_LEVEL_L		BIT(1)
  #define IOH_LEVEL_H		(BIT(0) | BIT(1))
  #define IOH_EDGE_BOTH		BIT(2)
  #define IOH_IM_MASK		(BIT(0) | BIT(1) | BIT(2))
  
  #define IOH_IRQ_BASE		0
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  
  #define PCI_VENDOR_ID_ROHM             0x10DB
  
  struct ioh_reg_comn {
  	u32	ien;
  	u32	istatus;
  	u32	idisp;
  	u32	iclr;
  	u32	imask;
  	u32	imaskclr;
  	u32	po;
  	u32	pi;
  	u32	pm;
  	u32	im_0;
  	u32	im_1;
  	u32	reserved;
  };
  
  struct ioh_regs {
  	struct ioh_reg_comn regs[8];
  	u32 reserve1[16];
  	u32 ioh_sel_reg[4];
  	u32 reserve2[11];
  	u32 srst;
  };
  
  /**
   * struct ioh_gpio_reg_data - The register store data.
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
61
62
   * @ien_reg	To store contents of interrupt enable register.
   * @imask_reg:	To store contents of interrupt mask regist
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
63
64
   * @po_reg:	To store contents of PO register.
   * @pm_reg:	To store contents of PM register.
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
65
66
   * @im0_reg:	To store contents of interrupt mode regist0
   * @im1_reg:	To store contents of interrupt mode regist1
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
67
   * @use_sel_reg: To store contents of GPIO_USE_SEL0~3
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
68
69
   */
  struct ioh_gpio_reg_data {
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
70
71
  	u32 ien_reg;
  	u32 imask_reg;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
72
73
  	u32 po_reg;
  	u32 pm_reg;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
74
75
  	u32 im0_reg;
  	u32 im1_reg;
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
76
  	u32 use_sel_reg;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
77
78
79
80
81
82
83
84
85
86
  };
  
  /**
   * struct ioh_gpio - GPIO private data structure.
   * @base:			PCI base address of Memory mapped I/O register.
   * @reg:			Memory mapped IOH GPIO register list.
   * @dev:			Pointer to device structure.
   * @gpio:			Data for GPIO infrastructure.
   * @ioh_gpio_reg:		Memory mapped Register data is saved here
   *				when suspend.
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
87
   * @gpio_use_sel:		Save GPIO_USE_SEL1~4 register for PM
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
88
   * @ch:				Indicate GPIO channel
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
89
   * @irq_base:		Save base of IRQ number for interrupt
02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
90
   * @spinlock:		Used for register access protection
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
91
92
93
94
95
96
97
   */
  struct ioh_gpio {
  	void __iomem *base;
  	struct ioh_regs __iomem *reg;
  	struct device *dev;
  	struct gpio_chip gpio;
  	struct ioh_gpio_reg_data ioh_gpio_reg;
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
98
  	u32 gpio_use_sel;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
99
  	int ch;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
100
101
  	int irq_base;
  	spinlock_t spinlock;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
102
103
104
105
106
107
108
  };
  
  static const int num_ports[] = {6, 12, 16, 16, 15, 16, 16, 12};
  
  static void ioh_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
  {
  	u32 reg_val;
47315578b   Linus Walleij   gpio: ml-ioh: use...
109
  	struct ioh_gpio *chip =	gpiochip_get_data(gpio);
02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
110
  	unsigned long flags;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
111

02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
112
  	spin_lock_irqsave(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
113
114
115
116
117
118
119
  	reg_val = ioread32(&chip->reg->regs[chip->ch].po);
  	if (val)
  		reg_val |= (1 << nr);
  	else
  		reg_val &= ~(1 << nr);
  
  	iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
120
  	spin_unlock_irqrestore(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
121
122
123
124
  }
  
  static int ioh_gpio_get(struct gpio_chip *gpio, unsigned nr)
  {
47315578b   Linus Walleij   gpio: ml-ioh: use...
125
  	struct ioh_gpio *chip =	gpiochip_get_data(gpio);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
126

6f14dd696   Linus Walleij   gpio: ml-ioh: Be ...
127
  	return !!(ioread32(&chip->reg->regs[chip->ch].pi) & (1 << nr));
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
128
129
130
131
132
  }
  
  static int ioh_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  				     int val)
  {
47315578b   Linus Walleij   gpio: ml-ioh: use...
133
  	struct ioh_gpio *chip =	gpiochip_get_data(gpio);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
134
135
  	u32 pm;
  	u32 reg_val;
02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
136
  	unsigned long flags;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
137

02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
138
  	spin_lock_irqsave(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
139
140
141
142
143
144
145
146
147
148
  	pm = ioread32(&chip->reg->regs[chip->ch].pm) &
  					((1 << num_ports[chip->ch]) - 1);
  	pm |= (1 << nr);
  	iowrite32(pm, &chip->reg->regs[chip->ch].pm);
  
  	reg_val = ioread32(&chip->reg->regs[chip->ch].po);
  	if (val)
  		reg_val |= (1 << nr);
  	else
  		reg_val &= ~(1 << nr);
ba4386127   Peter Tyser   gpio/ml_ioh_gpio:...
149
  	iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
150

02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
151
  	spin_unlock_irqrestore(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
152
153
154
155
156
157
  
  	return 0;
  }
  
  static int ioh_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  {
47315578b   Linus Walleij   gpio: ml-ioh: use...
158
  	struct ioh_gpio *chip =	gpiochip_get_data(gpio);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
159
  	u32 pm;
02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
160
  	unsigned long flags;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
161

02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
162
  	spin_lock_irqsave(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
163
164
165
166
  	pm = ioread32(&chip->reg->regs[chip->ch].pm) &
  				((1 << num_ports[chip->ch]) - 1);
  	pm &= ~(1 << nr);
  	iowrite32(pm, &chip->reg->regs[chip->ch].pm);
02a6794d5   Axel Lin   gpio: gpio-ml-ioh...
167
  	spin_unlock_irqrestore(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
168
169
170
  
  	return 0;
  }
545554e7c   Andrew Morton   drivers/gpio/ml_i...
171
  #ifdef CONFIG_PM
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
172
173
174
175
176
  /*
   * Save register configuration and disable interrupts.
   */
  static void ioh_gpio_save_reg_conf(struct ioh_gpio *chip)
  {
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  	int i;
  
  	for (i = 0; i < 8; i ++, chip++) {
  		chip->ioh_gpio_reg.po_reg =
  					ioread32(&chip->reg->regs[chip->ch].po);
  		chip->ioh_gpio_reg.pm_reg =
  					ioread32(&chip->reg->regs[chip->ch].pm);
  		chip->ioh_gpio_reg.ien_reg =
  				       ioread32(&chip->reg->regs[chip->ch].ien);
  		chip->ioh_gpio_reg.imask_reg =
  				     ioread32(&chip->reg->regs[chip->ch].imask);
  		chip->ioh_gpio_reg.im0_reg =
  				      ioread32(&chip->reg->regs[chip->ch].im_0);
  		chip->ioh_gpio_reg.im1_reg =
  				      ioread32(&chip->reg->regs[chip->ch].im_1);
  		if (i < 4)
  			chip->ioh_gpio_reg.use_sel_reg =
  					   ioread32(&chip->reg->ioh_sel_reg[i]);
  	}
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
196
197
198
199
200
201
202
  }
  
  /*
   * This function restores the register configuration of the GPIO device.
   */
  static void ioh_gpio_restore_reg_conf(struct ioh_gpio *chip)
  {
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  	int i;
  
  	for (i = 0; i < 8; i ++, chip++) {
  		iowrite32(chip->ioh_gpio_reg.po_reg,
  			  &chip->reg->regs[chip->ch].po);
  		iowrite32(chip->ioh_gpio_reg.pm_reg,
  			  &chip->reg->regs[chip->ch].pm);
  		iowrite32(chip->ioh_gpio_reg.ien_reg,
  			  &chip->reg->regs[chip->ch].ien);
  		iowrite32(chip->ioh_gpio_reg.imask_reg,
  			  &chip->reg->regs[chip->ch].imask);
  		iowrite32(chip->ioh_gpio_reg.im0_reg,
  			  &chip->reg->regs[chip->ch].im_0);
  		iowrite32(chip->ioh_gpio_reg.im1_reg,
  			  &chip->reg->regs[chip->ch].im_1);
  		if (i < 4)
  			iowrite32(chip->ioh_gpio_reg.use_sel_reg,
  				  &chip->reg->ioh_sel_reg[i]);
  	}
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
222
  }
545554e7c   Andrew Morton   drivers/gpio/ml_i...
223
  #endif
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
224

54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
225
226
  static int ioh_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
  {
47315578b   Linus Walleij   gpio: ml-ioh: use...
227
  	struct ioh_gpio *chip = gpiochip_get_data(gpio);
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
228
229
  	return chip->irq_base + offset;
  }
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
230
231
232
233
234
235
236
237
238
239
240
241
242
  static void ioh_gpio_setup(struct ioh_gpio *chip, int num_port)
  {
  	struct gpio_chip *gpio = &chip->gpio;
  
  	gpio->label = dev_name(chip->dev);
  	gpio->owner = THIS_MODULE;
  	gpio->direction_input = ioh_gpio_direction_input;
  	gpio->get = ioh_gpio_get;
  	gpio->direction_output = ioh_gpio_direction_output;
  	gpio->set = ioh_gpio_set;
  	gpio->dbg_show = NULL;
  	gpio->base = -1;
  	gpio->ngpio = num_port;
9fb1f39eb   Linus Walleij   gpio/pinctrl: mak...
243
  	gpio->can_sleep = false;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
244
245
246
247
248
249
  	gpio->to_irq = ioh_gpio_to_irq;
  }
  
  static int ioh_irq_type(struct irq_data *d, unsigned int type)
  {
  	u32 im;
dd9328a6b   Márton Németh   gpio-ml-ioh: clea...
250
  	void __iomem *im_reg;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  	u32 ien;
  	u32 im_pos;
  	int ch;
  	unsigned long flags;
  	u32 val;
  	int irq = d->irq;
  	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  	struct ioh_gpio *chip = gc->private;
  
  	ch = irq - chip->irq_base;
  	if (irq <= chip->irq_base + 7) {
  		im_reg = &chip->reg->regs[chip->ch].im_0;
  		im_pos = ch;
  	} else {
  		im_reg = &chip->reg->regs[chip->ch].im_1;
  		im_pos = ch - 8;
  	}
  	dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d type=%d
  ",
  		__func__, irq, type, ch, im_pos, type);
  
  	spin_lock_irqsave(&chip->spinlock, flags);
  
  	switch (type) {
  	case IRQ_TYPE_EDGE_RISING:
  		val = IOH_EDGE_RISING;
  		break;
  	case IRQ_TYPE_EDGE_FALLING:
  		val = IOH_EDGE_FALLING;
  		break;
  	case IRQ_TYPE_EDGE_BOTH:
  		val = IOH_EDGE_BOTH;
  		break;
  	case IRQ_TYPE_LEVEL_HIGH:
  		val = IOH_LEVEL_H;
  		break;
  	case IRQ_TYPE_LEVEL_LOW:
  		val = IOH_LEVEL_L;
  		break;
  	case IRQ_TYPE_PROBE:
  		goto end;
  	default:
  		dev_warn(chip->dev, "%s: unknown type(%dd)",
  			__func__, type);
  		goto end;
  	}
  
  	/* Set interrupt mode */
  	im = ioread32(im_reg) & ~(IOH_IM_MASK << (im_pos * 4));
  	iowrite32(im | (val << (im_pos * 4)), im_reg);
  
  	/* iclr */
  	iowrite32(BIT(ch), &chip->reg->regs[chip->ch].iclr);
  
  	/* IMASKCLR */
  	iowrite32(BIT(ch), &chip->reg->regs[chip->ch].imaskclr);
  
  	/* Enable interrupt */
  	ien = ioread32(&chip->reg->regs[chip->ch].ien);
  	iowrite32(ien | BIT(ch), &chip->reg->regs[chip->ch].ien);
  end:
  	spin_unlock_irqrestore(&chip->spinlock, flags);
  
  	return 0;
  }
  
  static void ioh_irq_unmask(struct irq_data *d)
  {
  	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  	struct ioh_gpio *chip = gc->private;
  
  	iowrite32(1 << (d->irq - chip->irq_base),
  		  &chip->reg->regs[chip->ch].imaskclr);
  }
  
  static void ioh_irq_mask(struct irq_data *d)
  {
  	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  	struct ioh_gpio *chip = gc->private;
  
  	iowrite32(1 << (d->irq - chip->irq_base),
  		  &chip->reg->regs[chip->ch].imask);
  }
4d052213f   Feng Tang   gpio-ml-ioh: Add ...
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  static void ioh_irq_disable(struct irq_data *d)
  {
  	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  	struct ioh_gpio *chip = gc->private;
  	unsigned long flags;
  	u32 ien;
  
  	spin_lock_irqsave(&chip->spinlock, flags);
  	ien = ioread32(&chip->reg->regs[chip->ch].ien);
  	ien &= ~(1 << (d->irq - chip->irq_base));
  	iowrite32(ien, &chip->reg->regs[chip->ch].ien);
  	spin_unlock_irqrestore(&chip->spinlock, flags);
  }
  
  static void ioh_irq_enable(struct irq_data *d)
  {
  	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  	struct ioh_gpio *chip = gc->private;
  	unsigned long flags;
  	u32 ien;
  
  	spin_lock_irqsave(&chip->spinlock, flags);
  	ien = ioread32(&chip->reg->regs[chip->ch].ien);
  	ien |= 1 << (d->irq - chip->irq_base);
  	iowrite32(ien, &chip->reg->regs[chip->ch].ien);
  	spin_unlock_irqrestore(&chip->spinlock, flags);
  }
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
361
362
363
364
365
366
  static irqreturn_t ioh_gpio_handler(int irq, void *dev_id)
  {
  	struct ioh_gpio *chip = dev_id;
  	u32 reg_val;
  	int i, j;
  	int ret = IRQ_NONE;
f9ea14efa   Feng Tang   gpio-ml-ioh: fix ...
367
  	for (i = 0; i < 8; i++, chip++) {
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  		reg_val = ioread32(&chip->reg->regs[i].istatus);
  		for (j = 0; j < num_ports[i]; j++) {
  			if (reg_val & BIT(j)) {
  				dev_dbg(chip->dev,
  					"%s:[%d]:irq=%d status=0x%x
  ",
  					__func__, j, irq, reg_val);
  				iowrite32(BIT(j),
  					  &chip->reg->regs[chip->ch].iclr);
  				generic_handle_irq(chip->irq_base + j);
  				ret = IRQ_HANDLED;
  			}
  		}
  	}
  	return ret;
  }
3836309d9   Bill Pemberton   gpio: remove use ...
384
  static void ioh_gpio_alloc_generic_chip(struct ioh_gpio *chip,
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
385
386
387
388
389
390
391
392
393
394
395
396
397
  				unsigned int irq_start, unsigned int num)
  {
  	struct irq_chip_generic *gc;
  	struct irq_chip_type *ct;
  
  	gc = irq_alloc_generic_chip("ioh_gpio", 1, irq_start, chip->base,
  				    handle_simple_irq);
  	gc->private = chip;
  	ct = gc->chip_types;
  
  	ct->chip.irq_mask = ioh_irq_mask;
  	ct->chip.irq_unmask = ioh_irq_unmask;
  	ct->chip.irq_set_type = ioh_irq_type;
4d052213f   Feng Tang   gpio-ml-ioh: Add ...
398
399
  	ct->chip.irq_disable = ioh_irq_disable;
  	ct->chip.irq_enable = ioh_irq_enable;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
400
401
402
  
  	irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
  			       IRQ_NOREQUEST | IRQ_NOPROBE, 0);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
403
  }
3836309d9   Bill Pemberton   gpio: remove use ...
404
  static int ioh_gpio_probe(struct pci_dev *pdev,
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
405
406
407
  				    const struct pci_device_id *id)
  {
  	int ret;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
408
  	int i, j;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
409
410
  	struct ioh_gpio *chip;
  	void __iomem *base;
dd9328a6b   Márton Németh   gpio-ml-ioh: clea...
411
  	void *chip_save;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
412
  	int irq_base;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  
  	ret = pci_enable_device(pdev);
  	if (ret) {
  		dev_err(&pdev->dev, "%s : pci_enable_device failed", __func__);
  		goto err_pci_enable;
  	}
  
  	ret = pci_request_regions(pdev, KBUILD_MODNAME);
  	if (ret) {
  		dev_err(&pdev->dev, "pci_request_regions failed-%d", ret);
  		goto err_request_regions;
  	}
  
  	base = pci_iomap(pdev, 1, 0);
2bd1c85e8   Márton Németh   gpio-ml-ioh: clea...
427
  	if (!base) {
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  		dev_err(&pdev->dev, "%s : pci_iomap failed", __func__);
  		ret = -ENOMEM;
  		goto err_iomap;
  	}
  
  	chip_save = kzalloc(sizeof(*chip) * 8, GFP_KERNEL);
  	if (chip_save == NULL) {
  		dev_err(&pdev->dev, "%s : kzalloc failed", __func__);
  		ret = -ENOMEM;
  		goto err_kzalloc;
  	}
  
  	chip = chip_save;
  	for (i = 0; i < 8; i++, chip++) {
  		chip->dev = &pdev->dev;
  		chip->base = base;
  		chip->reg = chip->base;
  		chip->ch = i;
7e3a70fb7   Axel Lin   gpio: Add missing...
446
  		spin_lock_init(&chip->spinlock);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
447
  		ioh_gpio_setup(chip, num_ports[i]);
47315578b   Linus Walleij   gpio: ml-ioh: use...
448
  		ret = gpiochip_add_data(&chip->gpio, chip);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
449
450
451
452
453
454
455
456
  		if (ret) {
  			dev_err(&pdev->dev, "IOH gpio: Failed to register GPIO
  ");
  			goto err_gpiochip_add;
  		}
  	}
  
  	chip = chip_save;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
457
458
  	for (j = 0; j < 8; j++, chip++) {
  		irq_base = irq_alloc_descs(-1, IOH_IRQ_BASE, num_ports[j],
a7aaa4f88   Tomoya MORINAGA   gpio-ml-ioh: Use ...
459
  					   NUMA_NO_NODE);
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
460
461
462
463
464
  		if (irq_base < 0) {
  			dev_warn(&pdev->dev,
  				"ml_ioh_gpio: Failed to get IRQ base num
  ");
  			chip->irq_base = -1;
df46dce09   Wei Yongjun   gpio-ml-ioh: fix ...
465
  			ret = irq_base;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  			goto err_irq_alloc_descs;
  		}
  		chip->irq_base = irq_base;
  		ioh_gpio_alloc_generic_chip(chip, irq_base, num_ports[j]);
  	}
  
  	chip = chip_save;
  	ret = request_irq(pdev->irq, ioh_gpio_handler,
  			     IRQF_SHARED, KBUILD_MODNAME, chip);
  	if (ret != 0) {
  		dev_err(&pdev->dev,
  			"%s request_irq failed
  ", __func__);
  		goto err_request_irq;
  	}
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
481
482
483
  	pci_set_drvdata(pdev, chip);
  
  	return 0;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
484
485
486
487
488
489
490
491
492
  err_request_irq:
  	chip = chip_save;
  err_irq_alloc_descs:
  	while (--j >= 0) {
  		chip--;
  		irq_free_descs(chip->irq_base, num_ports[j]);
  	}
  
  	chip = chip_save;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
493
  err_gpiochip_add:
33300571b   Axel Lin   gpio/ml-ioh: fix ...
494
  	while (--i >= 0) {
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
495
  		chip--;
9f5132ae8   abdoulaye berthe   gpio: remove all ...
496
  		gpiochip_remove(&chip->gpio);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
  	}
  	kfree(chip_save);
  
  err_kzalloc:
  	pci_iounmap(pdev, base);
  
  err_iomap:
  	pci_release_regions(pdev);
  
  err_request_regions:
  	pci_disable_device(pdev);
  
  err_pci_enable:
  
  	dev_err(&pdev->dev, "%s Failed returns %d
  ", __func__, ret);
  	return ret;
  }
206210ce6   Bill Pemberton   gpio: remove use ...
515
  static void ioh_gpio_remove(struct pci_dev *pdev)
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
516
  {
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
517
518
  	int i;
  	struct ioh_gpio *chip = pci_get_drvdata(pdev);
dd9328a6b   Márton Németh   gpio-ml-ioh: clea...
519
  	void *chip_save;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
520
521
  
  	chip_save = chip;
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
522
523
  
  	free_irq(pdev->irq, chip);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
524
  	for (i = 0; i < 8; i++, chip++) {
54be56631   Tomoya MORINAGA   gpio-ml-ioh: Supp...
525
  		irq_free_descs(chip->irq_base, num_ports[i]);
9f5132ae8   abdoulaye berthe   gpio: remove all ...
526
  		gpiochip_remove(&chip->gpio);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  	}
  
  	chip = chip_save;
  	pci_iounmap(pdev, chip->base);
  	pci_release_regions(pdev);
  	pci_disable_device(pdev);
  	kfree(chip);
  }
  
  #ifdef CONFIG_PM
  static int ioh_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
  {
  	s32 ret;
  	struct ioh_gpio *chip = pci_get_drvdata(pdev);
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
541
  	unsigned long flags;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
542

b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
543
  	spin_lock_irqsave(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
544
  	ioh_gpio_save_reg_conf(chip);
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
545
  	spin_unlock_irqrestore(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
  
  	ret = pci_save_state(pdev);
  	if (ret) {
  		dev_err(&pdev->dev, "pci_save_state Failed-%d
  ", ret);
  		return ret;
  	}
  	pci_disable_device(pdev);
  	pci_set_power_state(pdev, PCI_D0);
  	ret = pci_enable_wake(pdev, PCI_D0, 1);
  	if (ret)
  		dev_err(&pdev->dev, "pci_enable_wake Failed -%d
  ", ret);
  
  	return 0;
  }
  
  static int ioh_gpio_resume(struct pci_dev *pdev)
  {
  	s32 ret;
  	struct ioh_gpio *chip = pci_get_drvdata(pdev);
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
567
  	unsigned long flags;
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
568
569
570
571
572
573
574
575
576
577
  
  	ret = pci_enable_wake(pdev, PCI_D0, 0);
  
  	pci_set_power_state(pdev, PCI_D0);
  	ret = pci_enable_device(pdev);
  	if (ret) {
  		dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
  		return ret;
  	}
  	pci_restore_state(pdev);
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
578
  	spin_lock_irqsave(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
579
580
581
  	iowrite32(0x01, &chip->reg->srst);
  	iowrite32(0x00, &chip->reg->srst);
  	ioh_gpio_restore_reg_conf(chip);
b490fa0bf   Tomoya MORINAGA   gpio-ml-ioh: Fix ...
582
  	spin_unlock_irqrestore(&chip->spinlock, flags);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
583
584
585
586
587
588
589
  
  	return 0;
  }
  #else
  #define ioh_gpio_suspend NULL
  #define ioh_gpio_resume NULL
  #endif
14f4a8838   Jingoo Han   gpio: remove DEFI...
590
  static const struct pci_device_id ioh_gpio_pcidev_id[] = {
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
591
592
593
  	{ PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x802E) },
  	{ 0, }
  };
19234cdda   Axel Lin   gpio: add MODULE_...
594
  MODULE_DEVICE_TABLE(pci, ioh_gpio_pcidev_id);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
595
596
597
598
599
  
  static struct pci_driver ioh_gpio_driver = {
  	.name = "ml_ioh_gpio",
  	.id_table = ioh_gpio_pcidev_id,
  	.probe = ioh_gpio_probe,
8283c4ff5   Bill Pemberton   gpio: remove use ...
600
  	.remove = ioh_gpio_remove,
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
601
602
603
  	.suspend = ioh_gpio_suspend,
  	.resume = ioh_gpio_resume
  };
93baa65fe   Axel Lin   gpio: Convert dri...
604
  module_pci_driver(ioh_gpio_driver);
49a367937   Tomoya MORINAGA   gpio/ml_ioh_gpio:...
605
606
607
  
  MODULE_DESCRIPTION("OKI SEMICONDUCTOR ML-IOH series GPIO Driver");
  MODULE_LICENSE("GPL");