mfp-pxa2xx.c 8.3 KB

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