gpio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * arch/arm/mach-ns9xxx/gpio.c
  3. *
  4. * Copyright (C) 2006,2007 by Digi International Inc.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/compiler.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <linux/bitops.h>
  17. #include <mach/gpio.h>
  18. #include <mach/processor.h>
  19. #include <mach/processor-ns9360.h>
  20. #include <asm/bug.h>
  21. #include <asm/types.h>
  22. #include "gpio-ns9360.h"
  23. #if defined(CONFIG_PROCESSOR_NS9360)
  24. #define GPIO_MAX 72
  25. #elif defined(CONFIG_PROCESSOR_NS9750)
  26. #define GPIO_MAX 49
  27. #endif
  28. /* protects BBU_GCONFx and BBU_GCTRLx */
  29. static spinlock_t gpio_lock = __SPIN_LOCK_UNLOCKED(gpio_lock);
  30. /* only access gpiores with atomic ops */
  31. static DECLARE_BITMAP(gpiores, GPIO_MAX + 1);
  32. static inline int ns9xxx_valid_gpio(unsigned gpio)
  33. {
  34. #if defined(CONFIG_PROCESSOR_NS9360)
  35. if (processor_is_ns9360())
  36. return gpio <= 72;
  37. else
  38. #endif
  39. #if defined(CONFIG_PROCESSOR_NS9750)
  40. if (processor_is_ns9750())
  41. return gpio <= 49;
  42. else
  43. #endif
  44. {
  45. BUG();
  46. return 0;
  47. }
  48. }
  49. int gpio_request(unsigned gpio, const char *label)
  50. {
  51. if (likely(ns9xxx_valid_gpio(gpio)))
  52. return test_and_set_bit(gpio, gpiores) ? -EBUSY : 0;
  53. else
  54. return -EINVAL;
  55. }
  56. EXPORT_SYMBOL(gpio_request);
  57. void gpio_free(unsigned gpio)
  58. {
  59. might_sleep();
  60. clear_bit(gpio, gpiores);
  61. return;
  62. }
  63. EXPORT_SYMBOL(gpio_free);
  64. int gpio_direction_input(unsigned gpio)
  65. {
  66. if (likely(ns9xxx_valid_gpio(gpio))) {
  67. int ret = -EINVAL;
  68. unsigned long flags;
  69. spin_lock_irqsave(&gpio_lock, flags);
  70. #if defined(CONFIG_PROCESSOR_NS9360)
  71. if (processor_is_ns9360())
  72. ret = __ns9360_gpio_configure(gpio, 0, 0, 3);
  73. else
  74. #endif
  75. BUG();
  76. spin_unlock_irqrestore(&gpio_lock, flags);
  77. return ret;
  78. } else
  79. return -EINVAL;
  80. }
  81. EXPORT_SYMBOL(gpio_direction_input);
  82. int gpio_direction_output(unsigned gpio, int value)
  83. {
  84. if (likely(ns9xxx_valid_gpio(gpio))) {
  85. int ret = -EINVAL;
  86. unsigned long flags;
  87. gpio_set_value(gpio, value);
  88. spin_lock_irqsave(&gpio_lock, flags);
  89. #if defined(CONFIG_PROCESSOR_NS9360)
  90. if (processor_is_ns9360())
  91. ret = __ns9360_gpio_configure(gpio, 1, 0, 3);
  92. else
  93. #endif
  94. BUG();
  95. spin_unlock_irqrestore(&gpio_lock, flags);
  96. return ret;
  97. } else
  98. return -EINVAL;
  99. }
  100. EXPORT_SYMBOL(gpio_direction_output);
  101. int gpio_get_value(unsigned gpio)
  102. {
  103. #if defined(CONFIG_PROCESSOR_NS9360)
  104. if (processor_is_ns9360())
  105. return ns9360_gpio_get_value(gpio);
  106. else
  107. #endif
  108. {
  109. BUG();
  110. return -EINVAL;
  111. }
  112. }
  113. EXPORT_SYMBOL(gpio_get_value);
  114. void gpio_set_value(unsigned gpio, int value)
  115. {
  116. unsigned long flags;
  117. spin_lock_irqsave(&gpio_lock, flags);
  118. #if defined(CONFIG_PROCESSOR_NS9360)
  119. if (processor_is_ns9360())
  120. ns9360_gpio_set_value(gpio, value);
  121. else
  122. #endif
  123. BUG();
  124. spin_unlock_irqrestore(&gpio_lock, flags);
  125. }
  126. EXPORT_SYMBOL(gpio_set_value);