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. u32 im;
  208. u32 __iomem *im_reg;
  209. u32 ien;
  210. u32 im_pos;
  211. int ch;
  212. unsigned long flags;
  213. u32 val;
  214. int irq = d->irq;
  215. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  216. struct pch_gpio *chip = gc->private;
  217. ch = irq - chip->irq_base;
  218. if (irq <= chip->irq_base + 7) {
  219. im_reg = &chip->reg->im0;
  220. im_pos = ch;
  221. } else {
  222. im_reg = &chip->reg->im1;
  223. im_pos = ch - 8;
  224. }
  225. dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d\n",
  226. __func__, irq, type, ch, im_pos);
  227. spin_lock_irqsave(&chip->spinlock, flags);
  228. switch (type) {
  229. case IRQ_TYPE_EDGE_RISING:
  230. val = PCH_EDGE_RISING;
  231. break;
  232. case IRQ_TYPE_EDGE_FALLING:
  233. val = PCH_EDGE_FALLING;
  234. break;
  235. case IRQ_TYPE_EDGE_BOTH:
  236. val = PCH_EDGE_BOTH;
  237. break;
  238. case IRQ_TYPE_LEVEL_HIGH:
  239. val = PCH_LEVEL_H;
  240. break;
  241. case IRQ_TYPE_LEVEL_LOW:
  242. val = PCH_LEVEL_L;
  243. break;
  244. case IRQ_TYPE_PROBE:
  245. goto end;
  246. default:
  247. dev_warn(chip->dev, "%s: unknown type(%dd)",
  248. __func__, type);
  249. goto end;
  250. }
  251. /* Set interrupt mode */
  252. im = ioread32(im_reg) & ~(PCH_IM_MASK << (im_pos * 4));
  253. iowrite32(im | (val << (im_pos * 4)), im_reg);
  254. /* iclr */
  255. iowrite32(BIT(ch), &chip->reg->iclr);
  256. /* IMASKCLR */
  257. iowrite32(BIT(ch), &chip->reg->imaskclr);
  258. /* Enable interrupt */
  259. ien = ioread32(&chip->reg->ien);
  260. iowrite32(ien | BIT(ch), &chip->reg->ien);
  261. end:
  262. spin_unlock_irqrestore(&chip->spinlock, flags);
  263. return 0;
  264. }
  265. static void pch_irq_unmask(struct irq_data *d)
  266. {
  267. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  268. struct pch_gpio *chip = gc->private;
  269. iowrite32(1 << (d->irq - chip->irq_base), &chip->reg->imaskclr);
  270. }
  271. static void pch_irq_mask(struct irq_data *d)
  272. {
  273. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  274. struct pch_gpio *chip = gc->private;
  275. iowrite32(1 << (d->irq - chip->irq_base), &chip->reg->imask);
  276. }
  277. static irqreturn_t pch_gpio_handler(int irq, void *dev_id)
  278. {
  279. struct pch_gpio *chip = dev_id;
  280. u32 reg_val = ioread32(&chip->reg->istatus);
  281. int i;
  282. int ret = IRQ_NONE;
  283. for (i = 0; i < gpio_pins[chip->ioh]; i++) {
  284. if (reg_val & BIT(i)) {
  285. dev_dbg(chip->dev, "%s:[%d]:irq=%d status=0x%x\n",
  286. __func__, i, irq, reg_val);
  287. iowrite32(BIT(i), &chip->reg->iclr);
  288. generic_handle_irq(chip->irq_base + i);
  289. ret = IRQ_HANDLED;
  290. }
  291. }
  292. return ret;
  293. }
  294. static __devinit void pch_gpio_alloc_generic_chip(struct pch_gpio *chip,
  295. unsigned int irq_start, unsigned int num)
  296. {
  297. struct irq_chip_generic *gc;
  298. struct irq_chip_type *ct;
  299. gc = irq_alloc_generic_chip("pch_gpio", 1, irq_start, chip->base,
  300. handle_simple_irq);
  301. gc->private = chip;
  302. ct = gc->chip_types;
  303. ct->chip.irq_mask = pch_irq_mask;
  304. ct->chip.irq_unmask = pch_irq_unmask;
  305. ct->chip.irq_set_type = pch_irq_type;
  306. irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
  307. IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  308. }
  309. static int __devinit pch_gpio_probe(struct pci_dev *pdev,
  310. const struct pci_device_id *id)
  311. {
  312. s32 ret;
  313. struct pch_gpio *chip;
  314. int irq_base;
  315. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  316. if (chip == NULL)
  317. return -ENOMEM;
  318. chip->dev = &pdev->dev;
  319. ret = pci_enable_device(pdev);
  320. if (ret) {
  321. dev_err(&pdev->dev, "%s : pci_enable_device FAILED", __func__);
  322. goto err_pci_enable;
  323. }
  324. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  325. if (ret) {
  326. dev_err(&pdev->dev, "pci_request_regions FAILED-%d", ret);
  327. goto err_request_regions;
  328. }
  329. chip->base = pci_iomap(pdev, 1, 0);
  330. if (!chip->base) {
  331. dev_err(&pdev->dev, "%s : pci_iomap FAILED", __func__);
  332. ret = -ENOMEM;
  333. goto err_iomap;
  334. }
  335. if (pdev->device == 0x8803)
  336. chip->ioh = INTEL_EG20T_PCH;
  337. else if (pdev->device == 0x8014)
  338. chip->ioh = OKISEMI_ML7223m_IOH;
  339. else if (pdev->device == 0x8043)
  340. chip->ioh = OKISEMI_ML7223n_IOH;
  341. chip->reg = chip->base;
  342. pci_set_drvdata(pdev, chip);
  343. mutex_init(&chip->lock);
  344. spin_lock_init(&chip->spinlock);
  345. pch_gpio_setup(chip);
  346. ret = gpiochip_add(&chip->gpio);
  347. if (ret) {
  348. dev_err(&pdev->dev, "PCH gpio: Failed to register GPIO\n");
  349. goto err_gpiochip_add;
  350. }
  351. irq_base = irq_alloc_descs(-1, 0, gpio_pins[chip->ioh], NUMA_NO_NODE);
  352. if (irq_base < 0) {
  353. dev_warn(&pdev->dev, "PCH gpio: Failed to get IRQ base num\n");
  354. chip->irq_base = -1;
  355. goto end;
  356. }
  357. chip->irq_base = irq_base;
  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. /* Initialize interrupt ien register */
  367. iowrite32(0, &chip->reg->ien);
  368. end:
  369. return 0;
  370. err_request_irq:
  371. irq_free_descs(irq_base, gpio_pins[chip->ioh]);
  372. ret = gpiochip_remove(&chip->gpio);
  373. if (ret)
  374. dev_err(&pdev->dev, "%s gpiochip_remove failed\n", __func__);
  375. err_gpiochip_add:
  376. pci_iounmap(pdev, chip->base);
  377. err_iomap:
  378. pci_release_regions(pdev);
  379. err_request_regions:
  380. pci_disable_device(pdev);
  381. err_pci_enable:
  382. kfree(chip);
  383. dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
  384. return ret;
  385. }
  386. static void __devexit pch_gpio_remove(struct pci_dev *pdev)
  387. {
  388. int err;
  389. struct pch_gpio *chip = pci_get_drvdata(pdev);
  390. if (chip->irq_base != -1) {
  391. free_irq(pdev->irq, chip);
  392. irq_free_descs(chip->irq_base, gpio_pins[chip->ioh]);
  393. }
  394. err = gpiochip_remove(&chip->gpio);
  395. if (err)
  396. dev_err(&pdev->dev, "Failed gpiochip_remove\n");
  397. pci_iounmap(pdev, chip->base);
  398. pci_release_regions(pdev);
  399. pci_disable_device(pdev);
  400. kfree(chip);
  401. }
  402. #ifdef CONFIG_PM
  403. static int pch_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
  404. {
  405. s32 ret;
  406. struct pch_gpio *chip = pci_get_drvdata(pdev);
  407. unsigned long flags;
  408. spin_lock_irqsave(&chip->spinlock, flags);
  409. pch_gpio_save_reg_conf(chip);
  410. spin_unlock_irqrestore(&chip->spinlock, flags);
  411. ret = pci_save_state(pdev);
  412. if (ret) {
  413. dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
  414. return ret;
  415. }
  416. pci_disable_device(pdev);
  417. pci_set_power_state(pdev, PCI_D0);
  418. ret = pci_enable_wake(pdev, PCI_D0, 1);
  419. if (ret)
  420. dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
  421. return 0;
  422. }
  423. static int pch_gpio_resume(struct pci_dev *pdev)
  424. {
  425. s32 ret;
  426. struct pch_gpio *chip = pci_get_drvdata(pdev);
  427. unsigned long flags;
  428. ret = pci_enable_wake(pdev, PCI_D0, 0);
  429. pci_set_power_state(pdev, PCI_D0);
  430. ret = pci_enable_device(pdev);
  431. if (ret) {
  432. dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
  433. return ret;
  434. }
  435. pci_restore_state(pdev);
  436. spin_lock_irqsave(&chip->spinlock, flags);
  437. iowrite32(0x01, &chip->reg->reset);
  438. iowrite32(0x00, &chip->reg->reset);
  439. pch_gpio_restore_reg_conf(chip);
  440. spin_unlock_irqrestore(&chip->spinlock, flags);
  441. return 0;
  442. }
  443. #else
  444. #define pch_gpio_suspend NULL
  445. #define pch_gpio_resume NULL
  446. #endif
  447. #define PCI_VENDOR_ID_ROHM 0x10DB
  448. static DEFINE_PCI_DEVICE_TABLE(pch_gpio_pcidev_id) = {
  449. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8803) },
  450. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8014) },
  451. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8043) },
  452. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8803) },
  453. { 0, }
  454. };
  455. MODULE_DEVICE_TABLE(pci, pch_gpio_pcidev_id);
  456. static struct pci_driver pch_gpio_driver = {
  457. .name = "pch_gpio",
  458. .id_table = pch_gpio_pcidev_id,
  459. .probe = pch_gpio_probe,
  460. .remove = __devexit_p(pch_gpio_remove),
  461. .suspend = pch_gpio_suspend,
  462. .resume = pch_gpio_resume
  463. };
  464. static int __init pch_gpio_pci_init(void)
  465. {
  466. return pci_register_driver(&pch_gpio_driver);
  467. }
  468. module_init(pch_gpio_pci_init);
  469. static void __exit pch_gpio_pci_exit(void)
  470. {
  471. pci_unregister_driver(&pch_gpio_driver);
  472. }
  473. module_exit(pch_gpio_pci_exit);
  474. MODULE_DESCRIPTION("PCH GPIO PCI Driver");
  475. MODULE_LICENSE("GPL");