mfp-pxa2xx.c 9.9 KB

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