xref: /openbmc/linux/drivers/gpio/gpio-ml-ioh.c (revision 54be566317b6aece2389a95bb19ea209af9359be)
1c103de24SGrant Likely /*
2c103de24SGrant Likely  * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
3c103de24SGrant Likely  *
4c103de24SGrant Likely  * This program is free software; you can redistribute it and/or modify
5c103de24SGrant Likely  * it under the terms of the GNU General Public License as published by
6c103de24SGrant Likely  * the Free Software Foundation; version 2 of the License.
7c103de24SGrant Likely  *
8c103de24SGrant Likely  * This program is distributed in the hope that it will be useful,
9c103de24SGrant Likely  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10c103de24SGrant Likely  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11c103de24SGrant Likely  * GNU General Public License for more details.
12c103de24SGrant Likely  *
13c103de24SGrant Likely  * You should have received a copy of the GNU General Public License
14c103de24SGrant Likely  * along with this program; if not, write to the Free Software
15c103de24SGrant Likely  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16c103de24SGrant Likely  */
17c103de24SGrant Likely #include <linux/kernel.h>
18c103de24SGrant Likely #include <linux/slab.h>
19c103de24SGrant Likely #include <linux/pci.h>
20c103de24SGrant Likely #include <linux/gpio.h>
21*54be5663STomoya MORINAGA #include <linux/interrupt.h>
22*54be5663STomoya MORINAGA #include <linux/irq.h>
23*54be5663STomoya MORINAGA 
24*54be5663STomoya MORINAGA #define IOH_EDGE_FALLING	0
25*54be5663STomoya MORINAGA #define IOH_EDGE_RISING		BIT(0)
26*54be5663STomoya MORINAGA #define IOH_LEVEL_L		BIT(1)
27*54be5663STomoya MORINAGA #define IOH_LEVEL_H		(BIT(0) | BIT(1))
28*54be5663STomoya MORINAGA #define IOH_EDGE_BOTH		BIT(2)
29*54be5663STomoya MORINAGA #define IOH_IM_MASK		(BIT(0) | BIT(1) | BIT(2))
30*54be5663STomoya MORINAGA 
31*54be5663STomoya MORINAGA #define IOH_IRQ_BASE		0
32c103de24SGrant Likely 
33c103de24SGrant Likely #define PCI_VENDOR_ID_ROHM             0x10DB
34c103de24SGrant Likely 
35c103de24SGrant Likely struct ioh_reg_comn {
36c103de24SGrant Likely 	u32	ien;
37c103de24SGrant Likely 	u32	istatus;
38c103de24SGrant Likely 	u32	idisp;
39c103de24SGrant Likely 	u32	iclr;
40c103de24SGrant Likely 	u32	imask;
41c103de24SGrant Likely 	u32	imaskclr;
42c103de24SGrant Likely 	u32	po;
43c103de24SGrant Likely 	u32	pi;
44c103de24SGrant Likely 	u32	pm;
45c103de24SGrant Likely 	u32	im_0;
46c103de24SGrant Likely 	u32	im_1;
47c103de24SGrant Likely 	u32	reserved;
48c103de24SGrant Likely };
49c103de24SGrant Likely 
50c103de24SGrant Likely struct ioh_regs {
51c103de24SGrant Likely 	struct ioh_reg_comn regs[8];
52c103de24SGrant Likely 	u32 reserve1[16];
53c103de24SGrant Likely 	u32 ioh_sel_reg[4];
54c103de24SGrant Likely 	u32 reserve2[11];
55c103de24SGrant Likely 	u32 srst;
56c103de24SGrant Likely };
57c103de24SGrant Likely 
58c103de24SGrant Likely /**
59c103de24SGrant Likely  * struct ioh_gpio_reg_data - The register store data.
60*54be5663STomoya MORINAGA  * @ien_reg	To store contents of interrupt enable register.
61*54be5663STomoya MORINAGA  * @imask_reg:	To store contents of interrupt mask regist
62c103de24SGrant Likely  * @po_reg:	To store contents of PO register.
63c103de24SGrant Likely  * @pm_reg:	To store contents of PM register.
64*54be5663STomoya MORINAGA  * @im0_reg:	To store contents of interrupt mode regist0
65*54be5663STomoya MORINAGA  * @im1_reg:	To store contents of interrupt mode regist1
66c103de24SGrant Likely  */
67c103de24SGrant Likely struct ioh_gpio_reg_data {
68*54be5663STomoya MORINAGA 	u32 ien_reg;
69*54be5663STomoya MORINAGA 	u32 imask_reg;
70c103de24SGrant Likely 	u32 po_reg;
71c103de24SGrant Likely 	u32 pm_reg;
72*54be5663STomoya MORINAGA 	u32 im0_reg;
73*54be5663STomoya MORINAGA 	u32 im1_reg;
74c103de24SGrant Likely };
75c103de24SGrant Likely 
76c103de24SGrant Likely /**
77c103de24SGrant Likely  * struct ioh_gpio - GPIO private data structure.
78c103de24SGrant Likely  * @base:			PCI base address of Memory mapped I/O register.
79c103de24SGrant Likely  * @reg:			Memory mapped IOH GPIO register list.
80c103de24SGrant Likely  * @dev:			Pointer to device structure.
81c103de24SGrant Likely  * @gpio:			Data for GPIO infrastructure.
82c103de24SGrant Likely  * @ioh_gpio_reg:		Memory mapped Register data is saved here
83c103de24SGrant Likely  *				when suspend.
84c103de24SGrant Likely  * @ch:				Indicate GPIO channel
85*54be5663STomoya MORINAGA  * @irq_base:		Save base of IRQ number for interrupt
86*54be5663STomoya MORINAGA  * @spinlock:		Used for register access protection in
87*54be5663STomoya MORINAGA  *				interrupt context ioh_irq_type and PM;
88c103de24SGrant Likely  */
89c103de24SGrant Likely struct ioh_gpio {
90c103de24SGrant Likely 	void __iomem *base;
91c103de24SGrant Likely 	struct ioh_regs __iomem *reg;
92c103de24SGrant Likely 	struct device *dev;
93c103de24SGrant Likely 	struct gpio_chip gpio;
94c103de24SGrant Likely 	struct ioh_gpio_reg_data ioh_gpio_reg;
95c103de24SGrant Likely 	struct mutex lock;
96c103de24SGrant Likely 	int ch;
97*54be5663STomoya MORINAGA 	int irq_base;
98*54be5663STomoya MORINAGA 	spinlock_t spinlock;
99c103de24SGrant Likely };
100c103de24SGrant Likely 
101c103de24SGrant Likely static const int num_ports[] = {6, 12, 16, 16, 15, 16, 16, 12};
102c103de24SGrant Likely 
103c103de24SGrant Likely static void ioh_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
104c103de24SGrant Likely {
105c103de24SGrant Likely 	u32 reg_val;
106c103de24SGrant Likely 	struct ioh_gpio *chip =	container_of(gpio, struct ioh_gpio, gpio);
107c103de24SGrant Likely 
108c103de24SGrant Likely 	mutex_lock(&chip->lock);
109c103de24SGrant Likely 	reg_val = ioread32(&chip->reg->regs[chip->ch].po);
110c103de24SGrant Likely 	if (val)
111c103de24SGrant Likely 		reg_val |= (1 << nr);
112c103de24SGrant Likely 	else
113c103de24SGrant Likely 		reg_val &= ~(1 << nr);
114c103de24SGrant Likely 
115c103de24SGrant Likely 	iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
116c103de24SGrant Likely 	mutex_unlock(&chip->lock);
117c103de24SGrant Likely }
118c103de24SGrant Likely 
119c103de24SGrant Likely static int ioh_gpio_get(struct gpio_chip *gpio, unsigned nr)
120c103de24SGrant Likely {
121c103de24SGrant Likely 	struct ioh_gpio *chip =	container_of(gpio, struct ioh_gpio, gpio);
122c103de24SGrant Likely 
123c103de24SGrant Likely 	return ioread32(&chip->reg->regs[chip->ch].pi) & (1 << nr);
124c103de24SGrant Likely }
125c103de24SGrant Likely 
126c103de24SGrant Likely static int ioh_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
127c103de24SGrant Likely 				     int val)
128c103de24SGrant Likely {
129c103de24SGrant Likely 	struct ioh_gpio *chip =	container_of(gpio, struct ioh_gpio, gpio);
130c103de24SGrant Likely 	u32 pm;
131c103de24SGrant Likely 	u32 reg_val;
132c103de24SGrant Likely 
133c103de24SGrant Likely 	mutex_lock(&chip->lock);
134c103de24SGrant Likely 	pm = ioread32(&chip->reg->regs[chip->ch].pm) &
135c103de24SGrant Likely 					((1 << num_ports[chip->ch]) - 1);
136c103de24SGrant Likely 	pm |= (1 << nr);
137c103de24SGrant Likely 	iowrite32(pm, &chip->reg->regs[chip->ch].pm);
138c103de24SGrant Likely 
139c103de24SGrant Likely 	reg_val = ioread32(&chip->reg->regs[chip->ch].po);
140c103de24SGrant Likely 	if (val)
141c103de24SGrant Likely 		reg_val |= (1 << nr);
142c103de24SGrant Likely 	else
143c103de24SGrant Likely 		reg_val &= ~(1 << nr);
144c103de24SGrant Likely 	iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
145c103de24SGrant Likely 
146c103de24SGrant Likely 	mutex_unlock(&chip->lock);
147c103de24SGrant Likely 
148c103de24SGrant Likely 	return 0;
149c103de24SGrant Likely }
150c103de24SGrant Likely 
151c103de24SGrant Likely static int ioh_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
152c103de24SGrant Likely {
153c103de24SGrant Likely 	struct ioh_gpio *chip =	container_of(gpio, struct ioh_gpio, gpio);
154c103de24SGrant Likely 	u32 pm;
155c103de24SGrant Likely 
156c103de24SGrant Likely 	mutex_lock(&chip->lock);
157c103de24SGrant Likely 	pm = ioread32(&chip->reg->regs[chip->ch].pm) &
158c103de24SGrant Likely 				((1 << num_ports[chip->ch]) - 1);
159c103de24SGrant Likely 	pm &= ~(1 << nr);
160c103de24SGrant Likely 	iowrite32(pm, &chip->reg->regs[chip->ch].pm);
161c103de24SGrant Likely 	mutex_unlock(&chip->lock);
162c103de24SGrant Likely 
163c103de24SGrant Likely 	return 0;
164c103de24SGrant Likely }
165c103de24SGrant Likely 
166c103de24SGrant Likely #ifdef CONFIG_PM
167c103de24SGrant Likely /*
168c103de24SGrant Likely  * Save register configuration and disable interrupts.
169c103de24SGrant Likely  */
170c103de24SGrant Likely static void ioh_gpio_save_reg_conf(struct ioh_gpio *chip)
171c103de24SGrant Likely {
172c103de24SGrant Likely 	chip->ioh_gpio_reg.po_reg = ioread32(&chip->reg->regs[chip->ch].po);
173c103de24SGrant Likely 	chip->ioh_gpio_reg.pm_reg = ioread32(&chip->reg->regs[chip->ch].pm);
174*54be5663STomoya MORINAGA 	chip->ioh_gpio_reg.ien_reg = ioread32(&chip->reg->regs[chip->ch].ien);
175*54be5663STomoya MORINAGA 	chip->ioh_gpio_reg.imask_reg = ioread32(&chip->reg->regs[chip->ch].imask);
176*54be5663STomoya MORINAGA 	chip->ioh_gpio_reg.im0_reg = ioread32(&chip->reg->regs[chip->ch].im_0);
177*54be5663STomoya MORINAGA 	chip->ioh_gpio_reg.im1_reg = ioread32(&chip->reg->regs[chip->ch].im_1);
178c103de24SGrant Likely }
179c103de24SGrant Likely 
180c103de24SGrant Likely /*
181c103de24SGrant Likely  * This function restores the register configuration of the GPIO device.
182c103de24SGrant Likely  */
183c103de24SGrant Likely static void ioh_gpio_restore_reg_conf(struct ioh_gpio *chip)
184c103de24SGrant Likely {
185c103de24SGrant Likely 	iowrite32(chip->ioh_gpio_reg.po_reg, &chip->reg->regs[chip->ch].po);
186c103de24SGrant Likely 	iowrite32(chip->ioh_gpio_reg.pm_reg, &chip->reg->regs[chip->ch].pm);
187*54be5663STomoya MORINAGA 	iowrite32(chip->ioh_gpio_reg.ien_reg, &chip->reg->regs[chip->ch].ien);
188*54be5663STomoya MORINAGA 	iowrite32(chip->ioh_gpio_reg.imask_reg, &chip->reg->regs[chip->ch].imask);
189*54be5663STomoya MORINAGA 	iowrite32(chip->ioh_gpio_reg.im0_reg, &chip->reg->regs[chip->ch].im_0);
190*54be5663STomoya MORINAGA 	iowrite32(chip->ioh_gpio_reg.im1_reg, &chip->reg->regs[chip->ch].im_1);
191c103de24SGrant Likely }
192c103de24SGrant Likely #endif
193c103de24SGrant Likely 
194*54be5663STomoya MORINAGA static int ioh_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
195*54be5663STomoya MORINAGA {
196*54be5663STomoya MORINAGA 	struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
197*54be5663STomoya MORINAGA 	return chip->irq_base + offset;
198*54be5663STomoya MORINAGA }
199*54be5663STomoya MORINAGA 
200c103de24SGrant Likely static void ioh_gpio_setup(struct ioh_gpio *chip, int num_port)
201c103de24SGrant Likely {
202c103de24SGrant Likely 	struct gpio_chip *gpio = &chip->gpio;
203c103de24SGrant Likely 
204c103de24SGrant Likely 	gpio->label = dev_name(chip->dev);
205c103de24SGrant Likely 	gpio->owner = THIS_MODULE;
206c103de24SGrant Likely 	gpio->direction_input = ioh_gpio_direction_input;
207c103de24SGrant Likely 	gpio->get = ioh_gpio_get;
208c103de24SGrant Likely 	gpio->direction_output = ioh_gpio_direction_output;
209c103de24SGrant Likely 	gpio->set = ioh_gpio_set;
210c103de24SGrant Likely 	gpio->dbg_show = NULL;
211c103de24SGrant Likely 	gpio->base = -1;
212c103de24SGrant Likely 	gpio->ngpio = num_port;
213c103de24SGrant Likely 	gpio->can_sleep = 0;
214*54be5663STomoya MORINAGA 	gpio->to_irq = ioh_gpio_to_irq;
215*54be5663STomoya MORINAGA }
216*54be5663STomoya MORINAGA 
217*54be5663STomoya MORINAGA static int ioh_irq_type(struct irq_data *d, unsigned int type)
218*54be5663STomoya MORINAGA {
219*54be5663STomoya MORINAGA 	u32 im;
220*54be5663STomoya MORINAGA 	u32 *im_reg;
221*54be5663STomoya MORINAGA 	u32 ien;
222*54be5663STomoya MORINAGA 	u32 im_pos;
223*54be5663STomoya MORINAGA 	int ch;
224*54be5663STomoya MORINAGA 	unsigned long flags;
225*54be5663STomoya MORINAGA 	u32 val;
226*54be5663STomoya MORINAGA 	int irq = d->irq;
227*54be5663STomoya MORINAGA 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
228*54be5663STomoya MORINAGA 	struct ioh_gpio *chip = gc->private;
229*54be5663STomoya MORINAGA 
230*54be5663STomoya MORINAGA 	ch = irq - chip->irq_base;
231*54be5663STomoya MORINAGA 	if (irq <= chip->irq_base + 7) {
232*54be5663STomoya MORINAGA 		im_reg = &chip->reg->regs[chip->ch].im_0;
233*54be5663STomoya MORINAGA 		im_pos = ch;
234*54be5663STomoya MORINAGA 	} else {
235*54be5663STomoya MORINAGA 		im_reg = &chip->reg->regs[chip->ch].im_1;
236*54be5663STomoya MORINAGA 		im_pos = ch - 8;
237*54be5663STomoya MORINAGA 	}
238*54be5663STomoya MORINAGA 	dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d type=%d\n",
239*54be5663STomoya MORINAGA 		__func__, irq, type, ch, im_pos, type);
240*54be5663STomoya MORINAGA 
241*54be5663STomoya MORINAGA 	spin_lock_irqsave(&chip->spinlock, flags);
242*54be5663STomoya MORINAGA 
243*54be5663STomoya MORINAGA 	switch (type) {
244*54be5663STomoya MORINAGA 	case IRQ_TYPE_EDGE_RISING:
245*54be5663STomoya MORINAGA 		val = IOH_EDGE_RISING;
246*54be5663STomoya MORINAGA 		break;
247*54be5663STomoya MORINAGA 	case IRQ_TYPE_EDGE_FALLING:
248*54be5663STomoya MORINAGA 		val = IOH_EDGE_FALLING;
249*54be5663STomoya MORINAGA 		break;
250*54be5663STomoya MORINAGA 	case IRQ_TYPE_EDGE_BOTH:
251*54be5663STomoya MORINAGA 		val = IOH_EDGE_BOTH;
252*54be5663STomoya MORINAGA 		break;
253*54be5663STomoya MORINAGA 	case IRQ_TYPE_LEVEL_HIGH:
254*54be5663STomoya MORINAGA 		val = IOH_LEVEL_H;
255*54be5663STomoya MORINAGA 		break;
256*54be5663STomoya MORINAGA 	case IRQ_TYPE_LEVEL_LOW:
257*54be5663STomoya MORINAGA 		val = IOH_LEVEL_L;
258*54be5663STomoya MORINAGA 		break;
259*54be5663STomoya MORINAGA 	case IRQ_TYPE_PROBE:
260*54be5663STomoya MORINAGA 		goto end;
261*54be5663STomoya MORINAGA 	default:
262*54be5663STomoya MORINAGA 		dev_warn(chip->dev, "%s: unknown type(%dd)",
263*54be5663STomoya MORINAGA 			__func__, type);
264*54be5663STomoya MORINAGA 		goto end;
265*54be5663STomoya MORINAGA 	}
266*54be5663STomoya MORINAGA 
267*54be5663STomoya MORINAGA 	/* Set interrupt mode */
268*54be5663STomoya MORINAGA 	im = ioread32(im_reg) & ~(IOH_IM_MASK << (im_pos * 4));
269*54be5663STomoya MORINAGA 	iowrite32(im | (val << (im_pos * 4)), im_reg);
270*54be5663STomoya MORINAGA 
271*54be5663STomoya MORINAGA 	/* iclr */
272*54be5663STomoya MORINAGA 	iowrite32(BIT(ch), &chip->reg->regs[chip->ch].iclr);
273*54be5663STomoya MORINAGA 
274*54be5663STomoya MORINAGA 	/* IMASKCLR */
275*54be5663STomoya MORINAGA 	iowrite32(BIT(ch), &chip->reg->regs[chip->ch].imaskclr);
276*54be5663STomoya MORINAGA 
277*54be5663STomoya MORINAGA 	/* Enable interrupt */
278*54be5663STomoya MORINAGA 	ien = ioread32(&chip->reg->regs[chip->ch].ien);
279*54be5663STomoya MORINAGA 	iowrite32(ien | BIT(ch), &chip->reg->regs[chip->ch].ien);
280*54be5663STomoya MORINAGA end:
281*54be5663STomoya MORINAGA 	spin_unlock_irqrestore(&chip->spinlock, flags);
282*54be5663STomoya MORINAGA 
283*54be5663STomoya MORINAGA 	return 0;
284*54be5663STomoya MORINAGA }
285*54be5663STomoya MORINAGA 
286*54be5663STomoya MORINAGA static void ioh_irq_unmask(struct irq_data *d)
287*54be5663STomoya MORINAGA {
288*54be5663STomoya MORINAGA 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
289*54be5663STomoya MORINAGA 	struct ioh_gpio *chip = gc->private;
290*54be5663STomoya MORINAGA 
291*54be5663STomoya MORINAGA 	iowrite32(1 << (d->irq - chip->irq_base),
292*54be5663STomoya MORINAGA 		  &chip->reg->regs[chip->ch].imaskclr);
293*54be5663STomoya MORINAGA }
294*54be5663STomoya MORINAGA 
295*54be5663STomoya MORINAGA static void ioh_irq_mask(struct irq_data *d)
296*54be5663STomoya MORINAGA {
297*54be5663STomoya MORINAGA 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
298*54be5663STomoya MORINAGA 	struct ioh_gpio *chip = gc->private;
299*54be5663STomoya MORINAGA 
300*54be5663STomoya MORINAGA 	iowrite32(1 << (d->irq - chip->irq_base),
301*54be5663STomoya MORINAGA 		  &chip->reg->regs[chip->ch].imask);
302*54be5663STomoya MORINAGA }
303*54be5663STomoya MORINAGA 
304*54be5663STomoya MORINAGA static irqreturn_t ioh_gpio_handler(int irq, void *dev_id)
305*54be5663STomoya MORINAGA {
306*54be5663STomoya MORINAGA 	struct ioh_gpio *chip = dev_id;
307*54be5663STomoya MORINAGA 	u32 reg_val;
308*54be5663STomoya MORINAGA 	int i, j;
309*54be5663STomoya MORINAGA 	int ret = IRQ_NONE;
310*54be5663STomoya MORINAGA 
311*54be5663STomoya MORINAGA 	for (i = 0; i < 8; i++) {
312*54be5663STomoya MORINAGA 		reg_val = ioread32(&chip->reg->regs[i].istatus);
313*54be5663STomoya MORINAGA 		for (j = 0; j < num_ports[i]; j++) {
314*54be5663STomoya MORINAGA 			if (reg_val & BIT(j)) {
315*54be5663STomoya MORINAGA 				dev_dbg(chip->dev,
316*54be5663STomoya MORINAGA 					"%s:[%d]:irq=%d status=0x%x\n",
317*54be5663STomoya MORINAGA 					__func__, j, irq, reg_val);
318*54be5663STomoya MORINAGA 				iowrite32(BIT(j),
319*54be5663STomoya MORINAGA 					  &chip->reg->regs[chip->ch].iclr);
320*54be5663STomoya MORINAGA 				generic_handle_irq(chip->irq_base + j);
321*54be5663STomoya MORINAGA 				ret = IRQ_HANDLED;
322*54be5663STomoya MORINAGA 			}
323*54be5663STomoya MORINAGA 		}
324*54be5663STomoya MORINAGA 	}
325*54be5663STomoya MORINAGA 	return ret;
326*54be5663STomoya MORINAGA }
327*54be5663STomoya MORINAGA 
328*54be5663STomoya MORINAGA static __devinit void ioh_gpio_alloc_generic_chip(struct ioh_gpio *chip,
329*54be5663STomoya MORINAGA 				unsigned int irq_start, unsigned int num)
330*54be5663STomoya MORINAGA {
331*54be5663STomoya MORINAGA 	struct irq_chip_generic *gc;
332*54be5663STomoya MORINAGA 	struct irq_chip_type *ct;
333*54be5663STomoya MORINAGA 
334*54be5663STomoya MORINAGA 	gc = irq_alloc_generic_chip("ioh_gpio", 1, irq_start, chip->base,
335*54be5663STomoya MORINAGA 				    handle_simple_irq);
336*54be5663STomoya MORINAGA 	gc->private = chip;
337*54be5663STomoya MORINAGA 	ct = gc->chip_types;
338*54be5663STomoya MORINAGA 
339*54be5663STomoya MORINAGA 	ct->chip.irq_mask = ioh_irq_mask;
340*54be5663STomoya MORINAGA 	ct->chip.irq_unmask = ioh_irq_unmask;
341*54be5663STomoya MORINAGA 	ct->chip.irq_set_type = ioh_irq_type;
342*54be5663STomoya MORINAGA 
343*54be5663STomoya MORINAGA 	irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
344*54be5663STomoya MORINAGA 			       IRQ_NOREQUEST | IRQ_NOPROBE, 0);
345c103de24SGrant Likely }
346c103de24SGrant Likely 
347c103de24SGrant Likely static int __devinit ioh_gpio_probe(struct pci_dev *pdev,
348c103de24SGrant Likely 				    const struct pci_device_id *id)
349c103de24SGrant Likely {
350c103de24SGrant Likely 	int ret;
351*54be5663STomoya MORINAGA 	int i, j;
352c103de24SGrant Likely 	struct ioh_gpio *chip;
353c103de24SGrant Likely 	void __iomem *base;
354c103de24SGrant Likely 	void __iomem *chip_save;
355*54be5663STomoya MORINAGA 	int irq_base;
356c103de24SGrant Likely 
357c103de24SGrant Likely 	ret = pci_enable_device(pdev);
358c103de24SGrant Likely 	if (ret) {
359c103de24SGrant Likely 		dev_err(&pdev->dev, "%s : pci_enable_device failed", __func__);
360c103de24SGrant Likely 		goto err_pci_enable;
361c103de24SGrant Likely 	}
362c103de24SGrant Likely 
363c103de24SGrant Likely 	ret = pci_request_regions(pdev, KBUILD_MODNAME);
364c103de24SGrant Likely 	if (ret) {
365c103de24SGrant Likely 		dev_err(&pdev->dev, "pci_request_regions failed-%d", ret);
366c103de24SGrant Likely 		goto err_request_regions;
367c103de24SGrant Likely 	}
368c103de24SGrant Likely 
369c103de24SGrant Likely 	base = pci_iomap(pdev, 1, 0);
370c103de24SGrant Likely 	if (base == 0) {
371c103de24SGrant Likely 		dev_err(&pdev->dev, "%s : pci_iomap failed", __func__);
372c103de24SGrant Likely 		ret = -ENOMEM;
373c103de24SGrant Likely 		goto err_iomap;
374c103de24SGrant Likely 	}
375c103de24SGrant Likely 
376c103de24SGrant Likely 	chip_save = kzalloc(sizeof(*chip) * 8, GFP_KERNEL);
377c103de24SGrant Likely 	if (chip_save == NULL) {
378c103de24SGrant Likely 		dev_err(&pdev->dev, "%s : kzalloc failed", __func__);
379c103de24SGrant Likely 		ret = -ENOMEM;
380c103de24SGrant Likely 		goto err_kzalloc;
381c103de24SGrant Likely 	}
382c103de24SGrant Likely 
383c103de24SGrant Likely 	chip = chip_save;
384c103de24SGrant Likely 	for (i = 0; i < 8; i++, chip++) {
385c103de24SGrant Likely 		chip->dev = &pdev->dev;
386c103de24SGrant Likely 		chip->base = base;
387c103de24SGrant Likely 		chip->reg = chip->base;
388c103de24SGrant Likely 		chip->ch = i;
389c103de24SGrant Likely 		mutex_init(&chip->lock);
390c103de24SGrant Likely 		ioh_gpio_setup(chip, num_ports[i]);
391c103de24SGrant Likely 		ret = gpiochip_add(&chip->gpio);
392c103de24SGrant Likely 		if (ret) {
393c103de24SGrant Likely 			dev_err(&pdev->dev, "IOH gpio: Failed to register GPIO\n");
394c103de24SGrant Likely 			goto err_gpiochip_add;
395c103de24SGrant Likely 		}
396c103de24SGrant Likely 	}
397c103de24SGrant Likely 
398c103de24SGrant Likely 	chip = chip_save;
399*54be5663STomoya MORINAGA 	for (j = 0; j < 8; j++, chip++) {
400*54be5663STomoya MORINAGA 		irq_base = irq_alloc_descs(-1, IOH_IRQ_BASE, num_ports[j],
401*54be5663STomoya MORINAGA 					   GFP_KERNEL);
402*54be5663STomoya MORINAGA 		if (irq_base < 0) {
403*54be5663STomoya MORINAGA 			dev_warn(&pdev->dev,
404*54be5663STomoya MORINAGA 				"ml_ioh_gpio: Failed to get IRQ base num\n");
405*54be5663STomoya MORINAGA 			chip->irq_base = -1;
406*54be5663STomoya MORINAGA 			goto err_irq_alloc_descs;
407*54be5663STomoya MORINAGA 		}
408*54be5663STomoya MORINAGA 		chip->irq_base = irq_base;
409*54be5663STomoya MORINAGA 		ioh_gpio_alloc_generic_chip(chip, irq_base, num_ports[j]);
410*54be5663STomoya MORINAGA 	}
411*54be5663STomoya MORINAGA 
412*54be5663STomoya MORINAGA 	chip = chip_save;
413*54be5663STomoya MORINAGA 	ret = request_irq(pdev->irq, ioh_gpio_handler,
414*54be5663STomoya MORINAGA 			     IRQF_SHARED, KBUILD_MODNAME, chip);
415*54be5663STomoya MORINAGA 	if (ret != 0) {
416*54be5663STomoya MORINAGA 		dev_err(&pdev->dev,
417*54be5663STomoya MORINAGA 			"%s request_irq failed\n", __func__);
418*54be5663STomoya MORINAGA 		goto err_request_irq;
419*54be5663STomoya MORINAGA 	}
420*54be5663STomoya MORINAGA 
421c103de24SGrant Likely 	pci_set_drvdata(pdev, chip);
422c103de24SGrant Likely 
423c103de24SGrant Likely 	return 0;
424c103de24SGrant Likely 
425*54be5663STomoya MORINAGA err_request_irq:
426*54be5663STomoya MORINAGA 	chip = chip_save;
427*54be5663STomoya MORINAGA err_irq_alloc_descs:
428*54be5663STomoya MORINAGA 	while (--j >= 0) {
429*54be5663STomoya MORINAGA 		chip--;
430*54be5663STomoya MORINAGA 		irq_free_descs(chip->irq_base, num_ports[j]);
431*54be5663STomoya MORINAGA 	}
432*54be5663STomoya MORINAGA 
433*54be5663STomoya MORINAGA 	chip = chip_save;
434c103de24SGrant Likely err_gpiochip_add:
43533300571SAxel Lin 	while (--i >= 0) {
436c103de24SGrant Likely 		chip--;
437c103de24SGrant Likely 		ret = gpiochip_remove(&chip->gpio);
438c103de24SGrant Likely 		if (ret)
439c103de24SGrant Likely 			dev_err(&pdev->dev, "Failed gpiochip_remove(%d)\n", i);
440c103de24SGrant Likely 	}
441c103de24SGrant Likely 	kfree(chip_save);
442c103de24SGrant Likely 
443c103de24SGrant Likely err_kzalloc:
444c103de24SGrant Likely 	pci_iounmap(pdev, base);
445c103de24SGrant Likely 
446c103de24SGrant Likely err_iomap:
447c103de24SGrant Likely 	pci_release_regions(pdev);
448c103de24SGrant Likely 
449c103de24SGrant Likely err_request_regions:
450c103de24SGrant Likely 	pci_disable_device(pdev);
451c103de24SGrant Likely 
452c103de24SGrant Likely err_pci_enable:
453c103de24SGrant Likely 
454c103de24SGrant Likely 	dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
455c103de24SGrant Likely 	return ret;
456c103de24SGrant Likely }
457c103de24SGrant Likely 
458c103de24SGrant Likely static void __devexit ioh_gpio_remove(struct pci_dev *pdev)
459c103de24SGrant Likely {
460c103de24SGrant Likely 	int err;
461c103de24SGrant Likely 	int i;
462c103de24SGrant Likely 	struct ioh_gpio *chip = pci_get_drvdata(pdev);
463c103de24SGrant Likely 	void __iomem *chip_save;
464c103de24SGrant Likely 
465c103de24SGrant Likely 	chip_save = chip;
466*54be5663STomoya MORINAGA 
467*54be5663STomoya MORINAGA 	free_irq(pdev->irq, chip);
468*54be5663STomoya MORINAGA 
469c103de24SGrant Likely 	for (i = 0; i < 8; i++, chip++) {
470*54be5663STomoya MORINAGA 		irq_free_descs(chip->irq_base, num_ports[i]);
471c103de24SGrant Likely 		err = gpiochip_remove(&chip->gpio);
472c103de24SGrant Likely 		if (err)
473c103de24SGrant Likely 			dev_err(&pdev->dev, "Failed gpiochip_remove\n");
474c103de24SGrant Likely 	}
475c103de24SGrant Likely 
476c103de24SGrant Likely 	chip = chip_save;
477c103de24SGrant Likely 	pci_iounmap(pdev, chip->base);
478c103de24SGrant Likely 	pci_release_regions(pdev);
479c103de24SGrant Likely 	pci_disable_device(pdev);
480c103de24SGrant Likely 	kfree(chip);
481c103de24SGrant Likely }
482c103de24SGrant Likely 
483c103de24SGrant Likely #ifdef CONFIG_PM
484c103de24SGrant Likely static int ioh_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
485c103de24SGrant Likely {
486c103de24SGrant Likely 	s32 ret;
487c103de24SGrant Likely 	struct ioh_gpio *chip = pci_get_drvdata(pdev);
488c103de24SGrant Likely 
489c103de24SGrant Likely 	ioh_gpio_save_reg_conf(chip);
490c103de24SGrant Likely 
491c103de24SGrant Likely 	ret = pci_save_state(pdev);
492c103de24SGrant Likely 	if (ret) {
493c103de24SGrant Likely 		dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
494c103de24SGrant Likely 		return ret;
495c103de24SGrant Likely 	}
496c103de24SGrant Likely 	pci_disable_device(pdev);
497c103de24SGrant Likely 	pci_set_power_state(pdev, PCI_D0);
498c103de24SGrant Likely 	ret = pci_enable_wake(pdev, PCI_D0, 1);
499c103de24SGrant Likely 	if (ret)
500c103de24SGrant Likely 		dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
501c103de24SGrant Likely 
502c103de24SGrant Likely 	return 0;
503c103de24SGrant Likely }
504c103de24SGrant Likely 
505c103de24SGrant Likely static int ioh_gpio_resume(struct pci_dev *pdev)
506c103de24SGrant Likely {
507c103de24SGrant Likely 	s32 ret;
508c103de24SGrant Likely 	struct ioh_gpio *chip = pci_get_drvdata(pdev);
509c103de24SGrant Likely 
510c103de24SGrant Likely 	ret = pci_enable_wake(pdev, PCI_D0, 0);
511c103de24SGrant Likely 
512c103de24SGrant Likely 	pci_set_power_state(pdev, PCI_D0);
513c103de24SGrant Likely 	ret = pci_enable_device(pdev);
514c103de24SGrant Likely 	if (ret) {
515c103de24SGrant Likely 		dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
516c103de24SGrant Likely 		return ret;
517c103de24SGrant Likely 	}
518c103de24SGrant Likely 	pci_restore_state(pdev);
519c103de24SGrant Likely 
520c103de24SGrant Likely 	iowrite32(0x01, &chip->reg->srst);
521c103de24SGrant Likely 	iowrite32(0x00, &chip->reg->srst);
522c103de24SGrant Likely 	ioh_gpio_restore_reg_conf(chip);
523c103de24SGrant Likely 
524c103de24SGrant Likely 	return 0;
525c103de24SGrant Likely }
526c103de24SGrant Likely #else
527c103de24SGrant Likely #define ioh_gpio_suspend NULL
528c103de24SGrant Likely #define ioh_gpio_resume NULL
529c103de24SGrant Likely #endif
530c103de24SGrant Likely 
531c103de24SGrant Likely static DEFINE_PCI_DEVICE_TABLE(ioh_gpio_pcidev_id) = {
532c103de24SGrant Likely 	{ PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x802E) },
533c103de24SGrant Likely 	{ 0, }
534c103de24SGrant Likely };
535c103de24SGrant Likely MODULE_DEVICE_TABLE(pci, ioh_gpio_pcidev_id);
536c103de24SGrant Likely 
537c103de24SGrant Likely static struct pci_driver ioh_gpio_driver = {
538c103de24SGrant Likely 	.name = "ml_ioh_gpio",
539c103de24SGrant Likely 	.id_table = ioh_gpio_pcidev_id,
540c103de24SGrant Likely 	.probe = ioh_gpio_probe,
541c103de24SGrant Likely 	.remove = __devexit_p(ioh_gpio_remove),
542c103de24SGrant Likely 	.suspend = ioh_gpio_suspend,
543c103de24SGrant Likely 	.resume = ioh_gpio_resume
544c103de24SGrant Likely };
545c103de24SGrant Likely 
546c103de24SGrant Likely static int __init ioh_gpio_pci_init(void)
547c103de24SGrant Likely {
548c103de24SGrant Likely 	return pci_register_driver(&ioh_gpio_driver);
549c103de24SGrant Likely }
550c103de24SGrant Likely module_init(ioh_gpio_pci_init);
551c103de24SGrant Likely 
552c103de24SGrant Likely static void __exit ioh_gpio_pci_exit(void)
553c103de24SGrant Likely {
554c103de24SGrant Likely 	pci_unregister_driver(&ioh_gpio_driver);
555c103de24SGrant Likely }
556c103de24SGrant Likely module_exit(ioh_gpio_pci_exit);
557c103de24SGrant Likely 
558c103de24SGrant Likely MODULE_DESCRIPTION("OKI SEMICONDUCTOR ML-IOH series GPIO Driver");
559c103de24SGrant Likely MODULE_LICENSE("GPL");
560