ppc4xx_gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/driver.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 int ppc4xx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  65. {
  66. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  67. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  68. return !!(in_be32(&regs->ir) & GPIO_MASK(gpio));
  69. }
  70. static inline void
  71. __ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  72. {
  73. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  74. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  75. if (val)
  76. setbits32(&regs->or, GPIO_MASK(gpio));
  77. else
  78. clrbits32(&regs->or, GPIO_MASK(gpio));
  79. }
  80. static void
  81. ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  82. {
  83. struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
  84. unsigned long flags;
  85. spin_lock_irqsave(&chip->lock, flags);
  86. __ppc4xx_gpio_set(gc, gpio, val);
  87. spin_unlock_irqrestore(&chip->lock, flags);
  88. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  89. }
  90. static int ppc4xx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  91. {
  92. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  93. struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
  94. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  95. unsigned long flags;
  96. spin_lock_irqsave(&chip->lock, flags);
  97. /* Disable open-drain function */
  98. clrbits32(&regs->odr, GPIO_MASK(gpio));
  99. /* Float the pin */
  100. clrbits32(&regs->tcr, GPIO_MASK(gpio));
  101. /* Bits 0-15 use TSRL/OSRL, bits 16-31 use TSRH/OSRH */
  102. if (gpio < 16) {
  103. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  104. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  105. } else {
  106. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  107. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  108. }
  109. spin_unlock_irqrestore(&chip->lock, flags);
  110. return 0;
  111. }
  112. static int
  113. ppc4xx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  114. {
  115. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  116. struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
  117. struct ppc4xx_gpio __iomem *regs = mm_gc->regs;
  118. unsigned long flags;
  119. spin_lock_irqsave(&chip->lock, flags);
  120. /* First set initial value */
  121. __ppc4xx_gpio_set(gc, gpio, val);
  122. /* Disable open-drain function */
  123. clrbits32(&regs->odr, GPIO_MASK(gpio));
  124. /* Drive the pin */
  125. setbits32(&regs->tcr, GPIO_MASK(gpio));
  126. /* Bits 0-15 use TSRL, bits 16-31 use TSRH */
  127. if (gpio < 16) {
  128. clrbits32(&regs->osrl, GPIO_MASK2(gpio));
  129. clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
  130. } else {
  131. clrbits32(&regs->osrh, GPIO_MASK2(gpio));
  132. clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
  133. }
  134. spin_unlock_irqrestore(&chip->lock, flags);
  135. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  136. return 0;
  137. }
  138. static int __init ppc4xx_add_gpiochips(void)
  139. {
  140. struct device_node *np;
  141. for_each_compatible_node(np, NULL, "ibm,ppc4xx-gpio") {
  142. int ret;
  143. struct ppc4xx_gpio_chip *ppc4xx_gc;
  144. struct of_mm_gpio_chip *mm_gc;
  145. struct gpio_chip *gc;
  146. ppc4xx_gc = kzalloc(sizeof(*ppc4xx_gc), GFP_KERNEL);
  147. if (!ppc4xx_gc) {
  148. ret = -ENOMEM;
  149. goto err;
  150. }
  151. spin_lock_init(&ppc4xx_gc->lock);
  152. mm_gc = &ppc4xx_gc->mm_gc;
  153. gc = &mm_gc->gc;
  154. gc->ngpio = 32;
  155. gc->direction_input = ppc4xx_gpio_dir_in;
  156. gc->direction_output = ppc4xx_gpio_dir_out;
  157. gc->get = ppc4xx_gpio_get;
  158. gc->set = ppc4xx_gpio_set;
  159. ret = of_mm_gpiochip_add_data(np, mm_gc, ppc4xx_gc);
  160. if (ret)
  161. goto err;
  162. continue;
  163. err:
  164. pr_err("%s: registration failed with status %d\n",
  165. np->full_name, ret);
  166. kfree(ppc4xx_gc);
  167. /* try others anyway */
  168. }
  169. return 0;
  170. }
  171. arch_initcall(ppc4xx_add_gpiochips);