gpio-ml-ioh.c 13 KB

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