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/pci.h>
19 #include <linux/gpio.h>
21 #define PCI_VENDOR_ID_ROHM 0x10DB
39 struct ioh_reg_comn regs
[8];
47 * struct ioh_gpio_reg_data - The register store data.
48 * @po_reg: To store contents of PO register.
49 * @pm_reg: To store contents of PM register.
51 struct ioh_gpio_reg_data
{
57 * struct ioh_gpio - GPIO private data structure.
58 * @base: PCI base address of Memory mapped I/O register.
59 * @reg: Memory mapped IOH GPIO register list.
60 * @dev: Pointer to device structure.
61 * @gpio: Data for GPIO infrastructure.
62 * @ioh_gpio_reg: Memory mapped Register data is saved here
64 * @ch: Indicate GPIO channel
68 struct ioh_regs __iomem
*reg
;
70 struct gpio_chip gpio
;
71 struct ioh_gpio_reg_data ioh_gpio_reg
;
76 static const int num_ports
[] = {6, 12, 16, 16, 15, 16, 16, 12};
78 static void ioh_gpio_set(struct gpio_chip
*gpio
, unsigned nr
, int val
)
81 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
83 mutex_lock(&chip
->lock
);
84 reg_val
= ioread32(&chip
->reg
->regs
[chip
->ch
].po
);
88 reg_val
&= ~(1 << nr
);
90 iowrite32(reg_val
, &chip
->reg
->regs
[chip
->ch
].po
);
91 mutex_unlock(&chip
->lock
);
94 static int ioh_gpio_get(struct gpio_chip
*gpio
, unsigned nr
)
96 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
98 return ioread32(&chip
->reg
->regs
[chip
->ch
].pi
) & (1 << nr
);
101 static int ioh_gpio_direction_output(struct gpio_chip
*gpio
, unsigned nr
,
104 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
108 mutex_lock(&chip
->lock
);
109 pm
= ioread32(&chip
->reg
->regs
[chip
->ch
].pm
) &
110 ((1 << num_ports
[chip
->ch
]) - 1);
112 iowrite32(pm
, &chip
->reg
->regs
[chip
->ch
].pm
);
114 reg_val
= ioread32(&chip
->reg
->regs
[chip
->ch
].po
);
116 reg_val
|= (1 << nr
);
118 reg_val
&= ~(1 << nr
);
120 mutex_unlock(&chip
->lock
);
125 static int ioh_gpio_direction_input(struct gpio_chip
*gpio
, unsigned nr
)
127 struct ioh_gpio
*chip
= container_of(gpio
, struct ioh_gpio
, gpio
);
130 mutex_lock(&chip
->lock
);
131 pm
= ioread32(&chip
->reg
->regs
[chip
->ch
].pm
) &
132 ((1 << num_ports
[chip
->ch
]) - 1);
134 iowrite32(pm
, &chip
->reg
->regs
[chip
->ch
].pm
);
135 mutex_unlock(&chip
->lock
);
141 * Save register configuration and disable interrupts.
143 static void ioh_gpio_save_reg_conf(struct ioh_gpio
*chip
)
145 chip
->ioh_gpio_reg
.po_reg
= ioread32(&chip
->reg
->regs
[chip
->ch
].po
);
146 chip
->ioh_gpio_reg
.pm_reg
= ioread32(&chip
->reg
->regs
[chip
->ch
].pm
);
150 * This function restores the register configuration of the GPIO device.
152 static void ioh_gpio_restore_reg_conf(struct ioh_gpio
*chip
)
154 /* to store contents of PO register */
155 iowrite32(chip
->ioh_gpio_reg
.po_reg
, &chip
->reg
->regs
[chip
->ch
].po
);
156 /* to store contents of PM register */
157 iowrite32(chip
->ioh_gpio_reg
.pm_reg
, &chip
->reg
->regs
[chip
->ch
].pm
);
160 static void ioh_gpio_setup(struct ioh_gpio
*chip
, int num_port
)
162 struct gpio_chip
*gpio
= &chip
->gpio
;
164 gpio
->label
= dev_name(chip
->dev
);
165 gpio
->owner
= THIS_MODULE
;
166 gpio
->direction_input
= ioh_gpio_direction_input
;
167 gpio
->get
= ioh_gpio_get
;
168 gpio
->direction_output
= ioh_gpio_direction_output
;
169 gpio
->set
= ioh_gpio_set
;
170 gpio
->dbg_show
= NULL
;
172 gpio
->ngpio
= num_port
;
176 static int __devinit
ioh_gpio_probe(struct pci_dev
*pdev
,
177 const struct pci_device_id
*id
)
181 struct ioh_gpio
*chip
;
183 void __iomem
*chip_save
;
185 ret
= pci_enable_device(pdev
);
187 dev_err(&pdev
->dev
, "%s : pci_enable_device failed", __func__
);
191 ret
= pci_request_regions(pdev
, KBUILD_MODNAME
);
193 dev_err(&pdev
->dev
, "pci_request_regions failed-%d", ret
);
194 goto err_request_regions
;
197 base
= pci_iomap(pdev
, 1, 0);
199 dev_err(&pdev
->dev
, "%s : pci_iomap failed", __func__
);
204 chip_save
= kzalloc(sizeof(*chip
) * 8, GFP_KERNEL
);
205 if (chip_save
== NULL
) {
206 dev_err(&pdev
->dev
, "%s : kzalloc failed", __func__
);
212 for (i
= 0; i
< 8; i
++, chip
++) {
213 chip
->dev
= &pdev
->dev
;
215 chip
->reg
= chip
->base
;
217 mutex_init(&chip
->lock
);
218 ioh_gpio_setup(chip
, num_ports
[i
]);
219 ret
= gpiochip_add(&chip
->gpio
);
221 dev_err(&pdev
->dev
, "IOH gpio: Failed to register GPIO\n");
222 goto err_gpiochip_add
;
227 pci_set_drvdata(pdev
, chip
);
232 for (; i
!= 0; i
--) {
234 ret
= gpiochip_remove(&chip
->gpio
);
236 dev_err(&pdev
->dev
, "Failed gpiochip_remove(%d)\n", i
);
241 pci_iounmap(pdev
, base
);
244 pci_release_regions(pdev
);
247 pci_disable_device(pdev
);
251 dev_err(&pdev
->dev
, "%s Failed returns %d\n", __func__
, ret
);
255 static void __devexit
ioh_gpio_remove(struct pci_dev
*pdev
)
259 struct ioh_gpio
*chip
= pci_get_drvdata(pdev
);
260 void __iomem
*chip_save
;
263 for (i
= 0; i
< 8; i
++, chip
++) {
264 err
= gpiochip_remove(&chip
->gpio
);
266 dev_err(&pdev
->dev
, "Failed gpiochip_remove\n");
270 pci_iounmap(pdev
, chip
->base
);
271 pci_release_regions(pdev
);
272 pci_disable_device(pdev
);
277 static int ioh_gpio_suspend(struct pci_dev
*pdev
, pm_message_t state
)
280 struct ioh_gpio
*chip
= pci_get_drvdata(pdev
);
282 ioh_gpio_save_reg_conf(chip
);
283 ioh_gpio_restore_reg_conf(chip
);
285 ret
= pci_save_state(pdev
);
287 dev_err(&pdev
->dev
, "pci_save_state Failed-%d\n", ret
);
290 pci_disable_device(pdev
);
291 pci_set_power_state(pdev
, PCI_D0
);
292 ret
= pci_enable_wake(pdev
, PCI_D0
, 1);
294 dev_err(&pdev
->dev
, "pci_enable_wake Failed -%d\n", ret
);
299 static int ioh_gpio_resume(struct pci_dev
*pdev
)
302 struct ioh_gpio
*chip
= pci_get_drvdata(pdev
);
304 ret
= pci_enable_wake(pdev
, PCI_D0
, 0);
306 pci_set_power_state(pdev
, PCI_D0
);
307 ret
= pci_enable_device(pdev
);
309 dev_err(&pdev
->dev
, "pci_enable_device Failed-%d ", ret
);
312 pci_restore_state(pdev
);
314 iowrite32(0x01, &chip
->reg
->srst
);
315 iowrite32(0x00, &chip
->reg
->srst
);
316 ioh_gpio_restore_reg_conf(chip
);
321 #define ioh_gpio_suspend NULL
322 #define ioh_gpio_resume NULL
325 static DEFINE_PCI_DEVICE_TABLE(ioh_gpio_pcidev_id
) = {
326 { PCI_DEVICE(PCI_VENDOR_ID_ROHM
, 0x802E) },
329 MODULE_DEVICE_TABLE(pci
, ioh_gpio_pcidev_id
);
331 static struct pci_driver ioh_gpio_driver
= {
332 .name
= "ml_ioh_gpio",
333 .id_table
= ioh_gpio_pcidev_id
,
334 .probe
= ioh_gpio_probe
,
335 .remove
= __devexit_p(ioh_gpio_remove
),
336 .suspend
= ioh_gpio_suspend
,
337 .resume
= ioh_gpio_resume
340 static int __init
ioh_gpio_pci_init(void)
342 return pci_register_driver(&ioh_gpio_driver
);
344 module_init(ioh_gpio_pci_init
);
346 static void __exit
ioh_gpio_pci_exit(void)
348 pci_unregister_driver(&ioh_gpio_driver
);
350 module_exit(ioh_gpio_pci_exit
);
352 MODULE_DESCRIPTION("OKI SEMICONDUCTOR ML-IOH series GPIO Driver");
353 MODULE_LICENSE("GPL");