gpio-ml-ioh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/pci.h>
  21. #include <linux/gpio.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #define IOH_EDGE_FALLING 0
  25. #define IOH_EDGE_RISING BIT(0)
  26. #define IOH_LEVEL_L BIT(1)
  27. #define IOH_LEVEL_H (BIT(0) | BIT(1))
  28. #define IOH_EDGE_BOTH BIT(2)
  29. #define IOH_IM_MASK (BIT(0) | BIT(1) | BIT(2))
  30. #define IOH_IRQ_BASE 0
  31. #define PCI_VENDOR_ID_ROHM 0x10DB
  32. struct ioh_reg_comn {
  33. u32 ien;
  34. u32 istatus;
  35. u32 idisp;
  36. u32 iclr;
  37. u32 imask;
  38. u32 imaskclr;
  39. u32 po;
  40. u32 pi;
  41. u32 pm;
  42. u32 im_0;
  43. u32 im_1;
  44. u32 reserved;
  45. };
  46. struct ioh_regs {
  47. struct ioh_reg_comn regs[8];
  48. u32 reserve1[16];
  49. u32 ioh_sel_reg[4];
  50. u32 reserve2[11];
  51. u32 srst;
  52. };
  53. /**
  54. * struct ioh_gpio_reg_data - The register store data.
  55. * @ien_reg To store contents of interrupt enable register.
  56. * @imask_reg: To store contents of interrupt mask regist
  57. * @po_reg: To store contents of PO register.
  58. * @pm_reg: To store contents of PM register.
  59. * @im0_reg: To store contents of interrupt mode regist0
  60. * @im1_reg: To store contents of interrupt mode regist1
  61. * @use_sel_reg: To store contents of GPIO_USE_SEL0~3
  62. */
  63. struct ioh_gpio_reg_data {
  64. u32 ien_reg;
  65. u32 imask_reg;
  66. u32 po_reg;
  67. u32 pm_reg;
  68. u32 im0_reg;
  69. u32 im1_reg;
  70. u32 use_sel_reg;
  71. };
  72. /**
  73. * struct ioh_gpio - GPIO private data structure.
  74. * @base: PCI base address of Memory mapped I/O register.
  75. * @reg: Memory mapped IOH GPIO register list.
  76. * @dev: Pointer to device structure.
  77. * @gpio: Data for GPIO infrastructure.
  78. * @ioh_gpio_reg: Memory mapped Register data is saved here
  79. * when suspend.
  80. * @gpio_use_sel: Save GPIO_USE_SEL1~4 register for PM
  81. * @ch: Indicate GPIO channel
  82. * @irq_base: Save base of IRQ number for interrupt
  83. * @spinlock: Used for register access protection in
  84. * interrupt context ioh_irq_type and PM;
  85. */
  86. struct ioh_gpio {
  87. void __iomem *base;
  88. struct ioh_regs __iomem *reg;
  89. struct device *dev;
  90. struct gpio_chip gpio;
  91. struct ioh_gpio_reg_data ioh_gpio_reg;
  92. u32 gpio_use_sel;
  93. struct mutex lock;
  94. int ch;
  95. int irq_base;
  96. spinlock_t spinlock;
  97. };
  98. static const int num_ports[] = {6, 12, 16, 16, 15, 16, 16, 12};
  99. static void ioh_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
  100. {
  101. u32 reg_val;
  102. struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
  103. mutex_lock(&chip->lock);
  104. reg_val = ioread32(&chip->reg->regs[chip->ch].po);
  105. if (val)
  106. reg_val |= (1 << nr);
  107. else
  108. reg_val &= ~(1 << nr);
  109. iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
  110. mutex_unlock(&chip->lock);
  111. }
  112. static int ioh_gpio_get(struct gpio_chip *gpio, unsigned nr)
  113. {
  114. struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
  115. return ioread32(&chip->reg->regs[chip->ch].pi) & (1 << nr);
  116. }
  117. static int ioh_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
  118. int val)
  119. {
  120. struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
  121. u32 pm;
  122. u32 reg_val;
  123. mutex_lock(&chip->lock);
  124. pm = ioread32(&chip->reg->regs[chip->ch].pm) &
  125. ((1 << num_ports[chip->ch]) - 1);
  126. pm |= (1 << nr);
  127. iowrite32(pm, &chip->reg->regs[chip->ch].pm);
  128. reg_val = ioread32(&chip->reg->regs[chip->ch].po);
  129. if (val)
  130. reg_val |= (1 << nr);
  131. else
  132. reg_val &= ~(1 << nr);
  133. iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
  134. mutex_unlock(&chip->lock);
  135. return 0;
  136. }
  137. static int ioh_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  138. {
  139. struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
  140. u32 pm;
  141. mutex_lock(&chip->lock);
  142. pm = ioread32(&chip->reg->regs[chip->ch].pm) &
  143. ((1 << num_ports[chip->ch]) - 1);
  144. pm &= ~(1 << nr);
  145. iowrite32(pm, &chip->reg->regs[chip->ch].pm);
  146. mutex_unlock(&chip->lock);
  147. return 0;
  148. }
  149. #ifdef CONFIG_PM
  150. /*
  151. * Save register configuration and disable interrupts.
  152. */
  153. static void ioh_gpio_save_reg_conf(struct ioh_gpio *chip)
  154. {
  155. int i;
  156. for (i = 0; i < 8; i ++, chip++) {
  157. chip->ioh_gpio_reg.po_reg =
  158. ioread32(&chip->reg->regs[chip->ch].po);
  159. chip->ioh_gpio_reg.pm_reg =
  160. ioread32(&chip->reg->regs[chip->ch].pm);
  161. chip->ioh_gpio_reg.ien_reg =
  162. ioread32(&chip->reg->regs[chip->ch].ien);
  163. chip->ioh_gpio_reg.imask_reg =
  164. ioread32(&chip->reg->regs[chip->ch].imask);
  165. chip->ioh_gpio_reg.im0_reg =
  166. ioread32(&chip->reg->regs[chip->ch].im_0);
  167. chip->ioh_gpio_reg.im1_reg =
  168. ioread32(&chip->reg->regs[chip->ch].im_1);
  169. if (i < 4)
  170. chip->ioh_gpio_reg.use_sel_reg =
  171. ioread32(&chip->reg->ioh_sel_reg[i]);
  172. }
  173. }
  174. /*
  175. * This function restores the register configuration of the GPIO device.
  176. */
  177. static void ioh_gpio_restore_reg_conf(struct ioh_gpio *chip)
  178. {
  179. int i;
  180. for (i = 0; i < 8; i ++, chip++) {
  181. iowrite32(chip->ioh_gpio_reg.po_reg,
  182. &chip->reg->regs[chip->ch].po);
  183. iowrite32(chip->ioh_gpio_reg.pm_reg,
  184. &chip->reg->regs[chip->ch].pm);
  185. iowrite32(chip->ioh_gpio_reg.ien_reg,
  186. &chip->reg->regs[chip->ch].ien);
  187. iowrite32(chip->ioh_gpio_reg.imask_reg,
  188. &chip->reg->regs[chip->ch].imask);
  189. iowrite32(chip->ioh_gpio_reg.im0_reg,
  190. &chip->reg->regs[chip->ch].im_0);
  191. iowrite32(chip->ioh_gpio_reg.im1_reg,
  192. &chip->reg->regs[chip->ch].im_1);
  193. if (i < 4)
  194. iowrite32(chip->ioh_gpio_reg.use_sel_reg,
  195. &chip->reg->ioh_sel_reg[i]);
  196. }
  197. }
  198. #endif
  199. static int ioh_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
  200. {
  201. struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
  202. return chip->irq_base + offset;
  203. }
  204. static void ioh_gpio_setup(struct ioh_gpio *chip, int num_port)
  205. {
  206. struct gpio_chip *gpio = &chip->gpio;
  207. gpio->label = dev_name(chip->dev);
  208. gpio->owner = THIS_MODULE;
  209. gpio->direction_input = ioh_gpio_direction_input;
  210. gpio->get = ioh_gpio_get;
  211. gpio->direction_output = ioh_gpio_direction_output;
  212. gpio->set = ioh_gpio_set;
  213. gpio->dbg_show = NULL;
  214. gpio->base = -1;
  215. gpio->ngpio = num_port;
  216. gpio->can_sleep = 0;
  217. gpio->to_irq = ioh_gpio_to_irq;
  218. }
  219. static int ioh_irq_type(struct irq_data *d, unsigned int type)
  220. {
  221. u32 im;
  222. u32 *im_reg;
  223. u32 ien;
  224. u32 im_pos;
  225. int ch;
  226. unsigned long flags;
  227. u32 val;
  228. int irq = d->irq;
  229. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  230. struct ioh_gpio *chip = gc->private;
  231. ch = irq - chip->irq_base;
  232. if (irq <= chip->irq_base + 7) {
  233. im_reg = &chip->reg->regs[chip->ch].im_0;
  234. im_pos = ch;
  235. } else {
  236. im_reg = &chip->reg->regs[chip->ch].im_1;
  237. im_pos = ch - 8;
  238. }
  239. dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d type=%d\n",
  240. __func__, irq, type, ch, im_pos, type);
  241. spin_lock_irqsave(&chip->spinlock, flags);
  242. switch (type) {
  243. case IRQ_TYPE_EDGE_RISING:
  244. val = IOH_EDGE_RISING;
  245. break;
  246. case IRQ_TYPE_EDGE_FALLING:
  247. val = IOH_EDGE_FALLING;
  248. break;
  249. case IRQ_TYPE_EDGE_BOTH:
  250. val = IOH_EDGE_BOTH;
  251. break;
  252. case IRQ_TYPE_LEVEL_HIGH:
  253. val = IOH_LEVEL_H;
  254. break;
  255. case IRQ_TYPE_LEVEL_LOW:
  256. val = IOH_LEVEL_L;
  257. break;
  258. case IRQ_TYPE_PROBE:
  259. goto end;
  260. default:
  261. dev_warn(chip->dev, "%s: unknown type(%dd)",
  262. __func__, type);
  263. goto end;
  264. }
  265. /* Set interrupt mode */
  266. im = ioread32(im_reg) & ~(IOH_IM_MASK << (im_pos * 4));
  267. iowrite32(im | (val << (im_pos * 4)), im_reg);
  268. /* iclr */
  269. iowrite32(BIT(ch), &chip->reg->regs[chip->ch].iclr);
  270. /* IMASKCLR */
  271. iowrite32(BIT(ch), &chip->reg->regs[chip->ch].imaskclr);
  272. /* Enable interrupt */
  273. ien = ioread32(&chip->reg->regs[chip->ch].ien);
  274. iowrite32(ien | BIT(ch), &chip->reg->regs[chip->ch].ien);
  275. end:
  276. spin_unlock_irqrestore(&chip->spinlock, flags);
  277. return 0;
  278. }
  279. static void ioh_irq_unmask(struct irq_data *d)
  280. {
  281. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  282. struct ioh_gpio *chip = gc->private;
  283. iowrite32(1 << (d->irq - chip->irq_base),
  284. &chip->reg->regs[chip->ch].imaskclr);
  285. }
  286. static void ioh_irq_mask(struct irq_data *d)
  287. {
  288. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  289. struct ioh_gpio *chip = gc->private;
  290. iowrite32(1 << (d->irq - chip->irq_base),
  291. &chip->reg->regs[chip->ch].imask);
  292. }
  293. static irqreturn_t ioh_gpio_handler(int irq, void *dev_id)
  294. {
  295. struct ioh_gpio *chip = dev_id;
  296. u32 reg_val;
  297. int i, j;
  298. int ret = IRQ_NONE;
  299. for (i = 0; i < 8; i++) {
  300. reg_val = ioread32(&chip->reg->regs[i].istatus);
  301. for (j = 0; j < num_ports[i]; j++) {
  302. if (reg_val & BIT(j)) {
  303. dev_dbg(chip->dev,
  304. "%s:[%d]:irq=%d status=0x%x\n",
  305. __func__, j, irq, reg_val);
  306. iowrite32(BIT(j),
  307. &chip->reg->regs[chip->ch].iclr);
  308. generic_handle_irq(chip->irq_base + j);
  309. ret = IRQ_HANDLED;
  310. }
  311. }
  312. }
  313. return ret;
  314. }
  315. static __devinit void ioh_gpio_alloc_generic_chip(struct ioh_gpio *chip,
  316. unsigned int irq_start, unsigned int num)
  317. {
  318. struct irq_chip_generic *gc;
  319. struct irq_chip_type *ct;
  320. gc = irq_alloc_generic_chip("ioh_gpio", 1, irq_start, chip->base,
  321. handle_simple_irq);
  322. gc->private = chip;
  323. ct = gc->chip_types;
  324. ct->chip.irq_mask = ioh_irq_mask;
  325. ct->chip.irq_unmask = ioh_irq_unmask;
  326. ct->chip.irq_set_type = ioh_irq_type;
  327. irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
  328. IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  329. }
  330. static int __devinit ioh_gpio_probe(struct pci_dev *pdev,
  331. const struct pci_device_id *id)
  332. {
  333. int ret;
  334. int i, j;
  335. struct ioh_gpio *chip;
  336. void __iomem *base;
  337. void __iomem *chip_save;
  338. int irq_base;
  339. ret = pci_enable_device(pdev);
  340. if (ret) {
  341. dev_err(&pdev->dev, "%s : pci_enable_device failed", __func__);
  342. goto err_pci_enable;
  343. }
  344. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  345. if (ret) {
  346. dev_err(&pdev->dev, "pci_request_regions failed-%d", ret);
  347. goto err_request_regions;
  348. }
  349. base = pci_iomap(pdev, 1, 0);
  350. if (base == 0) {
  351. dev_err(&pdev->dev, "%s : pci_iomap failed", __func__);
  352. ret = -ENOMEM;
  353. goto err_iomap;
  354. }
  355. chip_save = kzalloc(sizeof(*chip) * 8, GFP_KERNEL);
  356. if (chip_save == NULL) {
  357. dev_err(&pdev->dev, "%s : kzalloc failed", __func__);
  358. ret = -ENOMEM;
  359. goto err_kzalloc;
  360. }
  361. chip = chip_save;
  362. for (i = 0; i < 8; i++, chip++) {
  363. chip->dev = &pdev->dev;
  364. chip->base = base;
  365. chip->reg = chip->base;
  366. chip->ch = i;
  367. mutex_init(&chip->lock);
  368. ioh_gpio_setup(chip, num_ports[i]);
  369. ret = gpiochip_add(&chip->gpio);
  370. if (ret) {
  371. dev_err(&pdev->dev, "IOH gpio: Failed to register GPIO\n");
  372. goto err_gpiochip_add;
  373. }
  374. }
  375. chip = chip_save;
  376. for (j = 0; j < 8; j++, chip++) {
  377. irq_base = irq_alloc_descs(-1, IOH_IRQ_BASE, num_ports[j],
  378. NUMA_NO_NODE);
  379. if (irq_base < 0) {
  380. dev_warn(&pdev->dev,
  381. "ml_ioh_gpio: Failed to get IRQ base num\n");
  382. chip->irq_base = -1;
  383. goto err_irq_alloc_descs;
  384. }
  385. chip->irq_base = irq_base;
  386. ioh_gpio_alloc_generic_chip(chip, irq_base, num_ports[j]);
  387. }
  388. chip = chip_save;
  389. ret = request_irq(pdev->irq, ioh_gpio_handler,
  390. IRQF_SHARED, KBUILD_MODNAME, chip);
  391. if (ret != 0) {
  392. dev_err(&pdev->dev,
  393. "%s request_irq failed\n", __func__);
  394. goto err_request_irq;
  395. }
  396. pci_set_drvdata(pdev, chip);
  397. return 0;
  398. err_request_irq:
  399. chip = chip_save;
  400. err_irq_alloc_descs:
  401. while (--j >= 0) {
  402. chip--;
  403. irq_free_descs(chip->irq_base, num_ports[j]);
  404. }
  405. chip = chip_save;
  406. err_gpiochip_add:
  407. while (--i >= 0) {
  408. chip--;
  409. ret = gpiochip_remove(&chip->gpio);
  410. if (ret)
  411. dev_err(&pdev->dev, "Failed gpiochip_remove(%d)\n", i);
  412. }
  413. kfree(chip_save);
  414. err_kzalloc:
  415. pci_iounmap(pdev, base);
  416. err_iomap:
  417. pci_release_regions(pdev);
  418. err_request_regions:
  419. pci_disable_device(pdev);
  420. err_pci_enable:
  421. dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
  422. return ret;
  423. }
  424. static void __devexit ioh_gpio_remove(struct pci_dev *pdev)
  425. {
  426. int err;
  427. int i;
  428. struct ioh_gpio *chip = pci_get_drvdata(pdev);
  429. void __iomem *chip_save;
  430. chip_save = chip;
  431. free_irq(pdev->irq, chip);
  432. for (i = 0; i < 8; i++, chip++) {
  433. irq_free_descs(chip->irq_base, num_ports[i]);
  434. err = gpiochip_remove(&chip->gpio);
  435. if (err)
  436. dev_err(&pdev->dev, "Failed gpiochip_remove\n");
  437. }
  438. chip = chip_save;
  439. pci_iounmap(pdev, chip->base);
  440. pci_release_regions(pdev);
  441. pci_disable_device(pdev);
  442. kfree(chip);
  443. }
  444. #ifdef CONFIG_PM
  445. static int ioh_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
  446. {
  447. s32 ret;
  448. struct ioh_gpio *chip = pci_get_drvdata(pdev);
  449. unsigned long flags;
  450. spin_lock_irqsave(&chip->spinlock, flags);
  451. ioh_gpio_save_reg_conf(chip);
  452. spin_unlock_irqrestore(&chip->spinlock, flags);
  453. ret = pci_save_state(pdev);
  454. if (ret) {
  455. dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
  456. return ret;
  457. }
  458. pci_disable_device(pdev);
  459. pci_set_power_state(pdev, PCI_D0);
  460. ret = pci_enable_wake(pdev, PCI_D0, 1);
  461. if (ret)
  462. dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
  463. return 0;
  464. }
  465. static int ioh_gpio_resume(struct pci_dev *pdev)
  466. {
  467. s32 ret;
  468. struct ioh_gpio *chip = pci_get_drvdata(pdev);
  469. unsigned long flags;
  470. ret = pci_enable_wake(pdev, PCI_D0, 0);
  471. pci_set_power_state(pdev, PCI_D0);
  472. ret = pci_enable_device(pdev);
  473. if (ret) {
  474. dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
  475. return ret;
  476. }
  477. pci_restore_state(pdev);
  478. spin_lock_irqsave(&chip->spinlock, flags);
  479. iowrite32(0x01, &chip->reg->srst);
  480. iowrite32(0x00, &chip->reg->srst);
  481. ioh_gpio_restore_reg_conf(chip);
  482. spin_unlock_irqrestore(&chip->spinlock, flags);
  483. return 0;
  484. }
  485. #else
  486. #define ioh_gpio_suspend NULL
  487. #define ioh_gpio_resume NULL
  488. #endif
  489. static DEFINE_PCI_DEVICE_TABLE(ioh_gpio_pcidev_id) = {
  490. { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x802E) },
  491. { 0, }
  492. };
  493. MODULE_DEVICE_TABLE(pci, ioh_gpio_pcidev_id);
  494. static struct pci_driver ioh_gpio_driver = {
  495. .name = "ml_ioh_gpio",
  496. .id_table = ioh_gpio_pcidev_id,
  497. .probe = ioh_gpio_probe,
  498. .remove = __devexit_p(ioh_gpio_remove),
  499. .suspend = ioh_gpio_suspend,
  500. .resume = ioh_gpio_resume
  501. };
  502. static int __init ioh_gpio_pci_init(void)
  503. {
  504. return pci_register_driver(&ioh_gpio_driver);
  505. }
  506. module_init(ioh_gpio_pci_init);
  507. static void __exit ioh_gpio_pci_exit(void)
  508. {
  509. pci_unregister_driver(&ioh_gpio_driver);
  510. }
  511. module_exit(ioh_gpio_pci_exit);
  512. MODULE_DESCRIPTION("OKI SEMICONDUCTOR ML-IOH series GPIO Driver");
  513. MODULE_LICENSE("GPL");