gpio-ml-ioh.c 14 KB

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