2 * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/pci.h>
20 #include <linux/gpio.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
24 #define IOH_EDGE_FALLING 0
25 #define IOH_EDGE_RISING BIT(0)
26 #define IOH_LEVEL_L BIT(1)
27 #define IOH_LEVEL_H (BIT(0) | BIT(1))
28 #define IOH_EDGE_BOTH BIT(2)
29 #define IOH_IM_MASK (BIT(0) | BIT(1) | BIT(2))
31 #define IOH_IRQ_BASE 0
33 #define PCI_VENDOR_ID_ROHM 0x10DB
51 struct ioh_reg_comn regs
[8];
59 * struct ioh_gpio_reg_data - The register store data.
60 * @ien_reg To store contents of interrupt enable register.
61 * @imask_reg: To store contents of interrupt mask regist
62 * @po_reg: To store contents of PO register.
63 * @pm_reg: To store contents of PM register.
64 * @im0_reg: To store contents of interrupt mode regist0
65 * @im1_reg: To store contents of interrupt mode regist1
66 * @use_sel_reg: To store contents of GPIO_USE_SEL0~3
68 struct ioh_gpio_reg_data
{
79 * struct ioh_gpio - GPIO private data structure.
80 * @base: PCI base address of Memory mapped I/O register.
81 * @reg: Memory mapped IOH GPIO register list.
82 * @dev: Pointer to device structure.
83 * @gpio: Data for GPIO infrastructure.
84 * @ioh_gpio_reg: Memory mapped Register data is saved here
86 * @gpio_use_sel: Save GPIO_USE_SEL1~4 register for PM
87 * @ch: Indicate GPIO channel
88 * @irq_base: Save base of IRQ number for interrupt
89 * @spinlock: Used for register access protection in
90 * interrupt context ioh_irq_type and PM;
94 struct ioh_regs __iomem
*reg
;
96 struct gpio_chip gpio
;
97 struct ioh_gpio_reg_data ioh_gpio_reg
;
105 static const int num_ports
[] = {6, 12, 16, 16, 15, 16, 16, 12};
107 static void ioh_gpio_set(struct gpio_chip
*gpio
, unsigned nr
, int val
)
110 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
112 mutex_lock(&chip
->lock
);
113 reg_val
= ioread32(&chip
->reg
->regs
[chip
->ch
].po
);
115 reg_val
|= (1 << nr
);
117 reg_val
&= ~(1 << nr
);
119 iowrite32(reg_val
, &chip
->reg
->regs
[chip
->ch
].po
);
120 mutex_unlock(&chip
->lock
);
123 static int ioh_gpio_get(struct gpio_chip
*gpio
, unsigned nr
)
125 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
127 return ioread32(&chip
->reg
->regs
[chip
->ch
].pi
) & (1 << nr
);
130 static int ioh_gpio_direction_output(struct gpio_chip
*gpio
, unsigned nr
,
133 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
137 mutex_lock(&chip
->lock
);
138 pm
= ioread32(&chip
->reg
->regs
[chip
->ch
].pm
) &
139 ((1 << num_ports
[chip
->ch
]) - 1);
141 iowrite32(pm
, &chip
->reg
->regs
[chip
->ch
].pm
);
143 reg_val
= ioread32(&chip
->reg
->regs
[chip
->ch
].po
);
145 reg_val
|= (1 << nr
);
147 reg_val
&= ~(1 << nr
);
148 iowrite32(reg_val
, &chip
->reg
->regs
[chip
->ch
].po
);
150 mutex_unlock(&chip
->lock
);
155 static int ioh_gpio_direction_input(struct gpio_chip
*gpio
, unsigned nr
)
157 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
160 mutex_lock(&chip
->lock
);
161 pm
= ioread32(&chip
->reg
->regs
[chip
->ch
].pm
) &
162 ((1 << num_ports
[chip
->ch
]) - 1);
164 iowrite32(pm
, &chip
->reg
->regs
[chip
->ch
].pm
);
165 mutex_unlock(&chip
->lock
);
172 * Save register configuration and disable interrupts.
174 static void ioh_gpio_save_reg_conf(struct ioh_gpio
*chip
)
178 for (i
= 0; i
< 8; i
++, chip
++) {
179 chip
->ioh_gpio_reg
.po_reg
=
180 ioread32(&chip
->reg
->regs
[chip
->ch
].po
);
181 chip
->ioh_gpio_reg
.pm_reg
=
182 ioread32(&chip
->reg
->regs
[chip
->ch
].pm
);
183 chip
->ioh_gpio_reg
.ien_reg
=
184 ioread32(&chip
->reg
->regs
[chip
->ch
].ien
);
185 chip
->ioh_gpio_reg
.imask_reg
=
186 ioread32(&chip
->reg
->regs
[chip
->ch
].imask
);
187 chip
->ioh_gpio_reg
.im0_reg
=
188 ioread32(&chip
->reg
->regs
[chip
->ch
].im_0
);
189 chip
->ioh_gpio_reg
.im1_reg
=
190 ioread32(&chip
->reg
->regs
[chip
->ch
].im_1
);
192 chip
->ioh_gpio_reg
.use_sel_reg
=
193 ioread32(&chip
->reg
->ioh_sel_reg
[i
]);
198 * This function restores the register configuration of the GPIO device.
200 static void ioh_gpio_restore_reg_conf(struct ioh_gpio
*chip
)
204 for (i
= 0; i
< 8; i
++, chip
++) {
205 iowrite32(chip
->ioh_gpio_reg
.po_reg
,
206 &chip
->reg
->regs
[chip
->ch
].po
);
207 iowrite32(chip
->ioh_gpio_reg
.pm_reg
,
208 &chip
->reg
->regs
[chip
->ch
].pm
);
209 iowrite32(chip
->ioh_gpio_reg
.ien_reg
,
210 &chip
->reg
->regs
[chip
->ch
].ien
);
211 iowrite32(chip
->ioh_gpio_reg
.imask_reg
,
212 &chip
->reg
->regs
[chip
->ch
].imask
);
213 iowrite32(chip
->ioh_gpio_reg
.im0_reg
,
214 &chip
->reg
->regs
[chip
->ch
].im_0
);
215 iowrite32(chip
->ioh_gpio_reg
.im1_reg
,
216 &chip
->reg
->regs
[chip
->ch
].im_1
);
218 iowrite32(chip
->ioh_gpio_reg
.use_sel_reg
,
219 &chip
->reg
->ioh_sel_reg
[i
]);
224 static int ioh_gpio_to_irq(struct gpio_chip
*gpio
, unsigned offset
)
226 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
227 return chip
->irq_base
+ offset
;
230 static void ioh_gpio_setup(struct ioh_gpio
*chip
, int num_port
)
232 struct gpio_chip
*gpio
= &chip
->gpio
;
234 gpio
->label
= dev_name(chip
->dev
);
235 gpio
->owner
= THIS_MODULE
;
236 gpio
->direction_input
= ioh_gpio_direction_input
;
237 gpio
->get
= ioh_gpio_get
;
238 gpio
->direction_output
= ioh_gpio_direction_output
;
239 gpio
->set
= ioh_gpio_set
;
240 gpio
->dbg_show
= NULL
;
242 gpio
->ngpio
= num_port
;
244 gpio
->to_irq
= ioh_gpio_to_irq
;
247 static int ioh_irq_type(struct irq_data
*d
, unsigned int type
)
257 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
258 struct ioh_gpio
*chip
= gc
->private;
260 ch
= irq
- chip
->irq_base
;
261 if (irq
<= chip
->irq_base
+ 7) {
262 im_reg
= &chip
->reg
->regs
[chip
->ch
].im_0
;
265 im_reg
= &chip
->reg
->regs
[chip
->ch
].im_1
;
268 dev_dbg(chip
->dev
, "%s:irq=%d type=%d ch=%d pos=%d type=%d\n",
269 __func__
, irq
, type
, ch
, im_pos
, type
);
271 spin_lock_irqsave(&chip
->spinlock
, flags
);
274 case IRQ_TYPE_EDGE_RISING
:
275 val
= IOH_EDGE_RISING
;
277 case IRQ_TYPE_EDGE_FALLING
:
278 val
= IOH_EDGE_FALLING
;
280 case IRQ_TYPE_EDGE_BOTH
:
283 case IRQ_TYPE_LEVEL_HIGH
:
286 case IRQ_TYPE_LEVEL_LOW
:
292 dev_warn(chip
->dev
, "%s: unknown type(%dd)",
297 /* Set interrupt mode */
298 im
= ioread32(im_reg
) & ~(IOH_IM_MASK
<< (im_pos
* 4));
299 iowrite32(im
| (val
<< (im_pos
* 4)), im_reg
);
302 iowrite32(BIT(ch
), &chip
->reg
->regs
[chip
->ch
].iclr
);
305 iowrite32(BIT(ch
), &chip
->reg
->regs
[chip
->ch
].imaskclr
);
307 /* Enable interrupt */
308 ien
= ioread32(&chip
->reg
->regs
[chip
->ch
].ien
);
309 iowrite32(ien
| BIT(ch
), &chip
->reg
->regs
[chip
->ch
].ien
);
311 spin_unlock_irqrestore(&chip
->spinlock
, flags
);
316 static void ioh_irq_unmask(struct irq_data
*d
)
318 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
319 struct ioh_gpio
*chip
= gc
->private;
321 iowrite32(1 << (d
->irq
- chip
->irq_base
),
322 &chip
->reg
->regs
[chip
->ch
].imaskclr
);
325 static void ioh_irq_mask(struct irq_data
*d
)
327 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
328 struct ioh_gpio
*chip
= gc
->private;
330 iowrite32(1 << (d
->irq
- chip
->irq_base
),
331 &chip
->reg
->regs
[chip
->ch
].imask
);
334 static irqreturn_t
ioh_gpio_handler(int irq
, void *dev_id
)
336 struct ioh_gpio
*chip
= dev_id
;
341 for (i
= 0; i
< 8; i
++) {
342 reg_val
= ioread32(&chip
->reg
->regs
[i
].istatus
);
343 for (j
= 0; j
< num_ports
[i
]; j
++) {
344 if (reg_val
& BIT(j
)) {
346 "%s:[%d]:irq=%d status=0x%x\n",
347 __func__
, j
, irq
, reg_val
);
349 &chip
->reg
->regs
[chip
->ch
].iclr
);
350 generic_handle_irq(chip
->irq_base
+ j
);
358 static __devinit
void ioh_gpio_alloc_generic_chip(struct ioh_gpio
*chip
,
359 unsigned int irq_start
, unsigned int num
)
361 struct irq_chip_generic
*gc
;
362 struct irq_chip_type
*ct
;
364 gc
= irq_alloc_generic_chip("ioh_gpio", 1, irq_start
, chip
->base
,
369 ct
->chip
.irq_mask
= ioh_irq_mask
;
370 ct
->chip
.irq_unmask
= ioh_irq_unmask
;
371 ct
->chip
.irq_set_type
= ioh_irq_type
;
373 irq_setup_generic_chip(gc
, IRQ_MSK(num
), IRQ_GC_INIT_MASK_CACHE
,
374 IRQ_NOREQUEST
| IRQ_NOPROBE
, 0);
377 static int __devinit
ioh_gpio_probe(struct pci_dev
*pdev
,
378 const struct pci_device_id
*id
)
382 struct ioh_gpio
*chip
;
384 void __iomem
*chip_save
;
387 ret
= pci_enable_device(pdev
);
389 dev_err(&pdev
->dev
, "%s : pci_enable_device failed", __func__
);
393 ret
= pci_request_regions(pdev
, KBUILD_MODNAME
);
395 dev_err(&pdev
->dev
, "pci_request_regions failed-%d", ret
);
396 goto err_request_regions
;
399 base
= pci_iomap(pdev
, 1, 0);
401 dev_err(&pdev
->dev
, "%s : pci_iomap failed", __func__
);
406 chip_save
= kzalloc(sizeof(*chip
) * 8, GFP_KERNEL
);
407 if (chip_save
== NULL
) {
408 dev_err(&pdev
->dev
, "%s : kzalloc failed", __func__
);
414 for (i
= 0; i
< 8; i
++, chip
++) {
415 chip
->dev
= &pdev
->dev
;
417 chip
->reg
= chip
->base
;
419 mutex_init(&chip
->lock
);
420 ioh_gpio_setup(chip
, num_ports
[i
]);
421 ret
= gpiochip_add(&chip
->gpio
);
423 dev_err(&pdev
->dev
, "IOH gpio: Failed to register GPIO\n");
424 goto err_gpiochip_add
;
429 for (j
= 0; j
< 8; j
++, chip
++) {
430 irq_base
= irq_alloc_descs(-1, IOH_IRQ_BASE
, num_ports
[j
],
434 "ml_ioh_gpio: Failed to get IRQ base num\n");
436 goto err_irq_alloc_descs
;
438 chip
->irq_base
= irq_base
;
439 ioh_gpio_alloc_generic_chip(chip
, irq_base
, num_ports
[j
]);
443 ret
= request_irq(pdev
->irq
, ioh_gpio_handler
,
444 IRQF_SHARED
, KBUILD_MODNAME
, chip
);
447 "%s request_irq failed\n", __func__
);
448 goto err_request_irq
;
451 pci_set_drvdata(pdev
, chip
);
460 irq_free_descs(chip
->irq_base
, num_ports
[j
]);
467 ret
= gpiochip_remove(&chip
->gpio
);
469 dev_err(&pdev
->dev
, "Failed gpiochip_remove(%d)\n", i
);
474 pci_iounmap(pdev
, base
);
477 pci_release_regions(pdev
);
480 pci_disable_device(pdev
);
484 dev_err(&pdev
->dev
, "%s Failed returns %d\n", __func__
, ret
);
488 static void __devexit
ioh_gpio_remove(struct pci_dev
*pdev
)
492 struct ioh_gpio
*chip
= pci_get_drvdata(pdev
);
493 void __iomem
*chip_save
;
497 free_irq(pdev
->irq
, chip
);
499 for (i
= 0; i
< 8; i
++, chip
++) {
500 irq_free_descs(chip
->irq_base
, num_ports
[i
]);
501 err
= gpiochip_remove(&chip
->gpio
);
503 dev_err(&pdev
->dev
, "Failed gpiochip_remove\n");
507 pci_iounmap(pdev
, chip
->base
);
508 pci_release_regions(pdev
);
509 pci_disable_device(pdev
);
514 static int ioh_gpio_suspend(struct pci_dev
*pdev
, pm_message_t state
)
517 struct ioh_gpio
*chip
= pci_get_drvdata(pdev
);
520 spin_lock_irqsave(&chip
->spinlock
, flags
);
521 ioh_gpio_save_reg_conf(chip
);
522 spin_unlock_irqrestore(&chip
->spinlock
, flags
);
524 ret
= pci_save_state(pdev
);
526 dev_err(&pdev
->dev
, "pci_save_state Failed-%d\n", ret
);
529 pci_disable_device(pdev
);
530 pci_set_power_state(pdev
, PCI_D0
);
531 ret
= pci_enable_wake(pdev
, PCI_D0
, 1);
533 dev_err(&pdev
->dev
, "pci_enable_wake Failed -%d\n", ret
);
538 static int ioh_gpio_resume(struct pci_dev
*pdev
)
541 struct ioh_gpio
*chip
= pci_get_drvdata(pdev
);
544 ret
= pci_enable_wake(pdev
, PCI_D0
, 0);
546 pci_set_power_state(pdev
, PCI_D0
);
547 ret
= pci_enable_device(pdev
);
549 dev_err(&pdev
->dev
, "pci_enable_device Failed-%d ", ret
);
552 pci_restore_state(pdev
);
554 spin_lock_irqsave(&chip
->spinlock
, flags
);
555 iowrite32(0x01, &chip
->reg
->srst
);
556 iowrite32(0x00, &chip
->reg
->srst
);
557 ioh_gpio_restore_reg_conf(chip
);
558 spin_unlock_irqrestore(&chip
->spinlock
, flags
);
563 #define ioh_gpio_suspend NULL
564 #define ioh_gpio_resume NULL
567 static DEFINE_PCI_DEVICE_TABLE(ioh_gpio_pcidev_id
) = {
568 { PCI_DEVICE(PCI_VENDOR_ID_ROHM
, 0x802E) },
571 MODULE_DEVICE_TABLE(pci
, ioh_gpio_pcidev_id
);
573 static struct pci_driver ioh_gpio_driver
= {
574 .name
= "ml_ioh_gpio",
575 .id_table
= ioh_gpio_pcidev_id
,
576 .probe
= ioh_gpio_probe
,
577 .remove
= __devexit_p(ioh_gpio_remove
),
578 .suspend
= ioh_gpio_suspend
,
579 .resume
= ioh_gpio_resume
582 static int __init
ioh_gpio_pci_init(void)
584 return pci_register_driver(&ioh_gpio_driver
);
586 module_init(ioh_gpio_pci_init
);
588 static void __exit
ioh_gpio_pci_exit(void)
590 pci_unregister_driver(&ioh_gpio_driver
);
592 module_exit(ioh_gpio_pci_exit
);
594 MODULE_DESCRIPTION("OKI SEMICONDUCTOR ML-IOH series GPIO Driver");
595 MODULE_LICENSE("GPL");