ppc4xx_gpio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * PPC4xx gpio driver
  3. *
  4. * Copyright (c) 2008 Harris Corporation
  5. * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  6. * Copyright (c) MontaVista Software, Inc. 2008.
  7. *
  8. * Author: Steve Falco <sfalco@harris.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/io.h>
  27. #include <linux/of.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/gpio.h>
  30. #include <linux/types.h>
  31. #include <linux/slab.h>
  32. #define GPIO_MASK(gpio) (0x80000000 >> (gpio))
  33. #define GPIO_MASK2(gpio) (0xc0000000 >> ((gpio) * 2))
  34. /* Physical GPIO register layout */
  35. struct ppc4xx_gpio {
  36. __be32 or;
  37. __be32 tcr;
  38. __be32 osrl;
  39. __be32 osrh;
  40. __be32 tsrl;
  41. __be32 tsrh;
  42. __be32 odr;
  43. __be32 ir;
  44. __be32 rr1;
  45. __be32 rr2;
  46. __be32 rr3;
  47. __be32 reserved1;
  48. __be32 isr1l;
  49. __be32 isr1h;
  50. __be32 isr2l;
  51. __be32 isr2h;
  52. __be32 isr3l;
  53. __be32 isr3h;
  54. };
  55. struct ppc4xx_gpio_chip {
  56. struct of_mm_gpio_chip mm_gc;
  57. spinlock_t lock;
  58. };
  59. /*
  60. * GPIO LIB API implementation for GPIOs
  61. *
  62. * There are a maximum of 32 gpios in each gpio controller.
  63. */
  64. static inline struct ppc4xx_gpio_chip *
  65. to_ppc4xx_gpiochip(struct of_mm_gpio_chip *mm_gc)
  66. {
  67. return container_of(mm_gc, struct ppc4xx_gpio_chip, mm_gc);
  68. }
  69. static int ppc4xx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  70. {
  71. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  72. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  73. return in_be32(&regs->ir) & GPIO_MASK(gpio);
  74. }
  75. static inline void
  76. __ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  77. {
  78. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  79. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  80. if (val)
  81. setbits32(&regs->or, GPIO_MASK(gpio));
  82. else
  83. clrbits32(&regs->or, GPIO_MASK(gpio));
  84. }
  85. static void
  86. ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  87. {
  88. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  89. struct ppc4xx_gpio_chip *chip = to_ppc4xx_gpiochip(mm_gc);
  90. unsigned long flags;
  91. spin_lock_irqsave(&chip->lock, flags);
  92. __ppc4xx_gpio_set(gc, gpio, val);
  93. spin_unlock_irqrestore(&chip->lock, flags);
  94. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  95. }
  96. static int ppc4xx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  97. {
  98. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  99. struct ppc4xx_gpio_chip *chip = to_ppc4xx_gpiochip(mm_gc);
  100. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  101. unsigned long flags;
  102. spin_lock_irqsave(&chip->lock, flags);
  103. /* Disable open-drain function */
  104. clrbits32(&regs->odr, GPIO_MASK(gpio));
  105. /* Float the pin */
  106. clrbits32(&regs->tcr, GPIO_MASK(gpio));
  107. /* Bits 0-15 use TSRL/OSRL, bits 16-31 use TSRH/OSRH */
  108. if (gpio < 16) {
  109. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  110. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  111. } else {
  112. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  113. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  114. }
  115. spin_unlock_irqrestore(&chip->lock, flags);
  116. return 0;
  117. }
  118. static int
  119. ppc4xx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  120. {
  121. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  122. struct ppc4xx_gpio_chip *chip = to_ppc4xx_gpiochip(mm_gc);
  123. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  124. unsigned long flags;
  125. spin_lock_irqsave(&chip->lock, flags);
  126. /* First set initial value */
  127. __ppc4xx_gpio_set(gc, gpio, val);
  128. /* Disable open-drain function */
  129. clrbits32(&regs->odr, GPIO_MASK(gpio));
  130. /* Drive the pin */
  131. setbits32(&regs->tcr, GPIO_MASK(gpio));
  132. /* Bits 0-15 use TSRL, bits 16-31 use TSRH */
  133. if (gpio < 16) {
  134. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  135. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  136. } else {
  137. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  138. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  139. }
  140. spin_unlock_irqrestore(&chip->lock, flags);
  141. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  142. return 0;
  143. }
  144. static int __init ppc4xx_add_gpiochips(void)
  145. {
  146. struct device_node *np;
  147. for_each_compatible_node(np, NULL, "ibm,ppc4xx-gpio") {
  148. int ret;
  149. struct ppc4xx_gpio_chip *ppc4xx_gc;
  150. struct of_mm_gpio_chip *mm_gc;
  151. struct gpio_chip *gc;
  152. ppc4xx_gc = kzalloc(sizeof(*ppc4xx_gc), GFP_KERNEL);
  153. if (!ppc4xx_gc) {
  154. ret = -ENOMEM;
  155. goto err;
  156. }
  157. spin_lock_init(&ppc4xx_gc->lock);
  158. mm_gc = &ppc4xx_gc->mm_gc;
  159. gc = &mm_gc->gc;
  160. gc->ngpio = 32;
  161. gc->direction_input = ppc4xx_gpio_dir_in;
  162. gc->direction_output = ppc4xx_gpio_dir_out;
  163. gc->get = ppc4xx_gpio_get;
  164. gc->set = ppc4xx_gpio_set;
  165. ret = of_mm_gpiochip_add(np, mm_gc);
  166. if (ret)
  167. goto err;
  168. continue;
  169. err:
  170. pr_err("%s: registration failed with status %d\n",
  171. np->full_name, ret);
  172. kfree(ppc4xx_gc);
  173. /* try others anyway */
  174. }
  175. return 0;
  176. }
  177. arch_initcall(ppc4xx_add_gpiochips);