mfp-pxa2xx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 <mach/hardware.h>
  20. #include <mach/pxa-regs.h>
  21. #include <mach/pxa2xx-regs.h>
  22. #include <mach/mfp-pxa2xx.h>
  23. #include "generic.h"
  24. #define gpio_to_bank(gpio) ((gpio) >> 5)
  25. #define PGSR(x) __REG2(0x40F00020, (x) << 2)
  26. #define __GAFR(u, x) __REG2((u) ? 0x40E00058 : 0x40E00054, (x) << 3)
  27. #define GAFR_L(x) __GAFR(0, x)
  28. #define GAFR_U(x) __GAFR(1, x)
  29. #define PWER_WE35 (1 << 24)
  30. struct gpio_desc {
  31. unsigned valid : 1;
  32. unsigned can_wakeup : 1;
  33. unsigned keypad_gpio : 1;
  34. unsigned int mask; /* bit mask in PWER or PKWR */
  35. unsigned long config;
  36. };
  37. static struct gpio_desc gpio_desc[MFP_PIN_GPIO127 + 1];
  38. static int gpio_nr;
  39. static unsigned long gpdr_lpm[4];
  40. static int __mfp_config_gpio(unsigned gpio, unsigned long c)
  41. {
  42. unsigned long gafr, mask = GPIO_bit(gpio);
  43. int bank = gpio_to_bank(gpio);
  44. int uorl = !!(gpio & 0x10); /* GAFRx_U or GAFRx_L ? */
  45. int shft = (gpio & 0xf) << 1;
  46. int fn = MFP_AF(c);
  47. int dir = c & MFP_DIR_OUT;
  48. if (fn > 3)
  49. return -EINVAL;
  50. /* alternate function and direction at run-time */
  51. gafr = (uorl == 0) ? GAFR_L(bank) : GAFR_U(bank);
  52. gafr = (gafr & ~(0x3 << shft)) | (fn << shft);
  53. if (uorl == 0)
  54. GAFR_L(bank) = gafr;
  55. else
  56. GAFR_U(bank) = gafr;
  57. if (dir == MFP_DIR_OUT)
  58. GPDR(gpio) |= mask;
  59. else
  60. GPDR(gpio) &= ~mask;
  61. /* alternate function and direction at low power mode */
  62. switch (c & MFP_LPM_STATE_MASK) {
  63. case MFP_LPM_DRIVE_HIGH:
  64. PGSR(bank) |= mask;
  65. dir = MFP_DIR_OUT;
  66. break;
  67. case MFP_LPM_DRIVE_LOW:
  68. PGSR(bank) &= ~mask;
  69. dir = MFP_DIR_OUT;
  70. break;
  71. case MFP_LPM_DEFAULT:
  72. break;
  73. default:
  74. /* warning and fall through, treat as MFP_LPM_DEFAULT */
  75. pr_warning("%s: GPIO%d: unsupported low power mode\n",
  76. __func__, gpio);
  77. break;
  78. }
  79. if (dir == MFP_DIR_OUT)
  80. gpdr_lpm[bank] |= mask;
  81. else
  82. gpdr_lpm[bank] &= ~mask;
  83. /* give early warning if MFP_LPM_CAN_WAKEUP is set on the
  84. * configurations of those pins not able to wakeup
  85. */
  86. if ((c & MFP_LPM_CAN_WAKEUP) && !gpio_desc[gpio].can_wakeup) {
  87. pr_warning("%s: GPIO%d unable to wakeup\n",
  88. __func__, gpio);
  89. return -EINVAL;
  90. }
  91. if ((c & MFP_LPM_CAN_WAKEUP) && (dir == MFP_DIR_OUT)) {
  92. pr_warning("%s: output GPIO%d unable to wakeup\n",
  93. __func__, gpio);
  94. return -EINVAL;
  95. }
  96. return 0;
  97. }
  98. static inline int __mfp_validate(int mfp)
  99. {
  100. int gpio = mfp_to_gpio(mfp);
  101. if ((mfp > MFP_PIN_GPIO127) || !gpio_desc[gpio].valid) {
  102. pr_warning("%s: GPIO%d is invalid pin\n", __func__, gpio);
  103. return -1;
  104. }
  105. return gpio;
  106. }
  107. void pxa2xx_mfp_config(unsigned long *mfp_cfgs, int num)
  108. {
  109. unsigned long flags;
  110. unsigned long *c;
  111. int i, gpio;
  112. for (i = 0, c = mfp_cfgs; i < num; i++, c++) {
  113. gpio = __mfp_validate(MFP_PIN(*c));
  114. if (gpio < 0)
  115. continue;
  116. local_irq_save(flags);
  117. gpio_desc[gpio].config = *c;
  118. __mfp_config_gpio(gpio, *c);
  119. local_irq_restore(flags);
  120. }
  121. }
  122. void pxa2xx_mfp_set_lpm(int mfp, unsigned long lpm)
  123. {
  124. unsigned long flags, c;
  125. int gpio;
  126. gpio = __mfp_validate(mfp);
  127. if (gpio < 0)
  128. return;
  129. local_irq_save(flags);
  130. c = gpio_desc[gpio].config;
  131. c = (c & ~MFP_LPM_STATE_MASK) | lpm;
  132. __mfp_config_gpio(gpio, c);
  133. local_irq_restore(flags);
  134. }
  135. int gpio_set_wake(unsigned int gpio, unsigned int on)
  136. {
  137. struct gpio_desc *d;
  138. unsigned long c;
  139. if (gpio > mfp_to_gpio(MFP_PIN_GPIO127))
  140. return -EINVAL;
  141. d = &gpio_desc[gpio];
  142. c = d->config;
  143. if (!d->valid)
  144. return -EINVAL;
  145. if (d->keypad_gpio)
  146. return -EINVAL;
  147. if (d->can_wakeup && (c & MFP_LPM_CAN_WAKEUP)) {
  148. if (on) {
  149. PWER |= d->mask;
  150. if (c & MFP_LPM_EDGE_RISE)
  151. PRER |= d->mask;
  152. else
  153. PRER &= ~d->mask;
  154. if (c & MFP_LPM_EDGE_FALL)
  155. PFER |= d->mask;
  156. else
  157. PFER &= ~d->mask;
  158. } else {
  159. PWER &= ~d->mask;
  160. PRER &= ~d->mask;
  161. PFER &= ~d->mask;
  162. }
  163. }
  164. return 0;
  165. }
  166. #ifdef CONFIG_PXA25x
  167. static void __init pxa25x_mfp_init(void)
  168. {
  169. int i;
  170. for (i = 0; i <= 84; i++)
  171. gpio_desc[i].valid = 1;
  172. for (i = 0; i <= 15; i++) {
  173. gpio_desc[i].can_wakeup = 1;
  174. gpio_desc[i].mask = GPIO_bit(i);
  175. }
  176. gpio_nr = 85;
  177. }
  178. #else
  179. static inline void pxa25x_mfp_init(void) {}
  180. #endif /* CONFIG_PXA25x */
  181. #ifdef CONFIG_PXA27x
  182. static int pxa27x_pkwr_gpio[] = {
  183. 13, 16, 17, 34, 36, 37, 38, 39, 90, 91, 93, 94,
  184. 95, 96, 97, 98, 99, 100, 101, 102
  185. };
  186. int keypad_set_wake(unsigned int on)
  187. {
  188. unsigned int i, gpio, mask = 0;
  189. if (!on) {
  190. PKWR = 0;
  191. return 0;
  192. }
  193. for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
  194. gpio = pxa27x_pkwr_gpio[i];
  195. if (gpio_desc[gpio].config & MFP_LPM_CAN_WAKEUP)
  196. mask |= gpio_desc[gpio].mask;
  197. }
  198. PKWR = mask;
  199. return 0;
  200. }
  201. static void __init pxa27x_mfp_init(void)
  202. {
  203. int i, gpio;
  204. for (i = 0; i <= 120; i++) {
  205. /* skip GPIO2, 5, 6, 7, 8, they are not
  206. * valid pins allow configuration
  207. */
  208. if (i == 2 || i == 5 || i == 6 || i == 7 || i == 8)
  209. continue;
  210. gpio_desc[i].valid = 1;
  211. }
  212. /* Keypad GPIOs */
  213. for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
  214. gpio = pxa27x_pkwr_gpio[i];
  215. gpio_desc[gpio].can_wakeup = 1;
  216. gpio_desc[gpio].keypad_gpio = 1;
  217. gpio_desc[gpio].mask = 1 << i;
  218. }
  219. /* Overwrite GPIO13 as a PWER wakeup source */
  220. for (i = 0; i <= 15; i++) {
  221. /* skip GPIO2, 5, 6, 7, 8 */
  222. if (GPIO_bit(i) & 0x1e4)
  223. continue;
  224. gpio_desc[i].can_wakeup = 1;
  225. gpio_desc[i].mask = GPIO_bit(i);
  226. }
  227. gpio_desc[35].can_wakeup = 1;
  228. gpio_desc[35].mask = PWER_WE35;
  229. gpio_nr = 121;
  230. }
  231. #else
  232. static inline void pxa27x_mfp_init(void) {}
  233. #endif /* CONFIG_PXA27x */
  234. #ifdef CONFIG_PM
  235. static unsigned long saved_gafr[2][4];
  236. static unsigned long saved_gpdr[4];
  237. static int pxa2xx_mfp_suspend(struct sys_device *d, pm_message_t state)
  238. {
  239. int i;
  240. for (i = 0; i <= gpio_to_bank(gpio_nr); i++) {
  241. saved_gafr[0][i] = GAFR_L(i);
  242. saved_gafr[1][i] = GAFR_U(i);
  243. saved_gpdr[i] = GPDR(i * 32);
  244. GPDR(i * 32) = gpdr_lpm[i];
  245. }
  246. return 0;
  247. }
  248. static int pxa2xx_mfp_resume(struct sys_device *d)
  249. {
  250. int i;
  251. for (i = 0; i <= gpio_to_bank(gpio_nr); i++) {
  252. GAFR_L(i) = saved_gafr[0][i];
  253. GAFR_U(i) = saved_gafr[1][i];
  254. GPDR(i * 32) = saved_gpdr[i];
  255. }
  256. PSSR = PSSR_RDH | PSSR_PH;
  257. return 0;
  258. }
  259. #else
  260. #define pxa2xx_mfp_suspend NULL
  261. #define pxa2xx_mfp_resume NULL
  262. #endif
  263. struct sysdev_class pxa2xx_mfp_sysclass = {
  264. .name = "mfp",
  265. .suspend = pxa2xx_mfp_suspend,
  266. .resume = pxa2xx_mfp_resume,
  267. };
  268. static int __init pxa2xx_mfp_init(void)
  269. {
  270. int i;
  271. if (!cpu_is_pxa2xx())
  272. return 0;
  273. if (cpu_is_pxa25x())
  274. pxa25x_mfp_init();
  275. if (cpu_is_pxa27x())
  276. pxa27x_mfp_init();
  277. /* initialize gafr_run[], pgsr_lpm[] from existing values */
  278. for (i = 0; i <= gpio_to_bank(gpio_nr); i++)
  279. gpdr_lpm[i] = GPDR(i * 32);
  280. return sysdev_class_register(&pxa2xx_mfp_sysclass);
  281. }
  282. postcore_initcall(pxa2xx_mfp_init);