mfp-pxa2xx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * linux/arch/arm/mach-pxa/mfp-pxa2xx.c
  3. *
  4. * PXA2xx pin mux configuration support
  5. *
  6. * The GPIOs on PXA2xx can be configured as one of many alternate
  7. * functions, this is by concept samilar to the MFP configuration
  8. * on PXA3xx, what's more important, the low power pin state and
  9. * wakeup detection are also supported by the same framework.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/sysdev.h>
  19. #include <asm/arch/hardware.h>
  20. #include <asm/arch/pxa-regs.h>
  21. #include <asm/arch/pxa2xx-regs.h>
  22. #include <asm/arch/mfp-pxa2xx.h>
  23. #include "generic.h"
  24. #define PGSR(x) __REG2(0x40F00020, ((x) & 0x60) >> 3)
  25. #define PWER_WE35 (1 << 24)
  26. struct gpio_desc {
  27. unsigned valid : 1;
  28. unsigned can_wakeup : 1;
  29. unsigned keypad_gpio : 1;
  30. unsigned int mask; /* bit mask in PWER or PKWR */
  31. unsigned long config;
  32. };
  33. static struct gpio_desc gpio_desc[MFP_PIN_GPIO127 + 1];
  34. static int __mfp_config_lpm(unsigned gpio, unsigned long lpm)
  35. {
  36. unsigned mask = GPIO_bit(gpio);
  37. /* low power state */
  38. switch (lpm) {
  39. case MFP_LPM_DRIVE_HIGH:
  40. PGSR(gpio) |= mask;
  41. break;
  42. case MFP_LPM_DRIVE_LOW:
  43. PGSR(gpio) &= ~mask;
  44. break;
  45. case MFP_LPM_INPUT:
  46. break;
  47. default:
  48. pr_warning("%s: invalid low power state for GPIO%d\n",
  49. __func__, gpio);
  50. return -EINVAL;
  51. }
  52. return 0;
  53. }
  54. static int __mfp_config_gpio(unsigned gpio, unsigned long c)
  55. {
  56. unsigned long gafr, mask = GPIO_bit(gpio);
  57. int fn;
  58. fn = MFP_AF(c);
  59. if (fn > 3)
  60. return -EINVAL;
  61. /* alternate function and direction */
  62. gafr = GAFR(gpio) & ~(0x3 << ((gpio & 0xf) * 2));
  63. GAFR(gpio) = gafr | (fn << ((gpio & 0xf) * 2));
  64. if (c & MFP_DIR_OUT)
  65. GPDR(gpio) |= mask;
  66. else
  67. GPDR(gpio) &= ~mask;
  68. if (__mfp_config_lpm(gpio, c & MFP_LPM_STATE_MASK))
  69. return -EINVAL;
  70. /* give early warning if MFP_LPM_CAN_WAKEUP is set on the
  71. * configurations of those pins not able to wakeup
  72. */
  73. if ((c & MFP_LPM_CAN_WAKEUP) && !gpio_desc[gpio].can_wakeup) {
  74. pr_warning("%s: GPIO%d unable to wakeup\n",
  75. __func__, gpio);
  76. return -EINVAL;
  77. }
  78. if ((c & MFP_LPM_CAN_WAKEUP) && (c & MFP_DIR_OUT)) {
  79. pr_warning("%s: output GPIO%d unable to wakeup\n",
  80. __func__, gpio);
  81. return -EINVAL;
  82. }
  83. return 0;
  84. }
  85. static inline int __mfp_validate(int mfp)
  86. {
  87. int gpio = mfp_to_gpio(mfp);
  88. if ((mfp > MFP_PIN_GPIO127) || !gpio_desc[gpio].valid) {
  89. pr_warning("%s: GPIO%d is invalid pin\n", __func__, gpio);
  90. return -1;
  91. }
  92. return gpio;
  93. }
  94. void pxa2xx_mfp_config(unsigned long *mfp_cfgs, int num)
  95. {
  96. unsigned long flags;
  97. unsigned long *c;
  98. int i, gpio;
  99. for (i = 0, c = mfp_cfgs; i < num; i++, c++) {
  100. gpio = __mfp_validate(MFP_PIN(*c));
  101. if (gpio < 0)
  102. continue;
  103. local_irq_save(flags);
  104. gpio_desc[gpio].config = *c;
  105. __mfp_config_gpio(gpio, *c);
  106. local_irq_restore(flags);
  107. }
  108. }
  109. void pxa2xx_mfp_set_lpm(int mfp, unsigned long lpm)
  110. {
  111. unsigned long flags;
  112. int gpio;
  113. gpio = __mfp_validate(mfp);
  114. if (gpio < 0)
  115. return;
  116. local_irq_save(flags);
  117. __mfp_config_lpm(gpio, lpm);
  118. local_irq_restore(flags);
  119. }
  120. int gpio_set_wake(unsigned int gpio, unsigned int on)
  121. {
  122. struct gpio_desc *d;
  123. unsigned long c;
  124. if (gpio > mfp_to_gpio(MFP_PIN_GPIO127))
  125. return -EINVAL;
  126. d = &gpio_desc[gpio];
  127. c = d->config;
  128. if (!d->valid)
  129. return -EINVAL;
  130. if (d->keypad_gpio)
  131. return -EINVAL;
  132. if (d->can_wakeup && (c & MFP_LPM_CAN_WAKEUP)) {
  133. if (on) {
  134. PWER |= d->mask;
  135. if (c & MFP_LPM_EDGE_RISE)
  136. PRER |= d->mask;
  137. else
  138. PRER &= ~d->mask;
  139. if (c & MFP_LPM_EDGE_FALL)
  140. PFER |= d->mask;
  141. else
  142. PFER &= ~d->mask;
  143. } else {
  144. PWER &= ~d->mask;
  145. PRER &= ~d->mask;
  146. PFER &= ~d->mask;
  147. }
  148. }
  149. return 0;
  150. }
  151. #ifdef CONFIG_PXA25x
  152. static int __init pxa25x_mfp_init(void)
  153. {
  154. int i;
  155. if (cpu_is_pxa25x()) {
  156. for (i = 0; i <= 84; i++)
  157. gpio_desc[i].valid = 1;
  158. for (i = 0; i <= 15; i++) {
  159. gpio_desc[i].can_wakeup = 1;
  160. gpio_desc[i].mask = GPIO_bit(i);
  161. }
  162. }
  163. return 0;
  164. }
  165. postcore_initcall(pxa25x_mfp_init);
  166. #endif /* CONFIG_PXA25x */
  167. #ifdef CONFIG_PXA27x
  168. static int pxa27x_pkwr_gpio[] = {
  169. 13, 16, 17, 34, 36, 37, 38, 39, 90, 91, 93, 94,
  170. 95, 96, 97, 98, 99, 100, 101, 102
  171. };
  172. int keypad_set_wake(unsigned int on)
  173. {
  174. unsigned int i, gpio, mask = 0;
  175. if (!on) {
  176. PKWR = 0;
  177. return 0;
  178. }
  179. for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
  180. gpio = pxa27x_pkwr_gpio[i];
  181. if (gpio_desc[gpio].config & MFP_LPM_CAN_WAKEUP)
  182. mask |= gpio_desc[gpio].mask;
  183. }
  184. PKWR = mask;
  185. return 0;
  186. }
  187. static int __init pxa27x_mfp_init(void)
  188. {
  189. int i, gpio;
  190. if (cpu_is_pxa27x()) {
  191. for (i = 0; i <= 120; i++) {
  192. /* skip GPIO2, 5, 6, 7, 8, they are not
  193. * valid pins allow configuration
  194. */
  195. if (i == 2 || i == 5 || i == 6 ||
  196. i == 7 || i == 8)
  197. continue;
  198. gpio_desc[i].valid = 1;
  199. }
  200. /* Keypad GPIOs */
  201. for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
  202. gpio = pxa27x_pkwr_gpio[i];
  203. gpio_desc[gpio].can_wakeup = 1;
  204. gpio_desc[gpio].keypad_gpio = 1;
  205. gpio_desc[gpio].mask = 1 << i;
  206. }
  207. /* Overwrite GPIO13 as a PWER wakeup source */
  208. for (i = 0; i <= 15; i++) {
  209. /* skip GPIO2, 5, 6, 7, 8 */
  210. if (GPIO_bit(i) & 0x1e4)
  211. continue;
  212. gpio_desc[i].can_wakeup = 1;
  213. gpio_desc[i].mask = GPIO_bit(i);
  214. }
  215. gpio_desc[35].can_wakeup = 1;
  216. gpio_desc[35].mask = PWER_WE35;
  217. }
  218. return 0;
  219. }
  220. postcore_initcall(pxa27x_mfp_init);
  221. #endif /* CONFIG_PXA27x */