gpio-cadence.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2017-2018 Cadence
  4. *
  5. * Authors:
  6. * Jan Kotas <jank@cadence.com>
  7. * Boris Brezillon <boris.brezillon@free-electrons.com>
  8. */
  9. #include <linux/gpio/driver.h>
  10. #include <linux/clk.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/spinlock.h>
  16. #define CDNS_GPIO_BYPASS_MODE 0x00
  17. #define CDNS_GPIO_DIRECTION_MODE 0x04
  18. #define CDNS_GPIO_OUTPUT_EN 0x08
  19. #define CDNS_GPIO_OUTPUT_VALUE 0x0c
  20. #define CDNS_GPIO_INPUT_VALUE 0x10
  21. #define CDNS_GPIO_IRQ_MASK 0x14
  22. #define CDNS_GPIO_IRQ_EN 0x18
  23. #define CDNS_GPIO_IRQ_DIS 0x1c
  24. #define CDNS_GPIO_IRQ_STATUS 0x20
  25. #define CDNS_GPIO_IRQ_TYPE 0x24
  26. #define CDNS_GPIO_IRQ_VALUE 0x28
  27. #define CDNS_GPIO_IRQ_ANY_EDGE 0x2c
  28. struct cdns_gpio_chip {
  29. struct gpio_chip gc;
  30. void __iomem *regs;
  31. u32 bypass_orig;
  32. };
  33. static int cdns_gpio_request(struct gpio_chip *chip, unsigned int offset)
  34. {
  35. struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
  36. unsigned long flags;
  37. raw_spin_lock_irqsave(&chip->bgpio_lock, flags);
  38. iowrite32(ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE) & ~BIT(offset),
  39. cgpio->regs + CDNS_GPIO_BYPASS_MODE);
  40. raw_spin_unlock_irqrestore(&chip->bgpio_lock, flags);
  41. return 0;
  42. }
  43. static void cdns_gpio_free(struct gpio_chip *chip, unsigned int offset)
  44. {
  45. struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
  46. unsigned long flags;
  47. raw_spin_lock_irqsave(&chip->bgpio_lock, flags);
  48. iowrite32(ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE) |
  49. (BIT(offset) & cgpio->bypass_orig),
  50. cgpio->regs + CDNS_GPIO_BYPASS_MODE);
  51. raw_spin_unlock_irqrestore(&chip->bgpio_lock, flags);
  52. }
  53. static void cdns_gpio_irq_mask(struct irq_data *d)
  54. {
  55. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  56. struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
  57. iowrite32(BIT(d->hwirq), cgpio->regs + CDNS_GPIO_IRQ_DIS);
  58. gpiochip_disable_irq(chip, irqd_to_hwirq(d));
  59. }
  60. static void cdns_gpio_irq_unmask(struct irq_data *d)
  61. {
  62. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  63. struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
  64. gpiochip_enable_irq(chip, irqd_to_hwirq(d));
  65. iowrite32(BIT(d->hwirq), cgpio->regs + CDNS_GPIO_IRQ_EN);
  66. }
  67. static int cdns_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  68. {
  69. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  70. struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
  71. unsigned long flags;
  72. u32 int_value;
  73. u32 int_type;
  74. u32 mask = BIT(d->hwirq);
  75. int ret = 0;
  76. raw_spin_lock_irqsave(&chip->bgpio_lock, flags);
  77. int_value = ioread32(cgpio->regs + CDNS_GPIO_IRQ_VALUE) & ~mask;
  78. int_type = ioread32(cgpio->regs + CDNS_GPIO_IRQ_TYPE) & ~mask;
  79. /*
  80. * The GPIO controller doesn't have an ACK register.
  81. * All interrupt statuses are cleared on a status register read.
  82. * Don't support edge interrupts for now.
  83. */
  84. if (type == IRQ_TYPE_LEVEL_HIGH) {
  85. int_type |= mask;
  86. int_value |= mask;
  87. } else if (type == IRQ_TYPE_LEVEL_LOW) {
  88. int_type |= mask;
  89. } else {
  90. ret = -EINVAL;
  91. goto err_irq_type;
  92. }
  93. iowrite32(int_value, cgpio->regs + CDNS_GPIO_IRQ_VALUE);
  94. iowrite32(int_type, cgpio->regs + CDNS_GPIO_IRQ_TYPE);
  95. err_irq_type:
  96. raw_spin_unlock_irqrestore(&chip->bgpio_lock, flags);
  97. return ret;
  98. }
  99. static void cdns_gpio_irq_handler(struct irq_desc *desc)
  100. {
  101. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  102. struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
  103. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  104. unsigned long status;
  105. int hwirq;
  106. chained_irq_enter(irqchip, desc);
  107. status = ioread32(cgpio->regs + CDNS_GPIO_IRQ_STATUS) &
  108. ~ioread32(cgpio->regs + CDNS_GPIO_IRQ_MASK);
  109. for_each_set_bit(hwirq, &status, chip->ngpio)
  110. generic_handle_domain_irq(chip->irq.domain, hwirq);
  111. chained_irq_exit(irqchip, desc);
  112. }
  113. static const struct irq_chip cdns_gpio_irqchip = {
  114. .name = "cdns-gpio",
  115. .irq_mask = cdns_gpio_irq_mask,
  116. .irq_unmask = cdns_gpio_irq_unmask,
  117. .irq_set_type = cdns_gpio_irq_set_type,
  118. .flags = IRQCHIP_IMMUTABLE,
  119. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  120. };
  121. static int cdns_gpio_probe(struct platform_device *pdev)
  122. {
  123. struct cdns_gpio_chip *cgpio;
  124. int ret, irq;
  125. u32 dir_prev;
  126. u32 num_gpios = 32;
  127. struct clk *clk;
  128. cgpio = devm_kzalloc(&pdev->dev, sizeof(*cgpio), GFP_KERNEL);
  129. if (!cgpio)
  130. return -ENOMEM;
  131. cgpio->regs = devm_platform_ioremap_resource(pdev, 0);
  132. if (IS_ERR(cgpio->regs))
  133. return PTR_ERR(cgpio->regs);
  134. of_property_read_u32(pdev->dev.of_node, "ngpios", &num_gpios);
  135. if (num_gpios > 32) {
  136. dev_err(&pdev->dev, "ngpios must be less or equal 32\n");
  137. return -EINVAL;
  138. }
  139. /*
  140. * Set all pins as inputs by default, otherwise:
  141. * gpiochip_lock_as_irq:
  142. * tried to flag a GPIO set as output for IRQ
  143. * Generic GPIO driver stores the direction value internally,
  144. * so it needs to be changed before bgpio_init() is called.
  145. */
  146. dir_prev = ioread32(cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
  147. iowrite32(GENMASK(num_gpios - 1, 0),
  148. cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
  149. ret = bgpio_init(&cgpio->gc, &pdev->dev, 4,
  150. cgpio->regs + CDNS_GPIO_INPUT_VALUE,
  151. cgpio->regs + CDNS_GPIO_OUTPUT_VALUE,
  152. NULL,
  153. NULL,
  154. cgpio->regs + CDNS_GPIO_DIRECTION_MODE,
  155. BGPIOF_READ_OUTPUT_REG_SET);
  156. if (ret) {
  157. dev_err(&pdev->dev, "Failed to register generic gpio, %d\n",
  158. ret);
  159. goto err_revert_dir;
  160. }
  161. cgpio->gc.label = dev_name(&pdev->dev);
  162. cgpio->gc.ngpio = num_gpios;
  163. cgpio->gc.parent = &pdev->dev;
  164. cgpio->gc.base = -1;
  165. cgpio->gc.owner = THIS_MODULE;
  166. cgpio->gc.request = cdns_gpio_request;
  167. cgpio->gc.free = cdns_gpio_free;
  168. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  169. if (IS_ERR(clk)) {
  170. ret = PTR_ERR(clk);
  171. dev_err(&pdev->dev,
  172. "Failed to retrieve peripheral clock, %d\n", ret);
  173. goto err_revert_dir;
  174. }
  175. /*
  176. * Optional irq_chip support
  177. */
  178. irq = platform_get_irq(pdev, 0);
  179. if (irq >= 0) {
  180. struct gpio_irq_chip *girq;
  181. girq = &cgpio->gc.irq;
  182. gpio_irq_chip_set_chip(girq, &cdns_gpio_irqchip);
  183. girq->parent_handler = cdns_gpio_irq_handler;
  184. girq->num_parents = 1;
  185. girq->parents = devm_kcalloc(&pdev->dev, 1,
  186. sizeof(*girq->parents),
  187. GFP_KERNEL);
  188. if (!girq->parents) {
  189. ret = -ENOMEM;
  190. goto err_revert_dir;
  191. }
  192. girq->parents[0] = irq;
  193. girq->default_type = IRQ_TYPE_NONE;
  194. girq->handler = handle_level_irq;
  195. }
  196. ret = devm_gpiochip_add_data(&pdev->dev, &cgpio->gc, cgpio);
  197. if (ret < 0) {
  198. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  199. goto err_revert_dir;
  200. }
  201. cgpio->bypass_orig = ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE);
  202. /*
  203. * Enable gpio outputs, ignored for input direction
  204. */
  205. iowrite32(GENMASK(num_gpios - 1, 0),
  206. cgpio->regs + CDNS_GPIO_OUTPUT_EN);
  207. iowrite32(0, cgpio->regs + CDNS_GPIO_BYPASS_MODE);
  208. platform_set_drvdata(pdev, cgpio);
  209. return 0;
  210. err_revert_dir:
  211. iowrite32(dir_prev, cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
  212. return ret;
  213. }
  214. static void cdns_gpio_remove(struct platform_device *pdev)
  215. {
  216. struct cdns_gpio_chip *cgpio = platform_get_drvdata(pdev);
  217. iowrite32(cgpio->bypass_orig, cgpio->regs + CDNS_GPIO_BYPASS_MODE);
  218. }
  219. static const struct of_device_id cdns_of_ids[] = {
  220. { .compatible = "cdns,gpio-r1p02" },
  221. { /* sentinel */ },
  222. };
  223. MODULE_DEVICE_TABLE(of, cdns_of_ids);
  224. static struct platform_driver cdns_gpio_driver = {
  225. .driver = {
  226. .name = "cdns-gpio",
  227. .of_match_table = cdns_of_ids,
  228. },
  229. .probe = cdns_gpio_probe,
  230. .remove_new = cdns_gpio_remove,
  231. };
  232. module_platform_driver(cdns_gpio_driver);
  233. MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
  234. MODULE_DESCRIPTION("Cadence GPIO driver");
  235. MODULE_LICENSE("GPL v2");
  236. MODULE_ALIAS("platform:cdns-gpio");