gpio-pch.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
  3. *
  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.
  7. *
  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.
  12. *
  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.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/gpio.h>
  20. #define PCH_GPIO_ALL_PINS 0xfff /* Mask for GPIO pins 0 to 11 */
  21. #define GPIO_NUM_PINS 12 /* Specifies number of GPIO PINS GPIO0-GPIO11 */
  22. struct pch_regs {
  23. u32 ien;
  24. u32 istatus;
  25. u32 idisp;
  26. u32 iclr;
  27. u32 imask;
  28. u32 imaskclr;
  29. u32 po;
  30. u32 pi;
  31. u32 pm;
  32. u32 im0;
  33. u32 im1;
  34. u32 reserved[4];
  35. u32 reset;
  36. };
  37. /**
  38. * struct pch_gpio_reg_data - The register store data.
  39. * @po_reg: To store contents of PO register.
  40. * @pm_reg: To store contents of PM register.
  41. */
  42. struct pch_gpio_reg_data {
  43. u32 po_reg;
  44. u32 pm_reg;
  45. };
  46. /**
  47. * struct pch_gpio - GPIO private data structure.
  48. * @base: PCI base address of Memory mapped I/O register.
  49. * @reg: Memory mapped PCH GPIO register list.
  50. * @dev: Pointer to device structure.
  51. * @gpio: Data for GPIO infrastructure.
  52. * @pch_gpio_reg: Memory mapped Register data is saved here
  53. * when suspend.
  54. * @spinlock: Used for register access protection in
  55. * interrupt context pch_irq_mask,
  56. * pch_irq_unmask and pch_irq_type;
  57. */
  58. struct pch_gpio {
  59. void __iomem *base;
  60. struct pch_regs __iomem *reg;
  61. struct device *dev;
  62. struct gpio_chip gpio;
  63. struct pch_gpio_reg_data pch_gpio_reg;
  64. struct mutex lock;
  65. spinlock_t spinlock;
  66. };
  67. static void pch_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
  68. {
  69. u32 reg_val;
  70. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  71. mutex_lock(&chip->lock);
  72. reg_val = ioread32(&chip->reg->po);
  73. if (val)
  74. reg_val |= (1 << nr);
  75. else
  76. reg_val &= ~(1 << nr);
  77. iowrite32(reg_val, &chip->reg->po);
  78. mutex_unlock(&chip->lock);
  79. }
  80. static int pch_gpio_get(struct gpio_chip *gpio, unsigned nr)
  81. {
  82. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  83. return ioread32(&chip->reg->pi) & (1 << nr);
  84. }
  85. static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  86. int val)
  87. {
  88. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  89. u32 pm;
  90. u32 reg_val;
  91. mutex_lock(&chip->lock);
  92. pm = ioread32(&chip->reg->pm) & PCH_GPIO_ALL_PINS;
  93. pm |= (1 << nr);
  94. iowrite32(pm, &chip->reg->pm);
  95. reg_val = ioread32(&chip->reg->po);
  96. if (val)
  97. reg_val |= (1 << nr);
  98. else
  99. reg_val &= ~(1 << nr);
  100. iowrite32(reg_val, &chip->reg->po);
  101. mutex_unlock(&chip->lock);
  102. return 0;
  103. }
  104. static int pch_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  105. {
  106. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  107. u32 pm;
  108. mutex_lock(&chip->lock);
  109. pm = ioread32(&chip->reg->pm) & PCH_GPIO_ALL_PINS; /*bits 0-11*/
  110. pm &= ~(1 << nr);
  111. iowrite32(pm, &chip->reg->pm);
  112. mutex_unlock(&chip->lock);
  113. return 0;
  114. }
  115. /*
  116. * Save register configuration and disable interrupts.
  117. */
  118. static void pch_gpio_save_reg_conf(struct pch_gpio *chip)
  119. {
  120. chip->pch_gpio_reg.po_reg = ioread32(&chip->reg->po);
  121. chip->pch_gpio_reg.pm_reg = ioread32(&chip->reg->pm);
  122. }
  123. /*
  124. * This function restores the register configuration of the GPIO device.
  125. */
  126. static void pch_gpio_restore_reg_conf(struct pch_gpio *chip)
  127. {
  128. /* to store contents of PO register */
  129. iowrite32(chip->pch_gpio_reg.po_reg, &chip->reg->po);
  130. /* to store contents of PM register */
  131. iowrite32(chip->pch_gpio_reg.pm_reg, &chip->reg->pm);
  132. }
  133. static void pch_gpio_setup(struct pch_gpio *chip)
  134. {
  135. struct gpio_chip *gpio = &chip->gpio;
  136. gpio->label = dev_name(chip->dev);
  137. gpio->owner = THIS_MODULE;
  138. gpio->direction_input = pch_gpio_direction_input;
  139. gpio->get = pch_gpio_get;
  140. gpio->direction_output = pch_gpio_direction_output;
  141. gpio->set = pch_gpio_set;
  142. gpio->dbg_show = NULL;
  143. gpio->base = -1;
  144. gpio->ngpio = GPIO_NUM_PINS;
  145. gpio->can_sleep = 0;
  146. }
  147. static int __devinit pch_gpio_probe(struct pci_dev *pdev,
  148. const struct pci_device_id *id)
  149. {
  150. s32 ret;
  151. struct pch_gpio *chip;
  152. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  153. if (chip == NULL)
  154. return -ENOMEM;
  155. chip->dev = &pdev->dev;
  156. ret = pci_enable_device(pdev);
  157. if (ret) {
  158. dev_err(&pdev->dev, "%s : pci_enable_device FAILED", __func__);
  159. goto err_pci_enable;
  160. }
  161. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  162. if (ret) {
  163. dev_err(&pdev->dev, "pci_request_regions FAILED-%d", ret);
  164. goto err_request_regions;
  165. }
  166. chip->base = pci_iomap(pdev, 1, 0);
  167. if (chip->base == 0) {
  168. dev_err(&pdev->dev, "%s : pci_iomap FAILED", __func__);
  169. ret = -ENOMEM;
  170. goto err_iomap;
  171. }
  172. chip->reg = chip->base;
  173. pci_set_drvdata(pdev, chip);
  174. mutex_init(&chip->lock);
  175. pch_gpio_setup(chip);
  176. ret = gpiochip_add(&chip->gpio);
  177. if (ret) {
  178. dev_err(&pdev->dev, "PCH gpio: Failed to register GPIO\n");
  179. goto err_gpiochip_add;
  180. }
  181. return 0;
  182. err_gpiochip_add:
  183. pci_iounmap(pdev, chip->base);
  184. err_iomap:
  185. pci_release_regions(pdev);
  186. err_request_regions:
  187. pci_disable_device(pdev);
  188. err_pci_enable:
  189. kfree(chip);
  190. dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
  191. return ret;
  192. }
  193. static void __devexit pch_gpio_remove(struct pci_dev *pdev)
  194. {
  195. int err;
  196. struct pch_gpio *chip = pci_get_drvdata(pdev);
  197. err = gpiochip_remove(&chip->gpio);
  198. if (err)
  199. dev_err(&pdev->dev, "Failed gpiochip_remove\n");
  200. pci_iounmap(pdev, chip->base);
  201. pci_release_regions(pdev);
  202. pci_disable_device(pdev);
  203. kfree(chip);
  204. }
  205. #ifdef CONFIG_PM
  206. static int pch_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
  207. {
  208. s32 ret;
  209. struct pch_gpio *chip = pci_get_drvdata(pdev);
  210. unsigned long flags;
  211. spin_lock_irqsave(&chip->spinlock, flags);
  212. pch_gpio_save_reg_conf(chip);
  213. spin_unlock_irqrestore(&chip->spinlock, flags);
  214. ret = pci_save_state(pdev);
  215. if (ret) {
  216. dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
  217. return ret;
  218. }
  219. pci_disable_device(pdev);
  220. pci_set_power_state(pdev, PCI_D0);
  221. ret = pci_enable_wake(pdev, PCI_D0, 1);
  222. if (ret)
  223. dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
  224. return 0;
  225. }
  226. static int pch_gpio_resume(struct pci_dev *pdev)
  227. {
  228. s32 ret;
  229. struct pch_gpio *chip = pci_get_drvdata(pdev);
  230. unsigned long flags;
  231. ret = pci_enable_wake(pdev, PCI_D0, 0);
  232. pci_set_power_state(pdev, PCI_D0);
  233. ret = pci_enable_device(pdev);
  234. if (ret) {
  235. dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
  236. return ret;
  237. }
  238. pci_restore_state(pdev);
  239. spin_lock_irqsave(&chip->spinlock, flags);
  240. iowrite32(0x01, &chip->reg->reset);
  241. iowrite32(0x00, &chip->reg->reset);
  242. pch_gpio_restore_reg_conf(chip);
  243. spin_unlock_irqrestore(&chip->spinlock, flags);
  244. return 0;
  245. }
  246. #else
  247. #define pch_gpio_suspend NULL
  248. #define pch_gpio_resume NULL
  249. #endif
  250. #define PCI_VENDOR_ID_ROHM 0x10DB
  251. static DEFINE_PCI_DEVICE_TABLE(pch_gpio_pcidev_id) = {
  252. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8803) },
  253. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8014) },
  254. { 0, }
  255. };
  256. MODULE_DEVICE_TABLE(pci, pch_gpio_pcidev_id);
  257. static struct pci_driver pch_gpio_driver = {
  258. .name = "pch_gpio",
  259. .id_table = pch_gpio_pcidev_id,
  260. .probe = pch_gpio_probe,
  261. .remove = __devexit_p(pch_gpio_remove),
  262. .suspend = pch_gpio_suspend,
  263. .resume = pch_gpio_resume
  264. };
  265. static int __init pch_gpio_pci_init(void)
  266. {
  267. return pci_register_driver(&pch_gpio_driver);
  268. }
  269. module_init(pch_gpio_pci_init);
  270. static void __exit pch_gpio_pci_exit(void)
  271. {
  272. pci_unregister_driver(&pch_gpio_driver);
  273. }
  274. module_exit(pch_gpio_pci_exit);
  275. MODULE_DESCRIPTION("PCH GPIO PCI Driver");
  276. MODULE_LICENSE("GPL");