ppc4xx_gpio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #define GPIO_MASK(gpio) (0x80000000 >> (gpio))
  32. #define GPIO_MASK2(gpio) (0xc0000000 >> ((gpio) * 2))
  33. /* Physical GPIO register layout */
  34. struct ppc4xx_gpio {
  35. __be32 or;
  36. __be32 tcr;
  37. __be32 osrl;
  38. __be32 osrh;
  39. __be32 tsrl;
  40. __be32 tsrh;
  41. __be32 odr;
  42. __be32 ir;
  43. __be32 rr1;
  44. __be32 rr2;
  45. __be32 rr3;
  46. __be32 reserved1;
  47. __be32 isr1l;
  48. __be32 isr1h;
  49. __be32 isr2l;
  50. __be32 isr2h;
  51. __be32 isr3l;
  52. __be32 isr3h;
  53. };
  54. struct ppc4xx_gpio_chip {
  55. struct of_mm_gpio_chip mm_gc;
  56. spinlock_t lock;
  57. };
  58. /*
  59. * GPIO LIB API implementation for GPIOs
  60. *
  61. * There are a maximum of 32 gpios in each gpio controller.
  62. */
  63. static inline struct ppc4xx_gpio_chip *
  64. to_ppc4xx_gpiochip(struct of_mm_gpio_chip *mm_gc)
  65. {
  66. return container_of(mm_gc, struct ppc4xx_gpio_chip, mm_gc);
  67. }
  68. static int ppc4xx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  69. {
  70. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  71. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  72. return in_be32(&regs->ir) & GPIO_MASK(gpio);
  73. }
  74. static inline void
  75. __ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  76. {
  77. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  78. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  79. if (val)
  80. setbits32(&regs->or, GPIO_MASK(gpio));
  81. else
  82. clrbits32(&regs->or, GPIO_MASK(gpio));
  83. }
  84. static void
  85. ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  86. {
  87. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  88. struct ppc4xx_gpio_chip *chip = to_ppc4xx_gpiochip(mm_gc);
  89. unsigned long flags;
  90. spin_lock_irqsave(&chip->lock, flags);
  91. __ppc4xx_gpio_set(gc, gpio, val);
  92. spin_unlock_irqrestore(&chip->lock, flags);
  93. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  94. }
  95. static int ppc4xx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  96. {
  97. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  98. struct ppc4xx_gpio_chip *chip = to_ppc4xx_gpiochip(mm_gc);
  99. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  100. unsigned long flags;
  101. spin_lock_irqsave(&chip->lock, flags);
  102. /* Disable open-drain function */
  103. clrbits32(&regs->odr, GPIO_MASK(gpio));
  104. /* Float the pin */
  105. clrbits32(&regs->tcr, GPIO_MASK(gpio));
  106. /* Bits 0-15 use TSRL/OSRL, bits 16-31 use TSRH/OSRH */
  107. if (gpio < 16) {
  108. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  109. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  110. } else {
  111. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  112. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  113. }
  114. spin_unlock_irqrestore(&chip->lock, flags);
  115. return 0;
  116. }
  117. static int
  118. ppc4xx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  119. {
  120. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  121. struct ppc4xx_gpio_chip *chip = to_ppc4xx_gpiochip(mm_gc);
  122. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  123. unsigned long flags;
  124. spin_lock_irqsave(&chip->lock, flags);
  125. /* First set initial value */
  126. __ppc4xx_gpio_set(gc, gpio, val);
  127. /* Disable open-drain function */
  128. clrbits32(&regs->odr, GPIO_MASK(gpio));
  129. /* Drive the pin */
  130. setbits32(&regs->tcr, GPIO_MASK(gpio));
  131. /* Bits 0-15 use TSRL, bits 16-31 use TSRH */
  132. if (gpio < 16) {
  133. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  134. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  135. } else {
  136. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  137. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  138. }
  139. spin_unlock_irqrestore(&chip->lock, flags);
  140. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  141. return 0;
  142. }
  143. static int __init ppc4xx_add_gpiochips(void)
  144. {
  145. struct device_node *np;
  146. for_each_compatible_node(np, NULL, "ibm,ppc4xx-gpio") {
  147. int ret;
  148. struct ppc4xx_gpio_chip *ppc4xx_gc;
  149. struct of_mm_gpio_chip *mm_gc;
  150. struct of_gpio_chip *of_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. of_gc = &mm_gc->of_gc;
  160. gc = &of_gc->gc;
  161. of_gc->gpio_cells = 2;
  162. gc->ngpio = 32;
  163. gc->direction_input = ppc4xx_gpio_dir_in;
  164. gc->direction_output = ppc4xx_gpio_dir_out;
  165. gc->get = ppc4xx_gpio_get;
  166. gc->set = ppc4xx_gpio_set;
  167. ret = of_mm_gpiochip_add(np, mm_gc);
  168. if (ret)
  169. goto err;
  170. continue;
  171. err:
  172. pr_err("%s: registration failed with status %d\n",
  173. np->full_name, ret);
  174. kfree(ppc4xx_gc);
  175. /* try others anyway */
  176. }
  177. return 0;
  178. }
  179. arch_initcall(ppc4xx_add_gpiochips);