gpio-mlxbf3.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause
  2. /* Copyright (C) 2022 NVIDIA CORPORATION & AFFILIATES */
  3. #include <linux/bitfield.h>
  4. #include <linux/bitops.h>
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/gpio/driver.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/types.h>
  14. /*
  15. * There are 2 YU GPIO blocks:
  16. * gpio[0]: HOST_GPIO0->HOST_GPIO31
  17. * gpio[1]: HOST_GPIO32->HOST_GPIO55
  18. */
  19. #define MLXBF3_GPIO_MAX_PINS_PER_BLOCK 32
  20. #define MLXBF3_GPIO_MAX_PINS_BLOCK0 32
  21. #define MLXBF3_GPIO_MAX_PINS_BLOCK1 24
  22. /*
  23. * fw_gpio[x] block registers and their offset
  24. */
  25. #define MLXBF_GPIO_FW_OUTPUT_ENABLE_SET 0x00
  26. #define MLXBF_GPIO_FW_DATA_OUT_SET 0x04
  27. #define MLXBF_GPIO_FW_OUTPUT_ENABLE_CLEAR 0x00
  28. #define MLXBF_GPIO_FW_DATA_OUT_CLEAR 0x04
  29. #define MLXBF_GPIO_CAUSE_RISE_EN 0x00
  30. #define MLXBF_GPIO_CAUSE_FALL_EN 0x04
  31. #define MLXBF_GPIO_READ_DATA_IN 0x08
  32. #define MLXBF_GPIO_CAUSE_OR_CAUSE_EVTEN0 0x00
  33. #define MLXBF_GPIO_CAUSE_OR_EVTEN0 0x14
  34. #define MLXBF_GPIO_CAUSE_OR_CLRCAUSE 0x18
  35. #define MLXBF_GPIO_CLR_ALL_INTS GENMASK(31, 0)
  36. struct mlxbf3_gpio_context {
  37. struct gpio_chip gc;
  38. /* YU GPIO block address */
  39. void __iomem *gpio_set_io;
  40. void __iomem *gpio_clr_io;
  41. void __iomem *gpio_io;
  42. /* YU GPIO cause block address */
  43. void __iomem *gpio_cause_io;
  44. };
  45. static void mlxbf3_gpio_irq_enable(struct irq_data *irqd)
  46. {
  47. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  48. struct mlxbf3_gpio_context *gs = gpiochip_get_data(gc);
  49. irq_hw_number_t offset = irqd_to_hwirq(irqd);
  50. unsigned long flags;
  51. u32 val;
  52. gpiochip_enable_irq(gc, offset);
  53. raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
  54. writel(BIT(offset), gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_CLRCAUSE);
  55. val = readl(gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_EVTEN0);
  56. val |= BIT(offset);
  57. writel(val, gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_EVTEN0);
  58. raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
  59. }
  60. static void mlxbf3_gpio_irq_disable(struct irq_data *irqd)
  61. {
  62. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  63. struct mlxbf3_gpio_context *gs = gpiochip_get_data(gc);
  64. irq_hw_number_t offset = irqd_to_hwirq(irqd);
  65. unsigned long flags;
  66. u32 val;
  67. raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
  68. val = readl(gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_EVTEN0);
  69. val &= ~BIT(offset);
  70. writel(val, gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_EVTEN0);
  71. writel(BIT(offset), gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_CLRCAUSE);
  72. raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
  73. gpiochip_disable_irq(gc, offset);
  74. }
  75. static irqreturn_t mlxbf3_gpio_irq_handler(int irq, void *ptr)
  76. {
  77. struct mlxbf3_gpio_context *gs = ptr;
  78. struct gpio_chip *gc = &gs->gc;
  79. unsigned long pending;
  80. u32 level;
  81. pending = readl(gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_CAUSE_EVTEN0);
  82. writel(pending, gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_CLRCAUSE);
  83. for_each_set_bit(level, &pending, gc->ngpio)
  84. generic_handle_domain_irq(gc->irq.domain, level);
  85. return IRQ_RETVAL(pending);
  86. }
  87. static int
  88. mlxbf3_gpio_irq_set_type(struct irq_data *irqd, unsigned int type)
  89. {
  90. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  91. struct mlxbf3_gpio_context *gs = gpiochip_get_data(gc);
  92. irq_hw_number_t offset = irqd_to_hwirq(irqd);
  93. unsigned long flags;
  94. u32 val;
  95. raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
  96. switch (type & IRQ_TYPE_SENSE_MASK) {
  97. case IRQ_TYPE_EDGE_BOTH:
  98. val = readl(gs->gpio_io + MLXBF_GPIO_CAUSE_FALL_EN);
  99. val |= BIT(offset);
  100. writel(val, gs->gpio_io + MLXBF_GPIO_CAUSE_FALL_EN);
  101. val = readl(gs->gpio_io + MLXBF_GPIO_CAUSE_RISE_EN);
  102. val |= BIT(offset);
  103. writel(val, gs->gpio_io + MLXBF_GPIO_CAUSE_RISE_EN);
  104. break;
  105. case IRQ_TYPE_EDGE_RISING:
  106. val = readl(gs->gpio_io + MLXBF_GPIO_CAUSE_RISE_EN);
  107. val |= BIT(offset);
  108. writel(val, gs->gpio_io + MLXBF_GPIO_CAUSE_RISE_EN);
  109. break;
  110. case IRQ_TYPE_EDGE_FALLING:
  111. val = readl(gs->gpio_io + MLXBF_GPIO_CAUSE_FALL_EN);
  112. val |= BIT(offset);
  113. writel(val, gs->gpio_io + MLXBF_GPIO_CAUSE_FALL_EN);
  114. break;
  115. default:
  116. raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
  117. return -EINVAL;
  118. }
  119. raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
  120. irq_set_handler_locked(irqd, handle_edge_irq);
  121. return 0;
  122. }
  123. /* This function needs to be defined for handle_edge_irq() */
  124. static void mlxbf3_gpio_irq_ack(struct irq_data *data)
  125. {
  126. }
  127. static const struct irq_chip gpio_mlxbf3_irqchip = {
  128. .name = "MLNXBF33",
  129. .irq_ack = mlxbf3_gpio_irq_ack,
  130. .irq_set_type = mlxbf3_gpio_irq_set_type,
  131. .irq_enable = mlxbf3_gpio_irq_enable,
  132. .irq_disable = mlxbf3_gpio_irq_disable,
  133. .flags = IRQCHIP_IMMUTABLE,
  134. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  135. };
  136. static int mlxbf3_gpio_add_pin_ranges(struct gpio_chip *chip)
  137. {
  138. unsigned int id;
  139. switch(chip->ngpio) {
  140. case MLXBF3_GPIO_MAX_PINS_BLOCK0:
  141. id = 0;
  142. break;
  143. case MLXBF3_GPIO_MAX_PINS_BLOCK1:
  144. id = 1;
  145. break;
  146. default:
  147. return -EINVAL;
  148. }
  149. return gpiochip_add_pin_range(chip, "MLNXBF34:00",
  150. chip->base, id * MLXBF3_GPIO_MAX_PINS_PER_BLOCK,
  151. chip->ngpio);
  152. }
  153. static int mlxbf3_gpio_probe(struct platform_device *pdev)
  154. {
  155. struct device *dev = &pdev->dev;
  156. struct mlxbf3_gpio_context *gs;
  157. struct gpio_irq_chip *girq;
  158. struct gpio_chip *gc;
  159. int ret, irq;
  160. gs = devm_kzalloc(dev, sizeof(*gs), GFP_KERNEL);
  161. if (!gs)
  162. return -ENOMEM;
  163. gs->gpio_io = devm_platform_ioremap_resource(pdev, 0);
  164. if (IS_ERR(gs->gpio_io))
  165. return PTR_ERR(gs->gpio_io);
  166. gs->gpio_cause_io = devm_platform_ioremap_resource(pdev, 1);
  167. if (IS_ERR(gs->gpio_cause_io))
  168. return PTR_ERR(gs->gpio_cause_io);
  169. gs->gpio_set_io = devm_platform_ioremap_resource(pdev, 2);
  170. if (IS_ERR(gs->gpio_set_io))
  171. return PTR_ERR(gs->gpio_set_io);
  172. gs->gpio_clr_io = devm_platform_ioremap_resource(pdev, 3);
  173. if (IS_ERR(gs->gpio_clr_io))
  174. return PTR_ERR(gs->gpio_clr_io);
  175. gc = &gs->gc;
  176. ret = bgpio_init(gc, dev, 4,
  177. gs->gpio_io + MLXBF_GPIO_READ_DATA_IN,
  178. gs->gpio_set_io + MLXBF_GPIO_FW_DATA_OUT_SET,
  179. gs->gpio_clr_io + MLXBF_GPIO_FW_DATA_OUT_CLEAR,
  180. gs->gpio_set_io + MLXBF_GPIO_FW_OUTPUT_ENABLE_SET,
  181. gs->gpio_clr_io + MLXBF_GPIO_FW_OUTPUT_ENABLE_CLEAR, 0);
  182. if (ret)
  183. return dev_err_probe(dev, ret, "%s: bgpio_init() failed", __func__);
  184. gc->request = gpiochip_generic_request;
  185. gc->free = gpiochip_generic_free;
  186. gc->owner = THIS_MODULE;
  187. gc->add_pin_ranges = mlxbf3_gpio_add_pin_ranges;
  188. irq = platform_get_irq_optional(pdev, 0);
  189. if (irq >= 0) {
  190. girq = &gs->gc.irq;
  191. gpio_irq_chip_set_chip(girq, &gpio_mlxbf3_irqchip);
  192. girq->default_type = IRQ_TYPE_NONE;
  193. /* This will let us handle the parent IRQ in the driver */
  194. girq->num_parents = 0;
  195. girq->parents = NULL;
  196. girq->parent_handler = NULL;
  197. girq->handler = handle_bad_irq;
  198. /*
  199. * Directly request the irq here instead of passing
  200. * a flow-handler because the irq is shared.
  201. */
  202. ret = devm_request_irq(dev, irq, mlxbf3_gpio_irq_handler,
  203. IRQF_SHARED, dev_name(dev), gs);
  204. if (ret)
  205. return dev_err_probe(dev, ret, "failed to request IRQ");
  206. }
  207. platform_set_drvdata(pdev, gs);
  208. ret = devm_gpiochip_add_data(dev, &gs->gc, gs);
  209. if (ret)
  210. dev_err_probe(dev, ret, "Failed adding memory mapped gpiochip\n");
  211. return 0;
  212. }
  213. static void mlxbf3_gpio_shutdown(struct platform_device *pdev)
  214. {
  215. struct mlxbf3_gpio_context *gs = platform_get_drvdata(pdev);
  216. /* Disable and clear all interrupts */
  217. writel(0, gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_EVTEN0);
  218. writel(MLXBF_GPIO_CLR_ALL_INTS, gs->gpio_cause_io + MLXBF_GPIO_CAUSE_OR_CLRCAUSE);
  219. }
  220. static const struct acpi_device_id mlxbf3_gpio_acpi_match[] = {
  221. { "MLNXBF33", 0 },
  222. {}
  223. };
  224. MODULE_DEVICE_TABLE(acpi, mlxbf3_gpio_acpi_match);
  225. static struct platform_driver mlxbf3_gpio_driver = {
  226. .driver = {
  227. .name = "mlxbf3_gpio",
  228. .acpi_match_table = mlxbf3_gpio_acpi_match,
  229. },
  230. .probe = mlxbf3_gpio_probe,
  231. .shutdown = mlxbf3_gpio_shutdown,
  232. };
  233. module_platform_driver(mlxbf3_gpio_driver);
  234. MODULE_SOFTDEP("pre: pinctrl-mlxbf3");
  235. MODULE_DESCRIPTION("NVIDIA BlueField-3 GPIO Driver");
  236. MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
  237. MODULE_LICENSE("Dual BSD/GPL");