gpio-visconti.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Toshiba Visconti GPIO Support
  4. *
  5. * (C) Copyright 2020 Toshiba Electronic Devices & Storage Corporation
  6. * (C) Copyright 2020 TOSHIBA CORPORATION
  7. *
  8. * Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/property.h>
  19. #include <linux/seq_file.h>
  20. /* register offset */
  21. #define GPIO_DIR 0x00
  22. #define GPIO_IDATA 0x08
  23. #define GPIO_ODATA 0x10
  24. #define GPIO_OSET 0x18
  25. #define GPIO_OCLR 0x20
  26. #define GPIO_INTMODE 0x30
  27. #define BASE_HW_IRQ 24
  28. struct visconti_gpio {
  29. void __iomem *base;
  30. spinlock_t lock; /* protect gpio register */
  31. struct gpio_chip gpio_chip;
  32. struct device *dev;
  33. };
  34. static int visconti_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  35. {
  36. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  37. struct visconti_gpio *priv = gpiochip_get_data(gc);
  38. u32 offset = irqd_to_hwirq(d);
  39. u32 bit = BIT(offset);
  40. u32 intc_type = IRQ_TYPE_EDGE_RISING;
  41. u32 intmode, odata;
  42. int ret = 0;
  43. unsigned long flags;
  44. spin_lock_irqsave(&priv->lock, flags);
  45. odata = readl(priv->base + GPIO_ODATA);
  46. intmode = readl(priv->base + GPIO_INTMODE);
  47. switch (type) {
  48. case IRQ_TYPE_EDGE_RISING:
  49. odata &= ~bit;
  50. intmode &= ~bit;
  51. break;
  52. case IRQ_TYPE_EDGE_FALLING:
  53. odata |= bit;
  54. intmode &= ~bit;
  55. break;
  56. case IRQ_TYPE_EDGE_BOTH:
  57. intmode |= bit;
  58. break;
  59. case IRQ_TYPE_LEVEL_HIGH:
  60. intc_type = IRQ_TYPE_LEVEL_HIGH;
  61. odata &= ~bit;
  62. intmode &= ~bit;
  63. break;
  64. case IRQ_TYPE_LEVEL_LOW:
  65. intc_type = IRQ_TYPE_LEVEL_HIGH;
  66. odata |= bit;
  67. intmode &= ~bit;
  68. break;
  69. default:
  70. ret = -EINVAL;
  71. goto err;
  72. }
  73. writel(odata, priv->base + GPIO_ODATA);
  74. writel(intmode, priv->base + GPIO_INTMODE);
  75. irq_set_irq_type(offset, intc_type);
  76. ret = irq_chip_set_type_parent(d, type);
  77. err:
  78. spin_unlock_irqrestore(&priv->lock, flags);
  79. return ret;
  80. }
  81. static int visconti_gpio_child_to_parent_hwirq(struct gpio_chip *gc,
  82. unsigned int child,
  83. unsigned int child_type,
  84. unsigned int *parent,
  85. unsigned int *parent_type)
  86. {
  87. /* Interrupts 0..15 mapped to interrupts 24..39 on the GIC */
  88. if (child < 16) {
  89. /* All these interrupts are level high in the CPU */
  90. *parent_type = IRQ_TYPE_LEVEL_HIGH;
  91. *parent = child + BASE_HW_IRQ;
  92. return 0;
  93. }
  94. return -EINVAL;
  95. }
  96. static int visconti_gpio_populate_parent_fwspec(struct gpio_chip *chip,
  97. union gpio_irq_fwspec *gfwspec,
  98. unsigned int parent_hwirq,
  99. unsigned int parent_type)
  100. {
  101. struct irq_fwspec *fwspec = &gfwspec->fwspec;
  102. fwspec->fwnode = chip->irq.parent_domain->fwnode;
  103. fwspec->param_count = 3;
  104. fwspec->param[0] = 0;
  105. fwspec->param[1] = parent_hwirq;
  106. fwspec->param[2] = parent_type;
  107. return 0;
  108. }
  109. static void visconti_gpio_mask_irq(struct irq_data *d)
  110. {
  111. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  112. irq_chip_mask_parent(d);
  113. gpiochip_disable_irq(gc, irqd_to_hwirq(d));
  114. }
  115. static void visconti_gpio_unmask_irq(struct irq_data *d)
  116. {
  117. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  118. gpiochip_enable_irq(gc, irqd_to_hwirq(d));
  119. irq_chip_unmask_parent(d);
  120. }
  121. static void visconti_gpio_irq_print_chip(struct irq_data *d, struct seq_file *p)
  122. {
  123. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  124. struct visconti_gpio *priv = gpiochip_get_data(gc);
  125. seq_printf(p, dev_name(priv->dev));
  126. }
  127. static const struct irq_chip visconti_gpio_irq_chip = {
  128. .irq_mask = visconti_gpio_mask_irq,
  129. .irq_unmask = visconti_gpio_unmask_irq,
  130. .irq_eoi = irq_chip_eoi_parent,
  131. .irq_set_type = visconti_gpio_irq_set_type,
  132. .irq_print_chip = visconti_gpio_irq_print_chip,
  133. .flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND |
  134. IRQCHIP_IMMUTABLE,
  135. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  136. };
  137. static int visconti_gpio_probe(struct platform_device *pdev)
  138. {
  139. struct device *dev = &pdev->dev;
  140. struct visconti_gpio *priv;
  141. struct gpio_irq_chip *girq;
  142. struct irq_domain *parent;
  143. struct device_node *irq_parent;
  144. int ret;
  145. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  146. if (!priv)
  147. return -ENOMEM;
  148. spin_lock_init(&priv->lock);
  149. priv->dev = dev;
  150. priv->base = devm_platform_ioremap_resource(pdev, 0);
  151. if (IS_ERR(priv->base))
  152. return PTR_ERR(priv->base);
  153. irq_parent = of_irq_find_parent(dev->of_node);
  154. if (!irq_parent) {
  155. dev_err(dev, "No IRQ parent node\n");
  156. return -ENODEV;
  157. }
  158. parent = irq_find_host(irq_parent);
  159. of_node_put(irq_parent);
  160. if (!parent) {
  161. dev_err(dev, "No IRQ parent domain\n");
  162. return -ENODEV;
  163. }
  164. ret = bgpio_init(&priv->gpio_chip, dev, 4,
  165. priv->base + GPIO_IDATA,
  166. priv->base + GPIO_OSET,
  167. priv->base + GPIO_OCLR,
  168. priv->base + GPIO_DIR,
  169. NULL,
  170. 0);
  171. if (ret) {
  172. dev_err(dev, "unable to init generic GPIO\n");
  173. return ret;
  174. }
  175. girq = &priv->gpio_chip.irq;
  176. gpio_irq_chip_set_chip(girq, &visconti_gpio_irq_chip);
  177. girq->fwnode = dev_fwnode(dev);
  178. girq->parent_domain = parent;
  179. girq->child_to_parent_hwirq = visconti_gpio_child_to_parent_hwirq;
  180. girq->populate_parent_alloc_arg = visconti_gpio_populate_parent_fwspec;
  181. girq->default_type = IRQ_TYPE_NONE;
  182. girq->handler = handle_level_irq;
  183. return devm_gpiochip_add_data(dev, &priv->gpio_chip, priv);
  184. }
  185. static const struct of_device_id visconti_gpio_of_match[] = {
  186. { .compatible = "toshiba,gpio-tmpv7708", },
  187. { /* end of table */ }
  188. };
  189. MODULE_DEVICE_TABLE(of, visconti_gpio_of_match);
  190. static struct platform_driver visconti_gpio_driver = {
  191. .probe = visconti_gpio_probe,
  192. .driver = {
  193. .name = "visconti_gpio",
  194. .of_match_table = visconti_gpio_of_match,
  195. }
  196. };
  197. module_platform_driver(visconti_gpio_driver);
  198. MODULE_AUTHOR("Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>");
  199. MODULE_DESCRIPTION("Toshiba Visconti GPIO Driver");
  200. MODULE_LICENSE("GPL v2");