gpio-pch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #define PCH_EDGE_FALLING 0
  23. #define PCH_EDGE_RISING BIT(0)
  24. #define PCH_LEVEL_L BIT(1)
  25. #define PCH_LEVEL_H (BIT(0) | BIT(1))
  26. #define PCH_EDGE_BOTH BIT(2)
  27. #define PCH_IM_MASK (BIT(0) | BIT(1) | BIT(2))
  28. #define PCH_IRQ_BASE 24
  29. struct pch_regs {
  30. u32 ien;
  31. u32 istatus;
  32. u32 idisp;
  33. u32 iclr;
  34. u32 imask;
  35. u32 imaskclr;
  36. u32 po;
  37. u32 pi;
  38. u32 pm;
  39. u32 im0;
  40. u32 im1;
  41. u32 reserved[3];
  42. u32 gpio_use_sel;
  43. u32 reset;
  44. };
  45. enum pch_type_t {
  46. INTEL_EG20T_PCH,
  47. OKISEMI_ML7223m_IOH, /* OKISEMI ML7223 IOH PCIe Bus-m */
  48. OKISEMI_ML7223n_IOH /* OKISEMI ML7223 IOH PCIe Bus-n */
  49. };
  50. /* Specifies number of GPIO PINS */
  51. static int gpio_pins[] = {
  52. [INTEL_EG20T_PCH] = 12,
  53. [OKISEMI_ML7223m_IOH] = 8,
  54. [OKISEMI_ML7223n_IOH] = 8,
  55. };
  56. /**
  57. * struct pch_gpio_reg_data - The register store data.
  58. * @ien_reg: To store contents of IEN register.
  59. * @imask_reg: To store contents of IMASK register.
  60. * @po_reg: To store contents of PO register.
  61. * @pm_reg: To store contents of PM register.
  62. * @im0_reg: To store contents of IM0 register.
  63. * @im1_reg: To store contents of IM1 register.
  64. * @gpio_use_sel_reg : To store contents of GPIO_USE_SEL register.
  65. * (Only ML7223 Bus-n)
  66. */
  67. struct pch_gpio_reg_data {
  68. u32 ien_reg;
  69. u32 imask_reg;
  70. u32 po_reg;
  71. u32 pm_reg;
  72. u32 im0_reg;
  73. u32 im1_reg;
  74. u32 gpio_use_sel_reg;
  75. };
  76. /**
  77. * struct pch_gpio - GPIO private data structure.
  78. * @base: PCI base address of Memory mapped I/O register.
  79. * @reg: Memory mapped PCH GPIO register list.
  80. * @dev: Pointer to device structure.
  81. * @gpio: Data for GPIO infrastructure.
  82. * @pch_gpio_reg: Memory mapped Register data is saved here
  83. * when suspend.
  84. * @lock: Used for register access protection
  85. * @irq_base: Save base of IRQ number for interrupt
  86. * @ioh: IOH ID
  87. * @spinlock: Used for register access protection in
  88. * interrupt context pch_irq_mask,
  89. * pch_irq_unmask and pch_irq_type;
  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. struct mutex lock;
  98. int irq_base;
  99. enum pch_type_t ioh;
  100. spinlock_t spinlock;
  101. };
  102. static void pch_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
  103. {
  104. u32 reg_val;
  105. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  106. mutex_lock(&chip->lock);
  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. mutex_unlock(&chip->lock);
  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. mutex_lock(&chip->lock);
  127. pm = ioread32(&chip->reg->pm) & ((1 << gpio_pins[chip->ioh]) - 1);
  128. pm |= (1 << nr);
  129. iowrite32(pm, &chip->reg->pm);
  130. reg_val = ioread32(&chip->reg->po);
  131. if (val)
  132. reg_val |= (1 << nr);
  133. else
  134. reg_val &= ~(1 << nr);
  135. iowrite32(reg_val, &chip->reg->po);
  136. mutex_unlock(&chip->lock);
  137. return 0;
  138. }
  139. static int pch_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  140. {
  141. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  142. u32 pm;
  143. mutex_lock(&chip->lock);
  144. pm = ioread32(&chip->reg->pm) & ((1 << gpio_pins[chip->ioh]) - 1);
  145. pm &= ~(1 << nr);
  146. iowrite32(pm, &chip->reg->pm);
  147. mutex_unlock(&chip->lock);
  148. return 0;
  149. }
  150. /*
  151. * Save register configuration and disable interrupts.
  152. */
  153. static void pch_gpio_save_reg_conf(struct pch_gpio *chip)
  154. {
  155. chip->pch_gpio_reg.ien_reg = ioread32(&chip->reg->ien);
  156. chip->pch_gpio_reg.imask_reg = ioread32(&chip->reg->imask);
  157. chip->pch_gpio_reg.po_reg = ioread32(&chip->reg->po);
  158. chip->pch_gpio_reg.pm_reg = ioread32(&chip->reg->pm);
  159. chip->pch_gpio_reg.im0_reg = ioread32(&chip->reg->im0);
  160. if (chip->ioh == INTEL_EG20T_PCH)
  161. chip->pch_gpio_reg.im1_reg = ioread32(&chip->reg->im1);
  162. if (chip->ioh == OKISEMI_ML7223n_IOH)
  163. chip->pch_gpio_reg.gpio_use_sel_reg =\
  164. ioread32(&chip->reg->gpio_use_sel);
  165. }
  166. /*
  167. * This function restores the register configuration of the GPIO device.
  168. */
  169. static void pch_gpio_restore_reg_conf(struct pch_gpio *chip)
  170. {
  171. iowrite32(chip->pch_gpio_reg.ien_reg, &chip->reg->ien);
  172. iowrite32(chip->pch_gpio_reg.imask_reg, &chip->reg->imask);
  173. /* to store contents of PO register */
  174. iowrite32(chip->pch_gpio_reg.po_reg, &chip->reg->po);
  175. /* to store contents of PM register */
  176. iowrite32(chip->pch_gpio_reg.pm_reg, &chip->reg->pm);
  177. iowrite32(chip->pch_gpio_reg.im0_reg, &chip->reg->im0);
  178. if (chip->ioh == INTEL_EG20T_PCH)
  179. iowrite32(chip->pch_gpio_reg.im1_reg, &chip->reg->im1);
  180. if (chip->ioh == OKISEMI_ML7223n_IOH)
  181. iowrite32(chip->pch_gpio_reg.gpio_use_sel_reg,
  182. &chip->reg->gpio_use_sel);
  183. }
  184. static int pch_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
  185. {
  186. struct pch_gpio *chip = container_of(gpio, struct pch_gpio, gpio);
  187. return chip->irq_base + offset;
  188. }
  189. static void pch_gpio_setup(struct pch_gpio *chip)
  190. {
  191. struct gpio_chip *gpio = &chip->gpio;
  192. gpio->label = dev_name(chip->dev);
  193. gpio->owner = THIS_MODULE;
  194. gpio->direction_input = pch_gpio_direction_input;
  195. gpio->get = pch_gpio_get;
  196. gpio->direction_output = pch_gpio_direction_output;
  197. gpio->set = pch_gpio_set;
  198. gpio->dbg_show = NULL;
  199. gpio->base = -1;
  200. gpio->ngpio = gpio_pins[chip->ioh];
  201. gpio->can_sleep = 0;
  202. gpio->to_irq = pch_gpio_to_irq;
  203. }
  204. static int pch_irq_type(struct irq_data *d, unsigned int type)
  205. {
  206. u32 im;
  207. u32 *im_reg;
  208. u32 ien;
  209. u32 im_pos;
  210. int ch;
  211. unsigned long flags;
  212. u32 val;
  213. int irq = d->irq;
  214. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  215. struct pch_gpio *chip = gc->private;
  216. ch = irq - chip->irq_base;
  217. if (irq <= chip->irq_base + 7) {
  218. im_reg = &chip->reg->im0;
  219. im_pos = ch;
  220. } else {
  221. im_reg = &chip->reg->im1;
  222. im_pos = ch - 8;
  223. }
  224. dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d\n",
  225. __func__, irq, type, ch, im_pos);
  226. spin_lock_irqsave(&chip->spinlock, flags);
  227. switch (type) {
  228. case IRQ_TYPE_EDGE_RISING:
  229. val = PCH_EDGE_RISING;
  230. break;
  231. case IRQ_TYPE_EDGE_FALLING:
  232. val = PCH_EDGE_FALLING;
  233. break;
  234. case IRQ_TYPE_EDGE_BOTH:
  235. val = PCH_EDGE_BOTH;
  236. break;
  237. case IRQ_TYPE_LEVEL_HIGH:
  238. val = PCH_LEVEL_H;
  239. break;
  240. case IRQ_TYPE_LEVEL_LOW:
  241. val = PCH_LEVEL_L;
  242. break;
  243. case IRQ_TYPE_PROBE:
  244. goto end;
  245. default:
  246. dev_warn(chip->dev, "%s: unknown type(%dd)",
  247. __func__, type);
  248. goto end;
  249. }
  250. /* Set interrupt mode */
  251. im = ioread32(im_reg) & ~(PCH_IM_MASK << (im_pos * 4));
  252. iowrite32(im | (val << (im_pos * 4)), im_reg);
  253. /* iclr */
  254. iowrite32(BIT(ch), &chip->reg->iclr);
  255. /* IMASKCLR */
  256. iowrite32(BIT(ch), &chip->reg->imaskclr);
  257. /* Enable interrupt */
  258. ien = ioread32(&chip->reg->ien);
  259. iowrite32(ien | BIT(ch), &chip->reg->ien);
  260. end:
  261. spin_unlock_irqrestore(&chip->spinlock, flags);
  262. return 0;
  263. }
  264. static void pch_irq_unmask(struct irq_data *d)
  265. {
  266. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  267. struct pch_gpio *chip = gc->private;
  268. iowrite32(1 << (d->irq - chip->irq_base), &chip->reg->imaskclr);
  269. }
  270. static void pch_irq_mask(struct irq_data *d)
  271. {
  272. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  273. struct pch_gpio *chip = gc->private;
  274. iowrite32(1 << (d->irq - chip->irq_base), &chip->reg->imask);
  275. }
  276. static irqreturn_t pch_gpio_handler(int irq, void *dev_id)
  277. {
  278. struct pch_gpio *chip = dev_id;
  279. u32 reg_val = ioread32(&chip->reg->istatus);
  280. int i;
  281. int 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. iowrite32(BIT(i), &chip->reg->iclr);
  287. generic_handle_irq(chip->irq_base + i);
  288. ret = IRQ_HANDLED;
  289. }
  290. }
  291. return ret;
  292. }
  293. static __devinit void pch_gpio_alloc_generic_chip(struct pch_gpio *chip,
  294. unsigned int irq_start, unsigned int num)
  295. {
  296. struct irq_chip_generic *gc;
  297. struct irq_chip_type *ct;
  298. gc = irq_alloc_generic_chip("pch_gpio", 1, irq_start, chip->base,
  299. handle_simple_irq);
  300. gc->private = chip;
  301. ct = gc->chip_types;
  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 __devinit 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. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  315. if (chip == NULL)
  316. return -ENOMEM;
  317. chip->dev = &pdev->dev;
  318. ret = pci_enable_device(pdev);
  319. if (ret) {
  320. dev_err(&pdev->dev, "%s : pci_enable_device FAILED", __func__);
  321. goto err_pci_enable;
  322. }
  323. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  324. if (ret) {
  325. dev_err(&pdev->dev, "pci_request_regions FAILED-%d", ret);
  326. goto err_request_regions;
  327. }
  328. chip->base = pci_iomap(pdev, 1, 0);
  329. if (chip->base == 0) {
  330. dev_err(&pdev->dev, "%s : pci_iomap FAILED", __func__);
  331. ret = -ENOMEM;
  332. goto err_iomap;
  333. }
  334. if (pdev->device == 0x8803)
  335. chip->ioh = INTEL_EG20T_PCH;
  336. else if (pdev->device == 0x8014)
  337. chip->ioh = OKISEMI_ML7223m_IOH;
  338. else if (pdev->device == 0x8043)
  339. chip->ioh = OKISEMI_ML7223n_IOH;
  340. chip->reg = chip->base;
  341. pci_set_drvdata(pdev, chip);
  342. mutex_init(&chip->lock);
  343. pch_gpio_setup(chip);
  344. ret = gpiochip_add(&chip->gpio);
  345. if (ret) {
  346. dev_err(&pdev->dev, "PCH gpio: Failed to register GPIO\n");
  347. goto err_gpiochip_add;
  348. }
  349. irq_base = irq_alloc_descs(-1, 0, gpio_pins[chip->ioh], NUMA_NO_NODE);
  350. if (irq_base < 0) {
  351. dev_warn(&pdev->dev, "PCH gpio: Failed to get IRQ base num\n");
  352. chip->irq_base = -1;
  353. goto end;
  354. }
  355. chip->irq_base = irq_base;
  356. ret = request_irq(pdev->irq, pch_gpio_handler,
  357. IRQF_SHARED, KBUILD_MODNAME, chip);
  358. if (ret != 0) {
  359. dev_err(&pdev->dev,
  360. "%s request_irq failed\n", __func__);
  361. goto err_request_irq;
  362. }
  363. pch_gpio_alloc_generic_chip(chip, irq_base, gpio_pins[chip->ioh]);
  364. /* Initialize interrupt ien register */
  365. iowrite32(0, &chip->reg->ien);
  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. { 0, }
  451. };
  452. MODULE_DEVICE_TABLE(pci, pch_gpio_pcidev_id);
  453. static struct pci_driver pch_gpio_driver = {
  454. .name = "pch_gpio",
  455. .id_table = pch_gpio_pcidev_id,
  456. .probe = pch_gpio_probe,
  457. .remove = __devexit_p(pch_gpio_remove),
  458. .suspend = pch_gpio_suspend,
  459. .resume = pch_gpio_resume
  460. };
  461. static int __init pch_gpio_pci_init(void)
  462. {
  463. return pci_register_driver(&pch_gpio_driver);
  464. }
  465. module_init(pch_gpio_pci_init);
  466. static void __exit pch_gpio_pci_exit(void)
  467. {
  468. pci_unregister_driver(&pch_gpio_driver);
  469. }
  470. module_exit(pch_gpio_pci_exit);
  471. MODULE_DESCRIPTION("PCH GPIO PCI Driver");
  472. MODULE_LICENSE("GPL");