gpio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. orion_clrbits(GPIO_BLINK_EN, mask);
  66. if (value)
  67. orion_setbits(GPIO_OUT, mask);
  68. else
  69. orion_clrbits(GPIO_OUT, mask);
  70. orion_clrbits(GPIO_IO_CONF, mask);
  71. spin_unlock_irqrestore(&gpio_lock, flags);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(gpio_direction_output);
  75. int gpio_get_value(unsigned pin)
  76. {
  77. int val, mask = 1 << pin;
  78. if (orion_read(GPIO_IO_CONF) & mask)
  79. val = orion_read(GPIO_DATA_IN) ^ orion_read(GPIO_IN_POL);
  80. else
  81. val = orion_read(GPIO_OUT);
  82. return val & mask;
  83. }
  84. EXPORT_SYMBOL(gpio_get_value);
  85. void gpio_set_value(unsigned pin, int value)
  86. {
  87. unsigned long flags;
  88. int mask = 1 << pin;
  89. spin_lock_irqsave(&gpio_lock, flags);
  90. orion_clrbits(GPIO_BLINK_EN, mask);
  91. if (value)
  92. orion_setbits(GPIO_OUT, mask);
  93. else
  94. orion_clrbits(GPIO_OUT, mask);
  95. spin_unlock_irqrestore(&gpio_lock, flags);
  96. }
  97. EXPORT_SYMBOL(gpio_set_value);
  98. void orion_gpio_set_blink(unsigned pin, int blink)
  99. {
  100. unsigned long flags;
  101. int mask = 1 << pin;
  102. spin_lock_irqsave(&gpio_lock, flags);
  103. orion_clrbits(GPIO_OUT, mask);
  104. if (blink)
  105. orion_setbits(GPIO_BLINK_EN, mask);
  106. else
  107. orion_clrbits(GPIO_BLINK_EN, mask);
  108. spin_unlock_irqrestore(&gpio_lock, flags);
  109. }
  110. EXPORT_SYMBOL(orion_gpio_set_blink);
  111. int gpio_request(unsigned pin, const char *label)
  112. {
  113. int ret = 0;
  114. unsigned long flags;
  115. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  116. pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
  117. return -EINVAL;
  118. }
  119. spin_lock_irqsave(&gpio_lock, flags);
  120. if (gpio_label[pin]) {
  121. pr_debug("%s: GPIO %d already used as %s\n",
  122. __FUNCTION__, pin, gpio_label[pin]);
  123. ret = -EBUSY;
  124. } else
  125. gpio_label[pin] = label ? label : "?";
  126. spin_unlock_irqrestore(&gpio_lock, flags);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL(gpio_request);
  130. void gpio_free(unsigned pin)
  131. {
  132. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  133. pr_debug("%s: invalid GPIO %d\n", __FUNCTION__, pin);
  134. return;
  135. }
  136. if (!gpio_label[pin])
  137. pr_warning("%s: GPIO %d already freed\n", __FUNCTION__, pin);
  138. else
  139. gpio_label[pin] = NULL;
  140. }
  141. EXPORT_SYMBOL(gpio_free);
  142. /* Debug helper */
  143. void gpio_display(void)
  144. {
  145. int i;
  146. for (i = 0; i < GPIO_MAX; i++) {
  147. printk(KERN_DEBUG "Pin-%d: ", i);
  148. if (!test_bit(i, gpio_valid)) {
  149. printk("non-GPIO\n");
  150. } else if (!gpio_label[i]) {
  151. printk("GPIO, free\n");
  152. } else {
  153. printk("GPIO, used by %s, ", gpio_label[i]);
  154. if (orion_read(GPIO_IO_CONF) & (1 << i)) {
  155. printk("input, active %s, level %s, edge %s\n",
  156. ((orion_read(GPIO_IN_POL) >> i) & 1) ? "low" : "high",
  157. ((orion_read(GPIO_LEVEL_MASK) >> i) & 1) ? "enabled" : "masked",
  158. ((orion_read(GPIO_EDGE_MASK) >> i) & 1) ? "enabled" : "masked");
  159. } else {
  160. printk("output, val=%d\n", (orion_read(GPIO_OUT) >> i) & 1);
  161. }
  162. }
  163. }
  164. printk(KERN_DEBUG "MPP_0_7_CTRL (0x%08x) = 0x%08x\n",
  165. MPP_0_7_CTRL, orion_read(MPP_0_7_CTRL));
  166. printk(KERN_DEBUG "MPP_8_15_CTRL (0x%08x) = 0x%08x\n",
  167. MPP_8_15_CTRL, orion_read(MPP_8_15_CTRL));
  168. printk(KERN_DEBUG "MPP_16_19_CTRL (0x%08x) = 0x%08x\n",
  169. MPP_16_19_CTRL, orion_read(MPP_16_19_CTRL));
  170. printk(KERN_DEBUG "MPP_DEV_CTRL (0x%08x) = 0x%08x\n",
  171. MPP_DEV_CTRL, orion_read(MPP_DEV_CTRL));
  172. printk(KERN_DEBUG "GPIO_OUT (0x%08x) = 0x%08x\n",
  173. GPIO_OUT, orion_read(GPIO_OUT));
  174. printk(KERN_DEBUG "GPIO_IO_CONF (0x%08x) = 0x%08x\n",
  175. GPIO_IO_CONF, orion_read(GPIO_IO_CONF));
  176. printk(KERN_DEBUG "GPIO_BLINK_EN (0x%08x) = 0x%08x\n",
  177. GPIO_BLINK_EN, orion_read(GPIO_BLINK_EN));
  178. printk(KERN_DEBUG "GPIO_IN_POL (0x%08x) = 0x%08x\n",
  179. GPIO_IN_POL, orion_read(GPIO_IN_POL));
  180. printk(KERN_DEBUG "GPIO_DATA_IN (0x%08x) = 0x%08x\n",
  181. GPIO_DATA_IN, orion_read(GPIO_DATA_IN));
  182. printk(KERN_DEBUG "GPIO_LEVEL_MASK (0x%08x) = 0x%08x\n",
  183. GPIO_LEVEL_MASK, orion_read(GPIO_LEVEL_MASK));
  184. printk(KERN_DEBUG "GPIO_EDGE_CAUSE (0x%08x) = 0x%08x\n",
  185. GPIO_EDGE_CAUSE, orion_read(GPIO_EDGE_CAUSE));
  186. printk(KERN_DEBUG "GPIO_EDGE_MASK (0x%08x) = 0x%08x\n",
  187. GPIO_EDGE_MASK, orion_read(GPIO_EDGE_MASK));
  188. }