gpio-pch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (C) 2011 LAPIS 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/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/gpio.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #define PCH_EDGE_FALLING 0
  24. #define PCH_EDGE_RISING BIT(0)
  25. #define PCH_LEVEL_L BIT(1)
  26. #define PCH_LEVEL_H (BIT(0) | BIT(1))
  27. #define PCH_EDGE_BOTH BIT(2)
  28. #define PCH_IM_MASK (BIT(0) | BIT(1) | BIT(2))
  29. #define PCH_IRQ_BASE 24
  30. struct pch_regs {
  31. u32 ien;
  32. u32 istatus;
  33. u32 idisp;
  34. u32 iclr;
  35. u32 imask;
  36. u32 imaskclr;
  37. u32 po;
  38. u32 pi;
  39. u32 pm;
  40. u32 im0;
  41. u32 im1;
  42. u32 reserved[3];
  43. u32 gpio_use_sel;
  44. u32 reset;
  45. };
  46. enum pch_type_t {
  47. INTEL_EG20T_PCH,
  48. OKISEMI_ML7223m_IOH, /* LAPIS Semiconductor ML7223 IOH PCIe Bus-m */
  49. OKISEMI_ML7223n_IOH /* LAPIS Semiconductor ML7223 IOH PCIe Bus-n */
  50. };
  51. /* Specifies number of GPIO PINS */
  52. static int gpio_pins[] = {
  53. [INTEL_EG20T_PCH] = 12,
  54. [OKISEMI_ML7223m_IOH] = 8,
  55. [OKISEMI_ML7223n_IOH] = 8,
  56. };
  57. /**
  58. * struct pch_gpio_reg_data - The register store data.
  59. * @ien_reg: To store contents of IEN register.
  60. * @imask_reg: To store contents of IMASK register.
  61. * @po_reg: To store contents of PO register.
  62. * @pm_reg: To store contents of PM register.
  63. * @im0_reg: To store contents of IM0 register.
  64. * @im1_reg: To store contents of IM1 register.
  65. * @gpio_use_sel_reg : To store contents of GPIO_USE_SEL register.
  66. * (Only ML7223 Bus-n)
  67. */
  68. struct pch_gpio_reg_data {
  69. u32 ien_reg;
  70. u32 imask_reg;
  71. u32 po_reg;
  72. u32 pm_reg;
  73. u32 im0_reg;
  74. u32 im1_reg;
  75. u32 gpio_use_sel_reg;
  76. };
  77. /**
  78. * struct pch_gpio - GPIO private data structure.
  79. * @base: PCI base address of Memory mapped I/O register.
  80. * @reg: Memory mapped PCH GPIO register list.
  81. * @dev: Pointer to device structure.
  82. * @gpio: Data for GPIO infrastructure.
  83. * @pch_gpio_reg: Memory mapped Register data is saved here
  84. * when suspend.
  85. * @lock: Used for register access protection
  86. * @irq_base: Save base of IRQ number for interrupt
  87. * @ioh: IOH ID
  88. * @spinlock: Used for register access protection in
  89. * interrupt context pch_irq_mask,
  90. * pch_irq_unmask and pch_irq_type;
  91. */
  92. struct pch_gpio {
  93. void __iomem *base;
  94. struct pch_regs __iomem *reg;
  95. struct device *dev;
  96. struct gpio_chip gpio;
  97. struct pch_gpio_reg_data pch_gpio_reg;
  98. struct mutex lock;
  99. int irq_base;
  100. enum pch_type_t ioh;
  101. spinlock_t spinlock;
  102. };
  103. static void pch_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
  104. {
  105. u32 reg_val;
  106. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  107. mutex_lock(&chip->lock);
  108. reg_val = ioread32(&chip->reg->po);
  109. if (val)
  110. reg_val |= (1 << nr);
  111. else
  112. reg_val &= ~(1 << nr);
  113. iowrite32(reg_val, &chip->reg->po);
  114. mutex_unlock(&chip->lock);
  115. }
  116. static int pch_gpio_get(struct gpio_chip *gpio, unsigned nr)
  117. {
  118. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  119. return ioread32(&chip->reg->pi) & (1 << nr);
  120. }
  121. static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  122. int val)
  123. {
  124. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  125. u32 pm;
  126. u32 reg_val;
  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. reg_val = ioread32(&chip->reg->po);
  132. if (val)
  133. reg_val |= (1 << nr);
  134. else
  135. reg_val &= ~(1 << nr);
  136. iowrite32(reg_val, &chip->reg->po);
  137. mutex_unlock(&chip->lock);
  138. return 0;
  139. }
  140. static int pch_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  141. {
  142. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  143. u32 pm;
  144. mutex_lock(&chip->lock);
  145. pm = ioread32(&chip->reg->pm) & ((1 << gpio_pins[chip->ioh]) - 1);
  146. pm &= ~(1 << nr);
  147. iowrite32(pm, &chip->reg->pm);
  148. mutex_unlock(&chip->lock);
  149. return 0;
  150. }
  151. /*
  152. * Save register configuration and disable interrupts.
  153. */
  154. static void pch_gpio_save_reg_conf(struct pch_gpio *chip)
  155. {
  156. chip->pch_gpio_reg.ien_reg = ioread32(&chip->reg->ien);
  157. chip->pch_gpio_reg.imask_reg = ioread32(&chip->reg->imask);
  158. chip->pch_gpio_reg.po_reg = ioread32(&chip->reg->po);
  159. chip->pch_gpio_reg.pm_reg = ioread32(&chip->reg->pm);
  160. chip->pch_gpio_reg.im0_reg = ioread32(&chip->reg->im0);
  161. if (chip->ioh == INTEL_EG20T_PCH)
  162. chip->pch_gpio_reg.im1_reg = ioread32(&chip->reg->im1);
  163. if (chip->ioh == OKISEMI_ML7223n_IOH)
  164. chip->pch_gpio_reg.gpio_use_sel_reg =\
  165. ioread32(&chip->reg->gpio_use_sel);
  166. }
  167. /*
  168. * This function restores the register configuration of the GPIO device.
  169. */
  170. static void pch_gpio_restore_reg_conf(struct pch_gpio *chip)
  171. {
  172. iowrite32(chip->pch_gpio_reg.ien_reg, &chip->reg->ien);
  173. iowrite32(chip->pch_gpio_reg.imask_reg, &chip->reg->imask);
  174. /* to store contents of PO register */
  175. iowrite32(chip->pch_gpio_reg.po_reg, &chip->reg->po);
  176. /* to store contents of PM register */
  177. iowrite32(chip->pch_gpio_reg.pm_reg, &chip->reg->pm);
  178. iowrite32(chip->pch_gpio_reg.im0_reg, &chip->reg->im0);
  179. if (chip->ioh == INTEL_EG20T_PCH)
  180. iowrite32(chip->pch_gpio_reg.im1_reg, &chip->reg->im1);
  181. if (chip->ioh == OKISEMI_ML7223n_IOH)
  182. iowrite32(chip->pch_gpio_reg.gpio_use_sel_reg,
  183. &chip->reg->gpio_use_sel);
  184. }
  185. static int pch_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
  186. {
  187. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  188. return chip->irq_base + offset;
  189. }
  190. static void pch_gpio_setup(struct pch_gpio *chip)
  191. {
  192. struct gpio_chip *gpio = &chip->gpio;
  193. gpio->label = dev_name(chip->dev);
  194. gpio->owner = THIS_MODULE;
  195. gpio->direction_input = pch_gpio_direction_input;
  196. gpio->get = pch_gpio_get;
  197. gpio->direction_output = pch_gpio_direction_output;
  198. gpio->set = pch_gpio_set;
  199. gpio->dbg_show = NULL;
  200. gpio->base = -1;
  201. gpio->ngpio = gpio_pins[chip->ioh];
  202. gpio->can_sleep = 0;
  203. gpio->to_irq = pch_gpio_to_irq;
  204. }
  205. static int pch_irq_type(struct irq_data *d, unsigned int type)
  206. {
  207. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  208. struct pch_gpio *chip = gc->private;
  209. u32 im, im_pos, val;
  210. u32 __iomem *im_reg;
  211. unsigned long flags;
  212. int ch, irq = d->irq;
  213. ch = irq - chip->irq_base;
  214. if (irq <= chip->irq_base + 7) {
  215. im_reg = &chip->reg->im0;
  216. im_pos = ch;
  217. } else {
  218. im_reg = &chip->reg->im1;
  219. im_pos = ch - 8;
  220. }
  221. dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d\n",
  222. __func__, irq, type, ch, im_pos);
  223. spin_lock_irqsave(&chip->spinlock, flags);
  224. switch (type) {
  225. case IRQ_TYPE_EDGE_RISING:
  226. val = PCH_EDGE_RISING;
  227. break;
  228. case IRQ_TYPE_EDGE_FALLING:
  229. val = PCH_EDGE_FALLING;
  230. break;
  231. case IRQ_TYPE_EDGE_BOTH:
  232. val = PCH_EDGE_BOTH;
  233. break;
  234. case IRQ_TYPE_LEVEL_HIGH:
  235. val = PCH_LEVEL_H;
  236. break;
  237. case IRQ_TYPE_LEVEL_LOW:
  238. val = PCH_LEVEL_L;
  239. break;
  240. default:
  241. goto unlock;
  242. }
  243. /* Set interrupt mode */
  244. im = ioread32(im_reg) & ~(PCH_IM_MASK << (im_pos * 4));
  245. iowrite32(im | (val << (im_pos * 4)), im_reg);
  246. /* And the handler */
  247. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  248. __irq_set_handler_locked(d->irq, handle_level_irq);
  249. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  250. __irq_set_handler_locked(d->irq, handle_edge_irq);
  251. unlock:
  252. spin_unlock_irqrestore(&chip->spinlock, flags);
  253. return 0;
  254. }
  255. static void pch_irq_unmask(struct irq_data *d)
  256. {
  257. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  258. struct pch_gpio *chip = gc->private;
  259. iowrite32(1 << (d->irq - chip->irq_base), &chip->reg->imaskclr);
  260. }
  261. static void pch_irq_mask(struct irq_data *d)
  262. {
  263. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  264. struct pch_gpio *chip = gc->private;
  265. iowrite32(1 << (d->irq - chip->irq_base), &chip->reg->imask);
  266. }
  267. static void pch_irq_ack(struct irq_data *d)
  268. {
  269. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  270. struct pch_gpio *chip = gc->private;
  271. iowrite32(1 << (d->irq - chip->irq_base), &chip->reg->iclr);
  272. }
  273. static irqreturn_t pch_gpio_handler(int irq, void *dev_id)
  274. {
  275. struct pch_gpio *chip = dev_id;
  276. u32 reg_val = ioread32(&chip->reg->istatus);
  277. int i, ret = IRQ_NONE;
  278. for (i = 0; i < gpio_pins[chip->ioh]; i++) {
  279. if (reg_val & BIT(i)) {
  280. dev_dbg(chip->dev, "%s:[%d]:irq=%d status=0x%x\n",
  281. __func__, i, irq, reg_val);
  282. generic_handle_irq(chip->irq_base + i);
  283. ret = IRQ_HANDLED;
  284. }
  285. }
  286. return ret;
  287. }
  288. static __devinit void pch_gpio_alloc_generic_chip(struct pch_gpio *chip,
  289. unsigned int irq_start, unsigned int num)
  290. {
  291. struct irq_chip_generic *gc;
  292. struct irq_chip_type *ct;
  293. gc = irq_alloc_generic_chip("pch_gpio", 1, irq_start, chip->base,
  294. handle_simple_irq);
  295. gc->private = chip;
  296. ct = gc->chip_types;
  297. ct->chip.irq_ack = pch_irq_ack;
  298. ct->chip.irq_mask = pch_irq_mask;
  299. ct->chip.irq_unmask = pch_irq_unmask;
  300. ct->chip.irq_set_type = pch_irq_type;
  301. irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
  302. IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  303. }
  304. static int __devinit pch_gpio_probe(struct pci_dev *pdev,
  305. const struct pci_device_id *id)
  306. {
  307. s32 ret;
  308. struct pch_gpio *chip;
  309. int irq_base;
  310. u32 msk;
  311. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  312. if (chip == NULL)
  313. return -ENOMEM;
  314. chip->dev = &pdev->dev;
  315. ret = pci_enable_device(pdev);
  316. if (ret) {
  317. dev_err(&pdev->dev, "%s : pci_enable_device FAILED", __func__);
  318. goto err_pci_enable;
  319. }
  320. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  321. if (ret) {
  322. dev_err(&pdev->dev, "pci_request_regions FAILED-%d", ret);
  323. goto err_request_regions;
  324. }
  325. chip->base = pci_iomap(pdev, 1, 0);
  326. if (!chip->base) {
  327. dev_err(&pdev->dev, "%s : pci_iomap FAILED", __func__);
  328. ret = -ENOMEM;
  329. goto err_iomap;
  330. }
  331. if (pdev->device == 0x8803)
  332. chip->ioh = INTEL_EG20T_PCH;
  333. else if (pdev->device == 0x8014)
  334. chip->ioh = OKISEMI_ML7223m_IOH;
  335. else if (pdev->device == 0x8043)
  336. chip->ioh = OKISEMI_ML7223n_IOH;
  337. chip->reg = chip->base;
  338. pci_set_drvdata(pdev, chip);
  339. mutex_init(&chip->lock);
  340. spin_lock_init(&chip->spinlock);
  341. pch_gpio_setup(chip);
  342. ret = gpiochip_add(&chip->gpio);
  343. if (ret) {
  344. dev_err(&pdev->dev, "PCH gpio: Failed to register GPIO\n");
  345. goto err_gpiochip_add;
  346. }
  347. irq_base = irq_alloc_descs(-1, 0, gpio_pins[chip->ioh], NUMA_NO_NODE);
  348. if (irq_base < 0) {
  349. dev_warn(&pdev->dev, "PCH gpio: Failed to get IRQ base num\n");
  350. chip->irq_base = -1;
  351. goto end;
  352. }
  353. chip->irq_base = irq_base;
  354. /* Mask all interrupts, but enable them */
  355. msk = (1 << gpio_pins[chip->ioh]) - 1;
  356. iowrite32(msk, &chip->reg->imask);
  357. iowrite32(msk, &chip->reg->ien);
  358. ret = request_irq(pdev->irq, pch_gpio_handler,
  359. IRQF_SHARED, KBUILD_MODNAME, chip);
  360. if (ret != 0) {
  361. dev_err(&pdev->dev,
  362. "%s request_irq failed\n", __func__);
  363. goto err_request_irq;
  364. }
  365. pch_gpio_alloc_generic_chip(chip, irq_base, gpio_pins[chip->ioh]);
  366. end:
  367. return 0;
  368. err_request_irq:
  369. irq_free_descs(irq_base, gpio_pins[chip->ioh]);
  370. ret = gpiochip_remove(&chip->gpio);
  371. if (ret)
  372. dev_err(&pdev->dev, "%s gpiochip_remove failed\n", __func__);
  373. err_gpiochip_add:
  374. pci_iounmap(pdev, chip->base);
  375. err_iomap:
  376. pci_release_regions(pdev);
  377. err_request_regions:
  378. pci_disable_device(pdev);
  379. err_pci_enable:
  380. kfree(chip);
  381. dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
  382. return ret;
  383. }
  384. static void __devexit pch_gpio_remove(struct pci_dev *pdev)
  385. {
  386. int err;
  387. struct pch_gpio *chip = pci_get_drvdata(pdev);
  388. if (chip->irq_base != -1) {
  389. free_irq(pdev->irq, chip);
  390. irq_free_descs(chip->irq_base, gpio_pins[chip->ioh]);
  391. }
  392. err = gpiochip_remove(&chip->gpio);
  393. if (err)
  394. dev_err(&pdev->dev, "Failed gpiochip_remove\n");
  395. pci_iounmap(pdev, chip->base);
  396. pci_release_regions(pdev);
  397. pci_disable_device(pdev);
  398. kfree(chip);
  399. }
  400. #ifdef CONFIG_PM
  401. static int pch_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
  402. {
  403. s32 ret;
  404. struct pch_gpio *chip = pci_get_drvdata(pdev);
  405. unsigned long flags;
  406. spin_lock_irqsave(&chip->spinlock, flags);
  407. pch_gpio_save_reg_conf(chip);
  408. spin_unlock_irqrestore(&chip->spinlock, flags);
  409. ret = pci_save_state(pdev);
  410. if (ret) {
  411. dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
  412. return ret;
  413. }
  414. pci_disable_device(pdev);
  415. pci_set_power_state(pdev, PCI_D0);
  416. ret = pci_enable_wake(pdev, PCI_D0, 1);
  417. if (ret)
  418. dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
  419. return 0;
  420. }
  421. static int pch_gpio_resume(struct pci_dev *pdev)
  422. {
  423. s32 ret;
  424. struct pch_gpio *chip = pci_get_drvdata(pdev);
  425. unsigned long flags;
  426. ret = pci_enable_wake(pdev, PCI_D0, 0);
  427. pci_set_power_state(pdev, PCI_D0);
  428. ret = pci_enable_device(pdev);
  429. if (ret) {
  430. dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
  431. return ret;
  432. }
  433. pci_restore_state(pdev);
  434. spin_lock_irqsave(&chip->spinlock, flags);
  435. iowrite32(0x01, &chip->reg->reset);
  436. iowrite32(0x00, &chip->reg->reset);
  437. pch_gpio_restore_reg_conf(chip);
  438. spin_unlock_irqrestore(&chip->spinlock, flags);
  439. return 0;
  440. }
  441. #else
  442. #define pch_gpio_suspend NULL
  443. #define pch_gpio_resume NULL
  444. #endif
  445. #define PCI_VENDOR_ID_ROHM 0x10DB
  446. static DEFINE_PCI_DEVICE_TABLE(pch_gpio_pcidev_id) = {
  447. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8803) },
  448. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8014) },
  449. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8043) },
  450. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8803) },
  451. { 0, }
  452. };
  453. MODULE_DEVICE_TABLE(pci, pch_gpio_pcidev_id);
  454. static struct pci_driver pch_gpio_driver = {
  455. .name = "pch_gpio",
  456. .id_table = pch_gpio_pcidev_id,
  457. .probe = pch_gpio_probe,
  458. .remove = __devexit_p(pch_gpio_remove),
  459. .suspend = pch_gpio_suspend,
  460. .resume = pch_gpio_resume
  461. };
  462. static int __init pch_gpio_pci_init(void)
  463. {
  464. return pci_register_driver(&pch_gpio_driver);
  465. }
  466. module_init(pch_gpio_pci_init);
  467. static void __exit pch_gpio_pci_exit(void)
  468. {
  469. pci_unregister_driver(&pch_gpio_driver);
  470. }
  471. module_exit(pch_gpio_pci_exit);
  472. MODULE_DESCRIPTION("PCH GPIO PCI Driver");
  473. MODULE_LICENSE("GPL");