mfp-pxa2xx.c 9.7 KB

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