gpio-pch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 = container_of(gpio, struct pch_gpio, 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 = container_of(gpio, struct pch_gpio, gpio);
  118. return ioread32(&chip->reg->pi) & (1 << nr);
  119. }
  120. static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  121. int val)
  122. {
  123. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, 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 = container_of(gpio, struct pch_gpio, 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 = container_of(gpio, struct pch_gpio, 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->dev = 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. ret = gpiochip_add(&chip->gpio);
  346. if (ret) {
  347. dev_err(&pdev->dev, "PCH gpio: Failed to register GPIO\n");
  348. goto err_gpiochip_add;
  349. }
  350. irq_base = irq_alloc_descs(-1, 0, gpio_pins[chip->ioh], NUMA_NO_NODE);
  351. if (irq_base < 0) {
  352. dev_warn(&pdev->dev, "PCH gpio: Failed to get IRQ base num\n");
  353. chip->irq_base = -1;
  354. goto end;
  355. }
  356. chip->irq_base = irq_base;
  357. /* Mask all interrupts, but enable them */
  358. msk = (1 << gpio_pins[chip->ioh]) - 1;
  359. iowrite32(msk, &chip->reg->imask);
  360. iowrite32(msk, &chip->reg->ien);
  361. ret = request_irq(pdev->irq, pch_gpio_handler,
  362. IRQF_SHARED, KBUILD_MODNAME, chip);
  363. if (ret != 0) {
  364. dev_err(&pdev->dev,
  365. "%s request_irq failed\n", __func__);
  366. goto err_request_irq;
  367. }
  368. pch_gpio_alloc_generic_chip(chip, irq_base, gpio_pins[chip->ioh]);
  369. end:
  370. return 0;
  371. err_request_irq:
  372. irq_free_descs(irq_base, gpio_pins[chip->ioh]);
  373. gpiochip_remove(&chip->gpio);
  374. err_gpiochip_add:
  375. pci_iounmap(pdev, chip->base);
  376. err_iomap:
  377. pci_release_regions(pdev);
  378. err_request_regions:
  379. pci_disable_device(pdev);
  380. err_pci_enable:
  381. kfree(chip);
  382. dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
  383. return ret;
  384. }
  385. static void pch_gpio_remove(struct pci_dev *pdev)
  386. {
  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. gpiochip_remove(&chip->gpio);
  393. pci_iounmap(pdev, chip->base);
  394. pci_release_regions(pdev);
  395. pci_disable_device(pdev);
  396. kfree(chip);
  397. }
  398. #ifdef CONFIG_PM
  399. static int pch_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
  400. {
  401. s32 ret;
  402. struct pch_gpio *chip = pci_get_drvdata(pdev);
  403. unsigned long flags;
  404. spin_lock_irqsave(&chip->spinlock, flags);
  405. pch_gpio_save_reg_conf(chip);
  406. spin_unlock_irqrestore(&chip->spinlock, flags);
  407. ret = pci_save_state(pdev);
  408. if (ret) {
  409. dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
  410. return ret;
  411. }
  412. pci_disable_device(pdev);
  413. pci_set_power_state(pdev, PCI_D0);
  414. ret = pci_enable_wake(pdev, PCI_D0, 1);
  415. if (ret)
  416. dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
  417. return 0;
  418. }
  419. static int pch_gpio_resume(struct pci_dev *pdev)
  420. {
  421. s32 ret;
  422. struct pch_gpio *chip = pci_get_drvdata(pdev);
  423. unsigned long flags;
  424. ret = pci_enable_wake(pdev, PCI_D0, 0);
  425. pci_set_power_state(pdev, PCI_D0);
  426. ret = pci_enable_device(pdev);
  427. if (ret) {
  428. dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
  429. return ret;
  430. }
  431. pci_restore_state(pdev);
  432. spin_lock_irqsave(&chip->spinlock, flags);
  433. iowrite32(0x01, &chip->reg->reset);
  434. iowrite32(0x00, &chip->reg->reset);
  435. pch_gpio_restore_reg_conf(chip);
  436. spin_unlock_irqrestore(&chip->spinlock, flags);
  437. return 0;
  438. }
  439. #else
  440. #define pch_gpio_suspend NULL
  441. #define pch_gpio_resume NULL
  442. #endif
  443. #define PCI_VENDOR_ID_ROHM 0x10DB
  444. static const struct pci_device_id pch_gpio_pcidev_id[] = {
  445. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8803) },
  446. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8014) },
  447. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8043) },
  448. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8803) },
  449. { 0, }
  450. };
  451. MODULE_DEVICE_TABLE(pci, pch_gpio_pcidev_id);
  452. static struct pci_driver pch_gpio_driver = {
  453. .name = "pch_gpio",
  454. .id_table = pch_gpio_pcidev_id,
  455. .probe = pch_gpio_probe,
  456. .remove = pch_gpio_remove,
  457. .suspend = pch_gpio_suspend,
  458. .resume = pch_gpio_resume
  459. };
  460. module_pci_driver(pch_gpio_driver);
  461. MODULE_DESCRIPTION("PCH GPIO PCI Driver");
  462. MODULE_LICENSE("GPL");