gpio-pch.c 14 KB

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