gpio.c 5.6 KB

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