gpio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PPC4xx gpio driver
  4. *
  5. * Copyright (c) 2008 Harris Corporation
  6. * Copyright (c) 2008 Sascha Hauer <[email protected]>, Pengutronix
  7. * Copyright (c) MontaVista Software, Inc. 2008.
  8. *
  9. * Author: Steve Falco <[email protected]>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/types.h>
  19. #include <linux/slab.h>
  20. #define GPIO_MASK(gpio) (0x80000000 >> (gpio))
  21. #define GPIO_MASK2(gpio) (0xc0000000 >> ((gpio) * 2))
  22. /* Physical GPIO register layout */
  23. struct ppc4xx_gpio {
  24. __be32 or;
  25. __be32 tcr;
  26. __be32 osrl;
  27. __be32 osrh;
  28. __be32 tsrl;
  29. __be32 tsrh;
  30. __be32 odr;
  31. __be32 ir;
  32. __be32 rr1;
  33. __be32 rr2;
  34. __be32 rr3;
  35. __be32 reserved1;
  36. __be32 isr1l;
  37. __be32 isr1h;
  38. __be32 isr2l;
  39. __be32 isr2h;
  40. __be32 isr3l;
  41. __be32 isr3h;
  42. };
  43. struct ppc4xx_gpio_chip {
  44. struct of_mm_gpio_chip mm_gc;
  45. spinlock_t lock;
  46. };
  47. /*
  48. * GPIO LIB API implementation for GPIOs
  49. *
  50. * There are a maximum of 32 gpios in each gpio controller.
  51. */
  52. static int ppc4xx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  53. {
  54. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  55. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  56. return !!(in_be32(&regs->ir) & GPIO_MASK(gpio));
  57. }
  58. static inline void
  59. __ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  60. {
  61. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  62. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  63. if (val)
  64. setbits32(&regs->or, GPIO_MASK(gpio));
  65. else
  66. clrbits32(&regs->or, GPIO_MASK(gpio));
  67. }
  68. static void
  69. ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  70. {
  71. struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
  72. unsigned long flags;
  73. spin_lock_irqsave(&chip->lock, flags);
  74. __ppc4xx_gpio_set(gc, gpio, val);
  75. spin_unlock_irqrestore(&chip->lock, flags);
  76. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  77. }
  78. static int ppc4xx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  79. {
  80. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  81. struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
  82. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  83. unsigned long flags;
  84. spin_lock_irqsave(&chip->lock, flags);
  85. /* Disable open-drain function */
  86. clrbits32(&regs->odr, GPIO_MASK(gpio));
  87. /* Float the pin */
  88. clrbits32(&regs->tcr, GPIO_MASK(gpio));
  89. /* Bits 0-15 use TSRL/OSRL, bits 16-31 use TSRH/OSRH */
  90. if (gpio < 16) {
  91. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  92. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  93. } else {
  94. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  95. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  96. }
  97. spin_unlock_irqrestore(&chip->lock, flags);
  98. return 0;
  99. }
  100. static int
  101. ppc4xx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  102. {
  103. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  104. struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
  105. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  106. unsigned long flags;
  107. spin_lock_irqsave(&chip->lock, flags);
  108. /* First set initial value */
  109. __ppc4xx_gpio_set(gc, gpio, val);
  110. /* Disable open-drain function */
  111. clrbits32(&regs->odr, GPIO_MASK(gpio));
  112. /* Drive the pin */
  113. setbits32(&regs->tcr, GPIO_MASK(gpio));
  114. /* Bits 0-15 use TSRL, bits 16-31 use TSRH */
  115. if (gpio < 16) {
  116. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  117. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  118. } else {
  119. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  120. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  121. }
  122. spin_unlock_irqrestore(&chip->lock, flags);
  123. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  124. return 0;
  125. }
  126. static int __init ppc4xx_add_gpiochips(void)
  127. {
  128. struct device_node *np;
  129. for_each_compatible_node(np, NULL, "ibm,ppc4xx-gpio") {
  130. int ret;
  131. struct ppc4xx_gpio_chip *ppc4xx_gc;
  132. struct of_mm_gpio_chip *mm_gc;
  133. struct gpio_chip *gc;
  134. ppc4xx_gc = kzalloc(sizeof(*ppc4xx_gc), GFP_KERNEL);
  135. if (!ppc4xx_gc) {
  136. ret = -ENOMEM;
  137. goto err;
  138. }
  139. spin_lock_init(&ppc4xx_gc->lock);
  140. mm_gc = &ppc4xx_gc->mm_gc;
  141. gc = &mm_gc->gc;
  142. gc->ngpio = 32;
  143. gc->direction_input = ppc4xx_gpio_dir_in;
  144. gc->direction_output = ppc4xx_gpio_dir_out;
  145. gc->get = ppc4xx_gpio_get;
  146. gc->set = ppc4xx_gpio_set;
  147. ret = of_mm_gpiochip_add_data(np, mm_gc, ppc4xx_gc);
  148. if (ret)
  149. goto err;
  150. continue;
  151. err:
  152. pr_err("%pOF: registration failed with status %d\n", np, ret);
  153. kfree(ppc4xx_gc);
  154. /* try others anyway */
  155. }
  156. return 0;
  157. }
  158. arch_initcall(ppc4xx_add_gpiochips);