gpio-pch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  4. */
  5. #include <linux/bits.h>
  6. #include <linux/gpio/driver.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #define PCH_EDGE_FALLING 0
  14. #define PCH_EDGE_RISING 1
  15. #define PCH_LEVEL_L 2
  16. #define PCH_LEVEL_H 3
  17. #define PCH_EDGE_BOTH 4
  18. #define PCH_IM_MASK GENMASK(2, 0)
  19. #define PCH_IRQ_BASE 24
  20. struct pch_regs {
  21. u32 ien;
  22. u32 istatus;
  23. u32 idisp;
  24. u32 iclr;
  25. u32 imask;
  26. u32 imaskclr;
  27. u32 po;
  28. u32 pi;
  29. u32 pm;
  30. u32 im0;
  31. u32 im1;
  32. u32 reserved[3];
  33. u32 gpio_use_sel;
  34. u32 reset;
  35. };
  36. #define PCI_DEVICE_ID_INTEL_EG20T_PCH 0x8803
  37. #define PCI_DEVICE_ID_ROHM_ML7223m_IOH 0x8014
  38. #define PCI_DEVICE_ID_ROHM_ML7223n_IOH 0x8043
  39. #define PCI_DEVICE_ID_ROHM_EG20T_PCH 0x8803
  40. enum pch_type_t {
  41. INTEL_EG20T_PCH,
  42. OKISEMI_ML7223m_IOH, /* LAPIS Semiconductor ML7223 IOH PCIe Bus-m */
  43. OKISEMI_ML7223n_IOH /* LAPIS Semiconductor ML7223 IOH PCIe Bus-n */
  44. };
  45. /* Specifies number of GPIO PINS */
  46. static int gpio_pins[] = {
  47. [INTEL_EG20T_PCH] = 12,
  48. [OKISEMI_ML7223m_IOH] = 8,
  49. [OKISEMI_ML7223n_IOH] = 8,
  50. };
  51. /**
  52. * struct pch_gpio_reg_data - The register store data.
  53. * @ien_reg: To store contents of IEN register.
  54. * @imask_reg: To store contents of IMASK register.
  55. * @po_reg: To store contents of PO register.
  56. * @pm_reg: To store contents of PM register.
  57. * @im0_reg: To store contents of IM0 register.
  58. * @im1_reg: To store contents of IM1 register.
  59. * @gpio_use_sel_reg : To store contents of GPIO_USE_SEL register.
  60. * (Only ML7223 Bus-n)
  61. */
  62. struct pch_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 gpio_use_sel_reg;
  70. };
  71. /**
  72. * struct pch_gpio - GPIO private data structure.
  73. * @base: PCI base address of Memory mapped I/O register.
  74. * @reg: Memory mapped PCH GPIO register list.
  75. * @dev: Pointer to device structure.
  76. * @gpio: Data for GPIO infrastructure.
  77. * @pch_gpio_reg: Memory mapped Register data is saved here
  78. * when suspend.
  79. * @irq_base: Save base of IRQ number for interrupt
  80. * @ioh: IOH ID
  81. * @spinlock: Used for register access protection
  82. */
  83. struct pch_gpio {
  84. void __iomem *base;
  85. struct pch_regs __iomem *reg;
  86. struct device *dev;
  87. struct gpio_chip gpio;
  88. struct pch_gpio_reg_data pch_gpio_reg;
  89. int irq_base;
  90. enum pch_type_t ioh;
  91. spinlock_t spinlock;
  92. };
  93. static void pch_gpio_set(struct gpio_chip *gpio, unsigned int nr, int val)
  94. {
  95. u32 reg_val;
  96. struct pch_gpio *chip = gpiochip_get_data(gpio);
  97. unsigned long flags;
  98. spin_lock_irqsave(&chip->spinlock, flags);
  99. reg_val = ioread32(&chip->reg->po);
  100. if (val)
  101. reg_val |= BIT(nr);
  102. else
  103. reg_val &= ~BIT(nr);
  104. iowrite32(reg_val, &chip->reg->po);
  105. spin_unlock_irqrestore(&chip->spinlock, flags);
  106. }
  107. static int pch_gpio_get(struct gpio_chip *gpio, unsigned int nr)
  108. {
  109. struct pch_gpio *chip = gpiochip_get_data(gpio);
  110. return !!(ioread32(&chip->reg->pi) & BIT(nr));
  111. }
  112. static int pch_gpio_direction_output(struct gpio_chip *gpio, unsigned int nr,
  113. int val)
  114. {
  115. struct pch_gpio *chip = gpiochip_get_data(gpio);
  116. u32 pm;
  117. u32 reg_val;
  118. unsigned long flags;
  119. spin_lock_irqsave(&chip->spinlock, flags);
  120. reg_val = ioread32(&chip->reg->po);
  121. if (val)
  122. reg_val |= BIT(nr);
  123. else
  124. reg_val &= ~BIT(nr);
  125. iowrite32(reg_val, &chip->reg->po);
  126. pm = ioread32(&chip->reg->pm);
  127. pm &= BIT(gpio_pins[chip->ioh]) - 1;
  128. pm |= BIT(nr);
  129. iowrite32(pm, &chip->reg->pm);
  130. spin_unlock_irqrestore(&chip->spinlock, flags);
  131. return 0;
  132. }
  133. static int pch_gpio_direction_input(struct gpio_chip *gpio, unsigned int nr)
  134. {
  135. struct pch_gpio *chip = gpiochip_get_data(gpio);
  136. u32 pm;
  137. unsigned long flags;
  138. spin_lock_irqsave(&chip->spinlock, flags);
  139. pm = ioread32(&chip->reg->pm);
  140. pm &= BIT(gpio_pins[chip->ioh]) - 1;
  141. pm &= ~BIT(nr);
  142. iowrite32(pm, &chip->reg->pm);
  143. spin_unlock_irqrestore(&chip->spinlock, flags);
  144. return 0;
  145. }
  146. /*
  147. * Save register configuration and disable interrupts.
  148. */
  149. static void __maybe_unused pch_gpio_save_reg_conf(struct pch_gpio *chip)
  150. {
  151. chip->pch_gpio_reg.ien_reg = ioread32(&chip->reg->ien);
  152. chip->pch_gpio_reg.imask_reg = ioread32(&chip->reg->imask);
  153. chip->pch_gpio_reg.po_reg = ioread32(&chip->reg->po);
  154. chip->pch_gpio_reg.pm_reg = ioread32(&chip->reg->pm);
  155. chip->pch_gpio_reg.im0_reg = ioread32(&chip->reg->im0);
  156. if (chip->ioh == INTEL_EG20T_PCH)
  157. chip->pch_gpio_reg.im1_reg = ioread32(&chip->reg->im1);
  158. if (chip->ioh == OKISEMI_ML7223n_IOH)
  159. chip->pch_gpio_reg.gpio_use_sel_reg = ioread32(&chip->reg->gpio_use_sel);
  160. }
  161. /*
  162. * This function restores the register configuration of the GPIO device.
  163. */
  164. static void __maybe_unused pch_gpio_restore_reg_conf(struct pch_gpio *chip)
  165. {
  166. iowrite32(chip->pch_gpio_reg.ien_reg, &chip->reg->ien);
  167. iowrite32(chip->pch_gpio_reg.imask_reg, &chip->reg->imask);
  168. /* to store contents of PO register */
  169. iowrite32(chip->pch_gpio_reg.po_reg, &chip->reg->po);
  170. /* to store contents of PM register */
  171. iowrite32(chip->pch_gpio_reg.pm_reg, &chip->reg->pm);
  172. iowrite32(chip->pch_gpio_reg.im0_reg, &chip->reg->im0);
  173. if (chip->ioh == INTEL_EG20T_PCH)
  174. iowrite32(chip->pch_gpio_reg.im1_reg, &chip->reg->im1);
  175. if (chip->ioh == OKISEMI_ML7223n_IOH)
  176. iowrite32(chip->pch_gpio_reg.gpio_use_sel_reg, &chip->reg->gpio_use_sel);
  177. }
  178. static int pch_gpio_to_irq(struct gpio_chip *gpio, unsigned int offset)
  179. {
  180. struct pch_gpio *chip = gpiochip_get_data(gpio);
  181. return chip->irq_base + offset;
  182. }
  183. static void pch_gpio_setup(struct pch_gpio *chip)
  184. {
  185. struct gpio_chip *gpio = &chip->gpio;
  186. gpio->label = dev_name(chip->dev);
  187. gpio->parent = chip->dev;
  188. gpio->owner = THIS_MODULE;
  189. gpio->direction_input = pch_gpio_direction_input;
  190. gpio->get = pch_gpio_get;
  191. gpio->direction_output = pch_gpio_direction_output;
  192. gpio->set = pch_gpio_set;
  193. gpio->base = -1;
  194. gpio->ngpio = gpio_pins[chip->ioh];
  195. gpio->can_sleep = false;
  196. gpio->to_irq = pch_gpio_to_irq;
  197. }
  198. static int pch_irq_type(struct irq_data *d, unsigned int type)
  199. {
  200. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  201. struct pch_gpio *chip = gc->private;
  202. u32 im, im_pos, val;
  203. u32 __iomem *im_reg;
  204. unsigned long flags;
  205. int ch, irq = d->irq;
  206. ch = irq - chip->irq_base;
  207. if (irq < chip->irq_base + 8) {
  208. im_reg = &chip->reg->im0;
  209. im_pos = ch - 0;
  210. } else {
  211. im_reg = &chip->reg->im1;
  212. im_pos = ch - 8;
  213. }
  214. dev_dbg(chip->dev, "irq=%d type=%d ch=%d pos=%d\n", irq, type, ch, im_pos);
  215. switch (type) {
  216. case IRQ_TYPE_EDGE_RISING:
  217. val = PCH_EDGE_RISING;
  218. break;
  219. case IRQ_TYPE_EDGE_FALLING:
  220. val = PCH_EDGE_FALLING;
  221. break;
  222. case IRQ_TYPE_EDGE_BOTH:
  223. val = PCH_EDGE_BOTH;
  224. break;
  225. case IRQ_TYPE_LEVEL_HIGH:
  226. val = PCH_LEVEL_H;
  227. break;
  228. case IRQ_TYPE_LEVEL_LOW:
  229. val = PCH_LEVEL_L;
  230. break;
  231. default:
  232. return 0;
  233. }
  234. spin_lock_irqsave(&chip->spinlock, flags);
  235. /* Set interrupt mode */
  236. im = ioread32(im_reg) & ~(PCH_IM_MASK << (im_pos * 4));
  237. iowrite32(im | (val << (im_pos * 4)), im_reg);
  238. /* And the handler */
  239. if (type & IRQ_TYPE_LEVEL_MASK)
  240. irq_set_handler_locked(d, handle_level_irq);
  241. else if (type & IRQ_TYPE_EDGE_BOTH)
  242. irq_set_handler_locked(d, handle_edge_irq);
  243. spin_unlock_irqrestore(&chip->spinlock, flags);
  244. return 0;
  245. }
  246. static void pch_irq_unmask(struct irq_data *d)
  247. {
  248. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  249. struct pch_gpio *chip = gc->private;
  250. iowrite32(BIT(d->irq - chip->irq_base), &chip->reg->imaskclr);
  251. }
  252. static void pch_irq_mask(struct irq_data *d)
  253. {
  254. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  255. struct pch_gpio *chip = gc->private;
  256. iowrite32(BIT(d->irq - chip->irq_base), &chip->reg->imask);
  257. }
  258. static void pch_irq_ack(struct irq_data *d)
  259. {
  260. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  261. struct pch_gpio *chip = gc->private;
  262. iowrite32(BIT(d->irq - chip->irq_base), &chip->reg->iclr);
  263. }
  264. static irqreturn_t pch_gpio_handler(int irq, void *dev_id)
  265. {
  266. struct pch_gpio *chip = dev_id;
  267. unsigned long reg_val = ioread32(&chip->reg->istatus);
  268. int i;
  269. dev_vdbg(chip->dev, "irq=%d status=0x%lx\n", irq, reg_val);
  270. reg_val &= BIT(gpio_pins[chip->ioh]) - 1;
  271. for_each_set_bit(i, &reg_val, gpio_pins[chip->ioh])
  272. generic_handle_irq(chip->irq_base + i);
  273. return IRQ_RETVAL(reg_val);
  274. }
  275. static int pch_gpio_alloc_generic_chip(struct pch_gpio *chip,
  276. unsigned int irq_start,
  277. unsigned int num)
  278. {
  279. struct irq_chip_generic *gc;
  280. struct irq_chip_type *ct;
  281. int rv;
  282. gc = devm_irq_alloc_generic_chip(chip->dev, "pch_gpio", 1, irq_start,
  283. chip->base, handle_simple_irq);
  284. if (!gc)
  285. return -ENOMEM;
  286. gc->private = chip;
  287. ct = gc->chip_types;
  288. ct->chip.irq_ack = pch_irq_ack;
  289. ct->chip.irq_mask = pch_irq_mask;
  290. ct->chip.irq_unmask = pch_irq_unmask;
  291. ct->chip.irq_set_type = pch_irq_type;
  292. rv = devm_irq_setup_generic_chip(chip->dev, gc, IRQ_MSK(num),
  293. IRQ_GC_INIT_MASK_CACHE,
  294. IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  295. return rv;
  296. }
  297. static int pch_gpio_probe(struct pci_dev *pdev,
  298. const struct pci_device_id *id)
  299. {
  300. struct device *dev = &pdev->dev;
  301. s32 ret;
  302. struct pch_gpio *chip;
  303. int irq_base;
  304. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  305. if (chip == NULL)
  306. return -ENOMEM;
  307. chip->dev = dev;
  308. ret = pcim_enable_device(pdev);
  309. if (ret)
  310. return dev_err_probe(dev, ret, "Failed to enable PCI device\n");
  311. ret = pcim_iomap_regions(pdev, BIT(1), KBUILD_MODNAME);
  312. if (ret)
  313. return dev_err_probe(dev, ret, "Failed to request and map PCI regions\n");
  314. chip->base = pcim_iomap_table(pdev)[1];
  315. chip->ioh = id->driver_data;
  316. chip->reg = chip->base;
  317. pci_set_drvdata(pdev, chip);
  318. spin_lock_init(&chip->spinlock);
  319. pch_gpio_setup(chip);
  320. ret = devm_gpiochip_add_data(dev, &chip->gpio, chip);
  321. if (ret)
  322. return dev_err_probe(dev, ret, "Failed to register GPIO\n");
  323. irq_base = devm_irq_alloc_descs(dev, -1, 0,
  324. gpio_pins[chip->ioh], NUMA_NO_NODE);
  325. if (irq_base < 0) {
  326. dev_warn(dev, "PCH gpio: Failed to get IRQ base num\n");
  327. chip->irq_base = -1;
  328. return 0;
  329. }
  330. chip->irq_base = irq_base;
  331. /* Mask all interrupts, but enable them */
  332. iowrite32(BIT(gpio_pins[chip->ioh]) - 1, &chip->reg->imask);
  333. iowrite32(BIT(gpio_pins[chip->ioh]) - 1, &chip->reg->ien);
  334. ret = devm_request_irq(dev, pdev->irq, pch_gpio_handler,
  335. IRQF_SHARED, KBUILD_MODNAME, chip);
  336. if (ret)
  337. return dev_err_probe(dev, ret, "Failed to request IRQ\n");
  338. return pch_gpio_alloc_generic_chip(chip, irq_base, gpio_pins[chip->ioh]);
  339. }
  340. static int __maybe_unused pch_gpio_suspend(struct device *dev)
  341. {
  342. struct pch_gpio *chip = dev_get_drvdata(dev);
  343. unsigned long flags;
  344. spin_lock_irqsave(&chip->spinlock, flags);
  345. pch_gpio_save_reg_conf(chip);
  346. spin_unlock_irqrestore(&chip->spinlock, flags);
  347. return 0;
  348. }
  349. static int __maybe_unused pch_gpio_resume(struct device *dev)
  350. {
  351. struct pch_gpio *chip = dev_get_drvdata(dev);
  352. unsigned long flags;
  353. spin_lock_irqsave(&chip->spinlock, flags);
  354. iowrite32(0x01, &chip->reg->reset);
  355. iowrite32(0x00, &chip->reg->reset);
  356. pch_gpio_restore_reg_conf(chip);
  357. spin_unlock_irqrestore(&chip->spinlock, flags);
  358. return 0;
  359. }
  360. static SIMPLE_DEV_PM_OPS(pch_gpio_pm_ops, pch_gpio_suspend, pch_gpio_resume);
  361. static const struct pci_device_id pch_gpio_pcidev_id[] = {
  362. { PCI_DEVICE_DATA(INTEL, EG20T_PCH, INTEL_EG20T_PCH) },
  363. { PCI_DEVICE_DATA(ROHM, ML7223m_IOH, OKISEMI_ML7223m_IOH) },
  364. { PCI_DEVICE_DATA(ROHM, ML7223n_IOH, OKISEMI_ML7223n_IOH) },
  365. { PCI_DEVICE_DATA(ROHM, EG20T_PCH, INTEL_EG20T_PCH) },
  366. { }
  367. };
  368. MODULE_DEVICE_TABLE(pci, pch_gpio_pcidev_id);
  369. static struct pci_driver pch_gpio_driver = {
  370. .name = "pch_gpio",
  371. .id_table = pch_gpio_pcidev_id,
  372. .probe = pch_gpio_probe,
  373. .driver = {
  374. .pm = &pch_gpio_pm_ops,
  375. },
  376. };
  377. module_pci_driver(pch_gpio_driver);
  378. MODULE_DESCRIPTION("PCH GPIO PCI Driver");
  379. MODULE_LICENSE("GPL v2");