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 PCH_GPIO_ALL_PINS 0xfff /* Mask for GPIO pins 0 to 11 */
22 #define GPIO_NUM_PINS 12 /* Specifies number of GPIO PINS GPIO0-GPIO11 */
41 * struct pch_gpio_reg_data - The register store data.
42 * @po_reg: To store contents of PO register.
43 * @pm_reg: To store contents of PM register.
45 struct pch_gpio_reg_data {
51 * struct pch_gpio - GPIO private data structure.
52 * @base: PCI base address of Memory mapped I/O register.
53 * @reg: Memory mapped PCH GPIO register list.
54 * @dev: Pointer to device structure.
55 * @gpio: Data for GPIO infrastructure.
56 * @pch_gpio_reg: Memory mapped Register data is saved here
58 * @spinlock: Used for register access protection in
59 * interrupt context pch_irq_mask,
60 * pch_irq_unmask and pch_irq_type;
64 struct pch_regs __iomem *reg;
66 struct gpio_chip gpio;
67 struct pch_gpio_reg_data pch_gpio_reg;
72 static void pch_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
75 struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
77 mutex_lock(&chip->lock);
78 reg_val = ioread32(&chip->reg->po);
82 reg_val &= ~(1 << nr);
84 iowrite32(reg_val, &chip->reg->po);
85 mutex_unlock(&chip->lock);
88 static int pch_gpio_get(struct gpio_chip *gpio, unsigned nr)
90 struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
92 return ioread32(&chip->reg->pi) & (1 << nr);
95 static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
98 struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
102 mutex_lock(&chip->lock);
103 pm = ioread32(&chip->reg->pm) & PCH_GPIO_ALL_PINS;
105 iowrite32(pm, &chip->reg->pm);
107 reg_val = ioread32(&chip->reg->po);
109 reg_val |= (1 << nr);
111 reg_val &= ~(1 << nr);
112 iowrite32(reg_val, &chip->reg->po);
114 mutex_unlock(&chip->lock);
119 static int pch_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
121 struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
124 mutex_lock(&chip->lock);
125 pm = ioread32(&chip->reg->pm) & PCH_GPIO_ALL_PINS; /*bits 0-11*/
127 iowrite32(pm, &chip->reg->pm);
128 mutex_unlock(&chip->lock);
134 * Save register configuration and disable interrupts.
136 static void pch_gpio_save_reg_conf(struct pch_gpio *chip)
138 chip->pch_gpio_reg.po_reg = ioread32(&chip->reg->po);
139 chip->pch_gpio_reg.pm_reg = ioread32(&chip->reg->pm);
143 * This function restores the register configuration of the GPIO device.
145 static void pch_gpio_restore_reg_conf(struct pch_gpio *chip)
147 /* to store contents of PO register */
148 iowrite32(chip->pch_gpio_reg.po_reg, &chip->reg->po);
149 /* to store contents of PM register */
150 iowrite32(chip->pch_gpio_reg.pm_reg, &chip->reg->pm);
153 static void pch_gpio_setup(struct pch_gpio *chip)
155 struct gpio_chip *gpio = &chip->gpio;
157 gpio->label = dev_name(chip->dev);
158 gpio->owner = THIS_MODULE;
159 gpio->direction_input = pch_gpio_direction_input;
160 gpio->get = pch_gpio_get;
161 gpio->direction_output = pch_gpio_direction_output;
162 gpio->set = pch_gpio_set;
163 gpio->dbg_show = NULL;
165 gpio->ngpio = GPIO_NUM_PINS;
169 static int __devinit pch_gpio_probe(struct pci_dev *pdev,
170 const struct pci_device_id *id)
173 struct pch_gpio *chip;
175 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
179 chip->dev = &pdev->dev;
180 ret = pci_enable_device(pdev);
182 dev_err(&pdev->dev, "%s : pci_enable_device FAILED", __func__);
186 ret = pci_request_regions(pdev, KBUILD_MODNAME);
188 dev_err(&pdev->dev, "pci_request_regions FAILED-%d", ret);
189 goto err_request_regions;
192 chip->base = pci_iomap(pdev, 1, 0);
193 if (chip->base == 0) {
194 dev_err(&pdev->dev, "%s : pci_iomap FAILED", __func__);
199 chip->reg = chip->base;
200 pci_set_drvdata(pdev, chip);
201 mutex_init(&chip->lock);
202 pch_gpio_setup(chip);
203 ret = gpiochip_add(&chip->gpio);
205 dev_err(&pdev->dev, "PCH gpio: Failed to register GPIO\n");
206 goto err_gpiochip_add;
212 pci_iounmap(pdev, chip->base);
215 pci_release_regions(pdev);
218 pci_disable_device(pdev);
222 dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
226 static void __devexit pch_gpio_remove(struct pci_dev *pdev)
229 struct pch_gpio *chip = pci_get_drvdata(pdev);
231 err = gpiochip_remove(&chip->gpio);
233 dev_err(&pdev->dev, "Failed gpiochip_remove\n");
235 pci_iounmap(pdev, chip->base);
236 pci_release_regions(pdev);
237 pci_disable_device(pdev);
242 static int pch_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
245 struct pch_gpio *chip = pci_get_drvdata(pdev);
248 spin_lock_irqsave(&chip->spinlock, flags);
249 pch_gpio_save_reg_conf(chip);
250 spin_unlock_irqrestore(&chip->spinlock, flags);
252 ret = pci_save_state(pdev);
254 dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
257 pci_disable_device(pdev);
258 pci_set_power_state(pdev, PCI_D0);
259 ret = pci_enable_wake(pdev, PCI_D0, 1);
261 dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
266 static int pch_gpio_resume(struct pci_dev *pdev)
269 struct pch_gpio *chip = pci_get_drvdata(pdev);
272 ret = pci_enable_wake(pdev, PCI_D0, 0);
274 pci_set_power_state(pdev, PCI_D0);
275 ret = pci_enable_device(pdev);
277 dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
280 pci_restore_state(pdev);
282 spin_lock_irqsave(&chip->spinlock, flags);
283 iowrite32(0x01, &chip->reg->reset);
284 iowrite32(0x00, &chip->reg->reset);
285 pch_gpio_restore_reg_conf(chip);
286 spin_unlock_irqrestore(&chip->spinlock, flags);
291 #define pch_gpio_suspend NULL
292 #define pch_gpio_resume NULL
295 #define PCI_VENDOR_ID_ROHM 0x10DB
296 static DEFINE_PCI_DEVICE_TABLE(pch_gpio_pcidev_id) = {
297 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8803) },
298 { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8014) },
299 { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8043) },
302 MODULE_DEVICE_TABLE(pci, pch_gpio_pcidev_id);
304 static struct pci_driver pch_gpio_driver = {
306 .id_table = pch_gpio_pcidev_id,
307 .probe = pch_gpio_probe,
308 .remove = __devexit_p(pch_gpio_remove),
309 .suspend = pch_gpio_suspend,
310 .resume = pch_gpio_resume
313 static int __init pch_gpio_pci_init(void)
315 return pci_register_driver(&pch_gpio_driver);
317 module_init(pch_gpio_pci_init);
319 static void __exit pch_gpio_pci_exit(void)
321 pci_unregister_driver(&pch_gpio_driver);
323 module_exit(pch_gpio_pci_exit);
325 MODULE_DESCRIPTION("PCH GPIO PCI Driver");
326 MODULE_LICENSE("GPL");