gpio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <asm/gpio.h>
  18. static DEFINE_SPINLOCK(gpio_lock);
  19. static const char *gpio_label[GPIO_MAX]; /* non null for allocated GPIOs */
  20. static unsigned long gpio_valid[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. /*
  42. * GENERIC_GPIO primitives.
  43. */
  44. int gpio_direction_input(unsigned pin)
  45. {
  46. unsigned long flags;
  47. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  48. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  49. return -EINVAL;
  50. }
  51. spin_lock_irqsave(&gpio_lock, flags);
  52. /*
  53. * Some callers might not have used gpio_request(),
  54. * so flag this pin as requested now.
  55. */
  56. if (gpio_label[pin] == NULL)
  57. gpio_label[pin] = "?";
  58. /*
  59. * Configure GPIO direction.
  60. */
  61. __set_direction(pin, 1);
  62. spin_unlock_irqrestore(&gpio_lock, flags);
  63. return 0;
  64. }
  65. EXPORT_SYMBOL(gpio_direction_input);
  66. int gpio_direction_output(unsigned pin, int value)
  67. {
  68. unsigned long flags;
  69. u32 u;
  70. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  71. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  72. return -EINVAL;
  73. }
  74. spin_lock_irqsave(&gpio_lock, flags);
  75. /*
  76. * Some callers might not have used gpio_request(),
  77. * so flag this pin as requested now.
  78. */
  79. if (gpio_label[pin] == NULL)
  80. gpio_label[pin] = "?";
  81. /*
  82. * Disable blinking.
  83. */
  84. u = readl(GPIO_BLINK_EN(pin));
  85. u &= ~(1 << (pin & 31));
  86. writel(u, GPIO_BLINK_EN(pin));
  87. /*
  88. * Configure GPIO output value.
  89. */
  90. __set_level(pin, value);
  91. /*
  92. * Configure GPIO direction.
  93. */
  94. __set_direction(pin, 0);
  95. spin_unlock_irqrestore(&gpio_lock, flags);
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(gpio_direction_output);
  99. int gpio_get_value(unsigned pin)
  100. {
  101. int val;
  102. if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
  103. val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
  104. else
  105. val = readl(GPIO_OUT(pin));
  106. return (val >> (pin & 31)) & 1;
  107. }
  108. EXPORT_SYMBOL(gpio_get_value);
  109. void gpio_set_value(unsigned pin, int value)
  110. {
  111. unsigned long flags;
  112. u32 u;
  113. spin_lock_irqsave(&gpio_lock, flags);
  114. /*
  115. * Disable blinking.
  116. */
  117. u = readl(GPIO_BLINK_EN(pin));
  118. u &= ~(1 << (pin & 31));
  119. writel(u, GPIO_BLINK_EN(pin));
  120. /*
  121. * Configure GPIO output value.
  122. */
  123. __set_level(pin, value);
  124. spin_unlock_irqrestore(&gpio_lock, flags);
  125. }
  126. EXPORT_SYMBOL(gpio_set_value);
  127. int gpio_request(unsigned pin, const char *label)
  128. {
  129. unsigned long flags;
  130. int ret;
  131. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  132. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  133. return -EINVAL;
  134. }
  135. spin_lock_irqsave(&gpio_lock, flags);
  136. if (gpio_label[pin] == NULL) {
  137. gpio_label[pin] = label ? label : "?";
  138. ret = 0;
  139. } else {
  140. pr_debug("%s: GPIO %d already used as %s\n",
  141. __func__, pin, gpio_label[pin]);
  142. ret = -EBUSY;
  143. }
  144. spin_unlock_irqrestore(&gpio_lock, flags);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL(gpio_request);
  148. void gpio_free(unsigned pin)
  149. {
  150. if (pin >= GPIO_MAX || !test_bit(pin, gpio_valid)) {
  151. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  152. return;
  153. }
  154. if (gpio_label[pin] == NULL)
  155. pr_warning("%s: GPIO %d already freed\n", __func__, pin);
  156. else
  157. gpio_label[pin] = NULL;
  158. }
  159. EXPORT_SYMBOL(gpio_free);
  160. /*
  161. * Orion-specific GPIO API extensions.
  162. */
  163. void __init orion_gpio_set_unused(unsigned pin)
  164. {
  165. /*
  166. * Configure as output, drive low.
  167. */
  168. __set_level(pin, 0);
  169. __set_direction(pin, 0);
  170. }
  171. void __init orion_gpio_set_valid(unsigned pin, int valid)
  172. {
  173. if (valid)
  174. __set_bit(pin, gpio_valid);
  175. else
  176. __clear_bit(pin, gpio_valid);
  177. }
  178. void orion_gpio_set_blink(unsigned pin, int blink)
  179. {
  180. unsigned long flags;
  181. u32 u;
  182. spin_lock_irqsave(&gpio_lock, flags);
  183. /*
  184. * Set output value to zero.
  185. */
  186. __set_level(pin, 0);
  187. u = readl(GPIO_BLINK_EN(pin));
  188. if (blink)
  189. u |= 1 << (pin & 31);
  190. else
  191. u &= ~(1 << (pin & 31));
  192. writel(u, GPIO_BLINK_EN(pin));
  193. spin_unlock_irqrestore(&gpio_lock, flags);
  194. }
  195. EXPORT_SYMBOL(orion_gpio_set_blink);
  196. /*****************************************************************************
  197. * Orion GPIO IRQ
  198. *
  199. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  200. * value of the line or the opposite value.
  201. *
  202. * Level IRQ handlers: DATA_IN is used directly as cause register.
  203. * Interrupt are masked by LEVEL_MASK registers.
  204. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  205. * Interrupt are masked by EDGE_MASK registers.
  206. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  207. * the polarity to catch the next line transaction.
  208. * This is a race condition that might not perfectly
  209. * work on some use cases.
  210. *
  211. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  212. * cause register.
  213. *
  214. * EDGE cause mask
  215. * data-in /--------| |-----| |----\
  216. * -----| |----- ---- to main cause reg
  217. * X \----------------| |----/
  218. * polarity LEVEL mask
  219. *
  220. ****************************************************************************/
  221. static void gpio_irq_ack(u32 irq)
  222. {
  223. int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
  224. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  225. int pin = irq_to_gpio(irq);
  226. writel(~(1 << (pin & 31)), GPIO_EDGE_CAUSE(pin));
  227. }
  228. }
  229. static void gpio_irq_mask(u32 irq)
  230. {
  231. int pin = irq_to_gpio(irq);
  232. int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
  233. u32 reg = (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) ?
  234. GPIO_EDGE_MASK(pin) : GPIO_LEVEL_MASK(pin);
  235. u32 u = readl(reg);
  236. u &= ~(1 << (pin & 31));
  237. writel(u, reg);
  238. }
  239. static void gpio_irq_unmask(u32 irq)
  240. {
  241. int pin = irq_to_gpio(irq);
  242. int type = irq_desc[irq].status & IRQ_TYPE_SENSE_MASK;
  243. u32 reg = (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) ?
  244. GPIO_EDGE_MASK(pin) : GPIO_LEVEL_MASK(pin);
  245. u32 u = readl(reg);
  246. u |= 1 << (pin & 31);
  247. writel(u, reg);
  248. }
  249. static int gpio_irq_set_type(u32 irq, u32 type)
  250. {
  251. int pin = irq_to_gpio(irq);
  252. struct irq_desc *desc;
  253. u32 u;
  254. u = readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31));
  255. if (!u) {
  256. printk(KERN_ERR "orion gpio_irq_set_type failed "
  257. "(irq %d, pin %d).\n", irq, pin);
  258. return -EINVAL;
  259. }
  260. desc = irq_desc + irq;
  261. /*
  262. * Set edge/level type.
  263. */
  264. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  265. desc->handle_irq = handle_edge_irq;
  266. } else if (type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  267. desc->handle_irq = handle_level_irq;
  268. } else {
  269. printk(KERN_ERR "failed to set irq=%d (type=%d)\n", irq, type);
  270. return -EINVAL;
  271. }
  272. /*
  273. * Configure interrupt polarity.
  274. */
  275. if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
  276. u = readl(GPIO_IN_POL(pin));
  277. u &= ~(1 << (pin & 31));
  278. writel(u, GPIO_IN_POL(pin));
  279. } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
  280. u = readl(GPIO_IN_POL(pin));
  281. u |= 1 << (pin & 31);
  282. writel(u, GPIO_IN_POL(pin));
  283. } else if (type == IRQ_TYPE_EDGE_BOTH) {
  284. u32 v;
  285. v = readl(GPIO_IN_POL(pin)) ^ readl(GPIO_DATA_IN(pin));
  286. /*
  287. * set initial polarity based on current input level
  288. */
  289. u = readl(GPIO_IN_POL(pin));
  290. if (v & (1 << (pin & 31)))
  291. u |= 1 << (pin & 31); /* falling */
  292. else
  293. u &= ~(1 << (pin & 31)); /* rising */
  294. writel(u, GPIO_IN_POL(pin));
  295. }
  296. desc->status = (desc->status & ~IRQ_TYPE_SENSE_MASK) | type;
  297. return 0;
  298. }
  299. struct irq_chip orion_gpio_irq_chip = {
  300. .name = "orion_gpio",
  301. .ack = gpio_irq_ack,
  302. .mask = gpio_irq_mask,
  303. .unmask = gpio_irq_unmask,
  304. .set_type = gpio_irq_set_type,
  305. };
  306. void orion_gpio_irq_handler(int pinoff)
  307. {
  308. u32 cause;
  309. int pin;
  310. cause = readl(GPIO_DATA_IN(pinoff)) & readl(GPIO_LEVEL_MASK(pinoff));
  311. cause |= readl(GPIO_EDGE_CAUSE(pinoff)) & readl(GPIO_EDGE_MASK(pinoff));
  312. for (pin = pinoff; pin < pinoff + 8; pin++) {
  313. int irq = gpio_to_irq(pin);
  314. struct irq_desc *desc = irq_desc + irq;
  315. if (!(cause & (1 << (pin & 31))))
  316. continue;
  317. if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  318. /* Swap polarity (race with GPIO line) */
  319. u32 polarity;
  320. polarity = readl(GPIO_IN_POL(pin));
  321. polarity ^= 1 << (pin & 31);
  322. writel(polarity, GPIO_IN_POL(pin));
  323. }
  324. desc_handle_irq(irq, desc);
  325. }
  326. }