gpio-pch.c 9.0 KB

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