gpio.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * arch/arm/plat-orion/gpio.c
  3. *
  4. * Marvell Orion SoC GPIO handling.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/bitops.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. static DEFINE_SPINLOCK(gpio_lock);
  19. static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
  20. static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
  21. static inline void __set_direction(unsigned pin, int input)
  22. {
  23. u32 u;
  24. u = readl(GPIO_IO_CONF(pin));
  25. if (input)
  26. u |= 1 << (pin & 31);
  27. else
  28. u &= ~(1 << (pin & 31));
  29. writel(u, GPIO_IO_CONF(pin));
  30. }
  31. static void __set_level(unsigned pin, int high)
  32. {
  33. u32 u;
  34. u = readl(GPIO_OUT(pin));
  35. if (high)
  36. u |= 1 << (pin & 31);
  37. else
  38. u &= ~(1 << (pin & 31));
  39. writel(u, GPIO_OUT(pin));
  40. }
  41. static inline void __set_blinking(unsigned pin, int blink)
  42. {
  43. u32 u;
  44. u = readl(GPIO_BLINK_EN(pin));
  45. if (blink)
  46. u |= 1 << (pin & 31);
  47. else
  48. u &= ~(1 << (pin & 31));
  49. writel(u, GPIO_BLINK_EN(pin));
  50. }
  51. static inline int orion_gpio_is_valid(unsigned pin, int mode)
  52. {
  53. if (pin < GPIO_MAX) {
  54. if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input))
  55. goto err_out;
  56. if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output))
  57. goto err_out;
  58. return true;
  59. }
  60. err_out:
  61. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  62. return false;
  63. }
  64. /*
  65. * GENERIC_GPIO primitives.
  66. */
  67. static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  68. {
  69. unsigned long flags;
  70. if (!orion_gpio_is_valid(pin, GPIO_INPUT_OK))
  71. return -EINVAL;
  72. spin_lock_irqsave(&gpio_lock, flags);
  73. /* Configure GPIO direction. */
  74. __set_direction(pin, 1);
  75. spin_unlock_irqrestore(&gpio_lock, flags);
  76. return 0;
  77. }
  78. static int orion_gpio_get_value(struct gpio_chip *chip, unsigned pin)
  79. {
  80. int val;
  81. if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
  82. val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
  83. else
  84. val = readl(GPIO_OUT(pin));
  85. return (val >> (pin & 31)) & 1;
  86. }
  87. static int orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin,
  88. int value)
  89. {
  90. unsigned long flags;
  91. if (!orion_gpio_is_valid(pin, GPIO_OUTPUT_OK))
  92. return -EINVAL;
  93. spin_lock_irqsave(&gpio_lock, flags);
  94. /* Disable blinking. */
  95. __set_blinking(pin, 0);
  96. /* Configure GPIO output value. */
  97. __set_level(pin, value);
  98. /* Configure GPIO direction. */
  99. __set_direction(pin, 0);
  100. spin_unlock_irqrestore(&gpio_lock, flags);
  101. return 0;
  102. }
  103. static void orion_gpio_set_value(struct gpio_chip *chip, unsigned pin,
  104. int value)
  105. {
  106. unsigned long flags;
  107. spin_lock_irqsave(&gpio_lock, flags);
  108. /* Configure GPIO output value. */
  109. __set_level(pin, value);
  110. spin_unlock_irqrestore(&gpio_lock, flags);
  111. }
  112. static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
  113. {
  114. if (orion_gpio_is_valid(pin, GPIO_INPUT_OK) ||
  115. orion_gpio_is_valid(pin, GPIO_OUTPUT_OK))
  116. return 0;
  117. return -EINVAL;
  118. }
  119. static struct gpio_chip orion_gpiochip = {
  120. .label = "orion_gpio",
  121. .direction_input = orion_gpio_direction_input,
  122. .get = orion_gpio_get_value,
  123. .direction_output = orion_gpio_direction_output,
  124. .set = orion_gpio_set_value,
  125. .request = orion_gpio_request,
  126. .base = 0,
  127. .ngpio = GPIO_MAX,
  128. .can_sleep = 0,
  129. };
  130. void __init orion_gpio_init(void)
  131. {
  132. gpiochip_add(&orion_gpiochip);
  133. }
  134. /*
  135. * Orion-specific GPIO API extensions.
  136. */
  137. void __init orion_gpio_set_unused(unsigned pin)
  138. {
  139. /* Configure as output, drive low. */
  140. __set_level(pin, 0);
  141. __set_direction(pin, 0);
  142. }
  143. void __init orion_gpio_set_valid(unsigned pin, int mode)
  144. {
  145. if (mode == 1)
  146. mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
  147. if (mode & GPIO_INPUT_OK)
  148. __set_bit(pin, gpio_valid_input);
  149. else
  150. __clear_bit(pin, gpio_valid_input);
  151. if (mode & GPIO_OUTPUT_OK)
  152. __set_bit(pin, gpio_valid_output);
  153. else
  154. __clear_bit(pin, gpio_valid_output);
  155. }
  156. void orion_gpio_set_blink(unsigned pin, int blink)
  157. {
  158. unsigned long flags;
  159. spin_lock_irqsave(&gpio_lock, flags);
  160. /* Set output value to zero. */
  161. __set_level(pin, 0);
  162. /* Set blinking. */
  163. __set_blinking(pin, blink);
  164. spin_unlock_irqrestore(&gpio_lock, flags);
  165. }
  166. EXPORT_SYMBOL(orion_gpio_set_blink);
  167. /*****************************************************************************
  168. * Orion GPIO IRQ
  169. *
  170. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  171. * value of the line or the opposite value.
  172. *
  173. * Level IRQ handlers: DATA_IN is used directly as cause register.
  174. * Interrupt are masked by LEVEL_MASK registers.
  175. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  176. * Interrupt are masked by EDGE_MASK registers.
  177. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  178. * the polarity to catch the next line transaction.
  179. * This is a race condition that might not perfectly
  180. * work on some use cases.
  181. *
  182. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  183. * cause register.
  184. *
  185. * EDGE cause mask
  186. * data-in /--------| |-----| |----\
  187. * -----| |----- ---- to main cause reg
  188. * X \----------------| |----/
  189. * polarity LEVEL mask
  190. *
  191. ****************************************************************************/
  192. static void gpio_irq_ack(u32 irq)
  193. {
  194. int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
  195. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  196. int pin = irq_to_gpio(irq);
  197. writel(~(1 << (pin & 31)), GPIO_EDGE_CAUSE(pin));
  198. }
  199. }
  200. static void gpio_irq_mask(u32 irq)
  201. {
  202. int pin = irq_to_gpio(irq);
  203. int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
  204. u32 reg = (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) ?
  205. GPIO_EDGE_MASK(pin) : GPIO_LEVEL_MASK(pin);
  206. u32 u = readl(reg);
  207. u &= ~(1 << (pin & 31));
  208. writel(u, reg);
  209. }
  210. static void gpio_irq_unmask(u32 irq)
  211. {
  212. int pin = irq_to_gpio(irq);
  213. int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
  214. u32 reg = (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) ?
  215. GPIO_EDGE_MASK(pin) : GPIO_LEVEL_MASK(pin);
  216. u32 u = readl(reg);
  217. u |= 1 << (pin & 31);
  218. writel(u, reg);
  219. }
  220. static int gpio_irq_set_type(u32 irq, u32 type)
  221. {
  222. int pin = irq_to_gpio(irq);
  223. struct irq_desc *desc;
  224. u32 u;
  225. u = readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31));
  226. if (!u) {
  227. printk(KERN_ERR "orion gpio_irq_set_type failed "
  228. "(irq %d, pin %d).\n", irq, pin);
  229. return -EINVAL;
  230. }
  231. desc = irq_desc + irq;
  232. /*
  233. * Set edge/level type.
  234. */
  235. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  236. desc->handle_irq = handle_edge_irq;
  237. } else if (type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  238. desc->handle_irq = handle_level_irq;
  239. } else {
  240. printk(KERN_ERR "failed to set irq=%d (type=%d)\n", irq, type);
  241. return -EINVAL;
  242. }
  243. /*
  244. * Configure interrupt polarity.
  245. */
  246. if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
  247. u = readl(GPIO_IN_POL(pin));
  248. u &= ~(1 << (pin & 31));
  249. writel(u, GPIO_IN_POL(pin));
  250. } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
  251. u = readl(GPIO_IN_POL(pin));
  252. u |= 1 << (pin & 31);
  253. writel(u, GPIO_IN_POL(pin));
  254. } else if (type == IRQ_TYPE_EDGE_BOTH) {
  255. u32 v;
  256. v = readl(GPIO_IN_POL(pin)) ^ readl(GPIO_DATA_IN(pin));
  257. /*
  258. * set initial polarity based on current input level
  259. */
  260. u = readl(GPIO_IN_POL(pin));
  261. if (v & (1 << (pin & 31)))
  262. u |= 1 << (pin & 31); /* falling */
  263. else
  264. u &= ~(1 << (pin & 31)); /* rising */
  265. writel(u, GPIO_IN_POL(pin));
  266. }
  267. desc->status = (desc->status & ~IRQ_TYPE_SENSE_MASK) | type;
  268. return 0;
  269. }
  270. struct irq_chip orion_gpio_irq_chip = {
  271. .name = "orion_gpio_irq",
  272. .ack = gpio_irq_ack,
  273. .mask = gpio_irq_mask,
  274. .unmask = gpio_irq_unmask,
  275. .set_type = gpio_irq_set_type,
  276. };
  277. void orion_gpio_irq_handler(int pinoff)
  278. {
  279. u32 cause;
  280. int pin;
  281. cause = readl(GPIO_DATA_IN(pinoff)) & readl(GPIO_LEVEL_MASK(pinoff));
  282. cause |= readl(GPIO_EDGE_CAUSE(pinoff)) & readl(GPIO_EDGE_MASK(pinoff));
  283. for (pin = pinoff; pin < pinoff + 8; pin++) {
  284. int irq = gpio_to_irq(pin);
  285. struct irq_desc *desc = irq_desc + irq;
  286. if (!(cause & (1 << (pin & 31))))
  287. continue;
  288. if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  289. /* Swap polarity (race with GPIO line) */
  290. u32 polarity;
  291. polarity = readl(GPIO_IN_POL(pin));
  292. polarity ^= 1 << (pin & 31);
  293. writel(polarity, GPIO_IN_POL(pin));
  294. }
  295. desc_handle_irq(irq, desc);
  296. }
  297. }