gpio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * arch/arm/mach-orion5x/gpio.c
  3. *
  4. * GPIO functions for Marvell Orion System On Chip
  5. *
  6. * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/bitops.h>
  17. #include <linux/io.h>
  18. #include <asm/gpio.h>
  19. #include <mach/orion5x.h>
  20. #include "common.h"
  21. static DEFINE_SPINLOCK(gpio_lock);
  22. static unsigned long gpio_valid[BITS_TO_LONGS(GPIO_MAX)];
  23. static const char *gpio_label[GPIO_MAX]; /* non null for allocated GPIOs */
  24. void __init orion5x_gpio_set_valid(unsigned pin, int valid)
  25. {
  26. if (valid)
  27. __set_bit(pin, gpio_valid);
  28. else
  29. __clear_bit(pin, gpio_valid);
  30. }
  31. /*
  32. * GENERIC_GPIO primitives
  33. */
  34. int gpio_direction_input(unsigned pin)
  35. {
  36. unsigned long flags;
  37. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  38. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  39. return -EINVAL;
  40. }
  41. spin_lock_irqsave(&gpio_lock, flags);
  42. /*
  43. * Some callers might have not used the gpio_request(),
  44. * so flag this pin as requested now.
  45. */
  46. if (!gpio_label[pin])
  47. gpio_label[pin] = "?";
  48. orion5x_setbits(GPIO_IO_CONF, 1 << pin);
  49. spin_unlock_irqrestore(&gpio_lock, flags);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(gpio_direction_input);
  53. int gpio_direction_output(unsigned pin, int value)
  54. {
  55. unsigned long flags;
  56. int mask;
  57. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  58. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  59. return -EINVAL;
  60. }
  61. spin_lock_irqsave(&gpio_lock, flags);
  62. /*
  63. * Some callers might have not used the gpio_request(),
  64. * so flag this pin as requested now.
  65. */
  66. if (!gpio_label[pin])
  67. gpio_label[pin] = "?";
  68. mask = 1 << pin;
  69. orion5x_clrbits(GPIO_BLINK_EN, mask);
  70. if (value)
  71. orion5x_setbits(GPIO_OUT, mask);
  72. else
  73. orion5x_clrbits(GPIO_OUT, mask);
  74. orion5x_clrbits(GPIO_IO_CONF, mask);
  75. spin_unlock_irqrestore(&gpio_lock, flags);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(gpio_direction_output);
  79. int gpio_get_value(unsigned pin)
  80. {
  81. int val, mask = 1 << pin;
  82. if (readl(GPIO_IO_CONF) & mask)
  83. val = readl(GPIO_DATA_IN) ^ readl(GPIO_IN_POL);
  84. else
  85. val = readl(GPIO_OUT);
  86. return val & mask;
  87. }
  88. EXPORT_SYMBOL(gpio_get_value);
  89. void gpio_set_value(unsigned pin, int value)
  90. {
  91. unsigned long flags;
  92. int mask = 1 << pin;
  93. spin_lock_irqsave(&gpio_lock, flags);
  94. orion5x_clrbits(GPIO_BLINK_EN, mask);
  95. if (value)
  96. orion5x_setbits(GPIO_OUT, mask);
  97. else
  98. orion5x_clrbits(GPIO_OUT, mask);
  99. spin_unlock_irqrestore(&gpio_lock, flags);
  100. }
  101. EXPORT_SYMBOL(gpio_set_value);
  102. void orion5x_gpio_set_blink(unsigned pin, int blink)
  103. {
  104. unsigned long flags;
  105. int mask = 1 << pin;
  106. spin_lock_irqsave(&gpio_lock, flags);
  107. orion5x_clrbits(GPIO_OUT, mask);
  108. if (blink)
  109. orion5x_setbits(GPIO_BLINK_EN, mask);
  110. else
  111. orion5x_clrbits(GPIO_BLINK_EN, mask);
  112. spin_unlock_irqrestore(&gpio_lock, flags);
  113. }
  114. EXPORT_SYMBOL(orion5x_gpio_set_blink);
  115. int gpio_request(unsigned pin, const char *label)
  116. {
  117. int ret = 0;
  118. unsigned long flags;
  119. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  120. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  121. return -EINVAL;
  122. }
  123. spin_lock_irqsave(&gpio_lock, flags);
  124. if (gpio_label[pin]) {
  125. pr_debug("%s: GPIO %d already used as %s\n",
  126. __func__, pin, gpio_label[pin]);
  127. ret = -EBUSY;
  128. } else
  129. gpio_label[pin] = label ? label : "?";
  130. spin_unlock_irqrestore(&gpio_lock, flags);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL(gpio_request);
  134. void gpio_free(unsigned pin)
  135. {
  136. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  137. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  138. return;
  139. }
  140. if (!gpio_label[pin])
  141. pr_warning("%s: GPIO %d already freed\n", __func__, pin);
  142. else
  143. gpio_label[pin] = NULL;
  144. }
  145. EXPORT_SYMBOL(gpio_free);
  146. /* Debug helper */
  147. void gpio_display(void)
  148. {
  149. int i;
  150. for (i = 0; i < GPIO_MAX; i++) {
  151. printk(KERN_DEBUG "Pin-%d: ", i);
  152. if (!test_bit(i, gpio_valid)) {
  153. printk("non-GPIO\n");
  154. } else if (!gpio_label[i]) {
  155. printk("GPIO, free\n");
  156. } else {
  157. printk("GPIO, used by %s, ", gpio_label[i]);
  158. if (readl(GPIO_IO_CONF) & (1 << i)) {
  159. printk("input, active %s, level %s, edge %s\n",
  160. ((readl(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
  161. ((readl(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
  162. ((readl(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
  163. } else {
  164. printk("output, val=%d\n", (readl(GPIO_OUT) >> i) & 1);
  165. }
  166. }
  167. }
  168. printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
  169. MPP_0_7_CTRL, readl(MPP_0_7_CTRL));
  170. printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
  171. MPP_8_15_CTRL, readl(MPP_8_15_CTRL));
  172. printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
  173. MPP_16_19_CTRL, readl(MPP_16_19_CTRL));
  174. printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
  175. MPP_DEV_CTRL, readl(MPP_DEV_CTRL));
  176. printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
  177. GPIO_OUT, readl(GPIO_OUT));
  178. printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
  179. GPIO_IO_CONF, readl(GPIO_IO_CONF));
  180. printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
  181. GPIO_BLINK_EN, readl(GPIO_BLINK_EN));
  182. printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
  183. GPIO_IN_POL, readl(GPIO_IN_POL));
  184. printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
  185. GPIO_DATA_IN, readl(GPIO_DATA_IN));
  186. printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
  187. GPIO_LEVEL_MASK, readl(GPIO_LEVEL_MASK));
  188. printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
  189. GPIO_EDGE_CAUSE, readl(GPIO_EDGE_CAUSE));
  190. printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
  191. GPIO_EDGE_MASK, readl(GPIO_EDGE_MASK));
  192. }