gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * arch/arm/mach-orion/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 <asm/gpio.h>
  18. #include <asm/arch/orion.h>
  19. #include "common.h"
  20. static DEFINE_SPINLOCK(gpio_lock);
  21. static unsigned long gpio_valid[BITS_TO_LONGS(GPIO_MAX)];
  22. static const char *gpio_label[GPIO_MAX]; /* non null for allocated GPIOs */
  23. void __init orion_gpio_set_valid_pins(u32 pins)
  24. {
  25. gpio_valid[0] = pins;
  26. }
  27. /*
  28. * GENERIC_GPIO primitives
  29. */
  30. int gpio_direction_input(unsigned pin)
  31. {
  32. unsigned long flags;
  33. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  34. pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
  35. return -EINVAL;
  36. }
  37. spin_lock_irqsave(&gpio_lock, flags);
  38. /*
  39. * Some callers might have not used the gpio_request(),
  40. * so flag this pin as requested now.
  41. */
  42. if (!gpio_label[pin])
  43. gpio_label[pin] = "?";
  44. orion_setbits(GPIO_IO_CONF, 1 << pin);
  45. spin_unlock_irqrestore(&gpio_lock, flags);
  46. return 0;
  47. }
  48. EXPORT_SYMBOL(gpio_direction_input);
  49. int gpio_direction_output(unsigned pin, int value)
  50. {
  51. unsigned long flags;
  52. int mask;
  53. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  54. pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
  55. return -EINVAL;
  56. }
  57. spin_lock_irqsave(&gpio_lock, flags);
  58. /*
  59. * Some callers might have not used the gpio_request(),
  60. * so flag this pin as requested now.
  61. */
  62. if (!gpio_label[pin])
  63. gpio_label[pin] = "?";
  64. mask = 1 << pin;
  65. if (value)
  66. orion_setbits(GPIO_OUT, mask);
  67. else
  68. orion_clrbits(GPIO_OUT, mask);
  69. orion_clrbits(GPIO_IO_CONF, mask);
  70. spin_unlock_irqrestore(&gpio_lock, flags);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(gpio_direction_output);
  74. int gpio_get_value(unsigned pin)
  75. {
  76. int val, mask = 1 << pin;
  77. if (orion_read(GPIO_IO_CONF) & mask)
  78. val = orion_read(GPIO_DATA_IN);
  79. else
  80. val = orion_read(GPIO_OUT);
  81. return val & mask;
  82. }
  83. EXPORT_SYMBOL(gpio_get_value);
  84. void gpio_set_value(unsigned pin, int value)
  85. {
  86. unsigned long flags;
  87. int mask = 1 << pin;
  88. spin_lock_irqsave(&gpio_lock, flags);
  89. if (value)
  90. orion_setbits(GPIO_OUT, mask);
  91. else
  92. orion_clrbits(GPIO_OUT, mask);
  93. spin_unlock_irqrestore(&gpio_lock, flags);
  94. }
  95. EXPORT_SYMBOL(gpio_set_value);
  96. int gpio_request(unsigned pin, const char *label)
  97. {
  98. int ret = 0;
  99. unsigned long flags;
  100. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  101. pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
  102. return -EINVAL;
  103. }
  104. spin_lock_irqsave(&gpio_lock, flags);
  105. if (gpio_label[pin]) {
  106. pr_debug("%s: GPIO %d already used as %s\n",
  107. __FUNCTION__, pin, gpio_label[pin]);
  108. ret = -EBUSY;
  109. } else
  110. gpio_label[pin] = label ? label : "?";
  111. spin_unlock_irqrestore(&gpio_lock, flags);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL(gpio_request);
  115. void gpio_free(unsigned pin)
  116. {
  117. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  118. pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
  119. return;
  120. }
  121. if (!gpio_label[pin])
  122. pr_warning("%s: GPIO %d already freed\n", __FUNCTION__, pin);
  123. else
  124. gpio_label[pin] = NULL;
  125. }
  126. EXPORT_SYMBOL(gpio_free);
  127. /* Debug helper */
  128. void gpio_display(void)
  129. {
  130. int i;
  131. for (i = 0; i < GPIO_MAX; i++) {
  132. printk(KERN_DEBUG "Pin-%d: ", i);
  133. if (!test_bit(i, gpio_valid)) {
  134. printk("non-GPIO\n");
  135. } else if (!gpio_label[i]) {
  136. printk("GPIO, free\n");
  137. } else {
  138. printk("GPIO, used by %s, ", gpio_label[i]);
  139. if (orion_read(GPIO_IO_CONF) & (1 << i)) {
  140. printk("input, active %s, level %s, edge %s\n",
  141. ((orion_read(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
  142. ((orion_read(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
  143. ((orion_read(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
  144. } else {
  145. printk("output, val=%d\n", (orion_read(GPIO_OUT) >> i) & 1);
  146. }
  147. }
  148. }
  149. printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
  150. MPP_0_7_CTRL, orion_read(MPP_0_7_CTRL));
  151. printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
  152. MPP_8_15_CTRL, orion_read(MPP_8_15_CTRL));
  153. printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
  154. MPP_16_19_CTRL, orion_read(MPP_16_19_CTRL));
  155. printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
  156. MPP_DEV_CTRL, orion_read(MPP_DEV_CTRL));
  157. printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
  158. GPIO_OUT, orion_read(GPIO_OUT));
  159. printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
  160. GPIO_IO_CONF, orion_read(GPIO_IO_CONF));
  161. printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
  162. GPIO_BLINK_EN, orion_read(GPIO_BLINK_EN));
  163. printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
  164. GPIO_IN_POL, orion_read(GPIO_IN_POL));
  165. printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
  166. GPIO_DATA_IN, orion_read(GPIO_DATA_IN));
  167. printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
  168. GPIO_LEVEL_MASK, orion_read(GPIO_LEVEL_MASK));
  169. printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
  170. GPIO_EDGE_CAUSE, orion_read(GPIO_EDGE_CAUSE));
  171. printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
  172. GPIO_EDGE_MASK, orion_read(GPIO_EDGE_MASK));
  173. }