pm-gpio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* linux/arch/arm/plat-s3c/pm-gpio.c
  2. *
  3. * Copyright 2008 Openmoko, Inc.
  4. * Copyright 2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * S3C series GPIO PM code
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/sysdev.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/gpio.h>
  19. #include <plat/gpio-core.h>
  20. #include <plat/pm.h>
  21. /* PM GPIO helpers */
  22. #define OFFS_CON (0x00)
  23. #define OFFS_DAT (0x04)
  24. #define OFFS_UP (0x08)
  25. static void s3c_gpio_pm_1bit_save(struct s3c_gpio_chip *chip)
  26. {
  27. chip->pm_save[0] = __raw_readl(chip->base + OFFS_CON);
  28. chip->pm_save[1] = __raw_readl(chip->base + OFFS_DAT);
  29. }
  30. static void s3c_gpio_pm_1bit_resume(struct s3c_gpio_chip *chip)
  31. {
  32. void __iomem *base = chip->base;
  33. u32 old_gpcon = __raw_readl(base + OFFS_CON);
  34. u32 old_gpdat = __raw_readl(base + OFFS_DAT);
  35. u32 gps_gpcon = chip->pm_save[0];
  36. u32 gps_gpdat = chip->pm_save[1];
  37. u32 gpcon;
  38. /* GPACON only has one bit per control / data and no PULLUPs.
  39. * GPACON[x] = 0 => Output, 1 => SFN */
  40. /* first set all SFN bits to SFN */
  41. gpcon = old_gpcon | gps_gpcon;
  42. __raw_writel(gpcon, base + OFFS_CON);
  43. /* now set all the other bits */
  44. __raw_writel(gps_gpdat, base + OFFS_DAT);
  45. __raw_writel(gps_gpcon, base + OFFS_CON);
  46. S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
  47. chip->chip.label, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
  48. }
  49. struct s3c_gpio_pm s3c_gpio_pm_1bit = {
  50. .save = s3c_gpio_pm_1bit_save,
  51. .resume = s3c_gpio_pm_1bit_resume,
  52. };
  53. static void s3c_gpio_pm_2bit_save(struct s3c_gpio_chip *chip)
  54. {
  55. chip->pm_save[0] = __raw_readl(chip->base + OFFS_CON);
  56. chip->pm_save[1] = __raw_readl(chip->base + OFFS_DAT);
  57. chip->pm_save[2] = __raw_readl(chip->base + OFFS_UP);
  58. }
  59. /* Test whether the given masked+shifted bits of an GPIO configuration
  60. * are one of the SFN (special function) modes. */
  61. static inline int is_sfn(unsigned long con)
  62. {
  63. return con >= 2;
  64. }
  65. /* Test if the given masked+shifted GPIO configuration is an input */
  66. static inline int is_in(unsigned long con)
  67. {
  68. return con == 0;
  69. }
  70. /* Test if the given masked+shifted GPIO configuration is an output */
  71. static inline int is_out(unsigned long con)
  72. {
  73. return con == 1;
  74. }
  75. /**
  76. * s3c_gpio_pm_2bit_resume() - restore the given GPIO bank
  77. * @chip: The chip information to resume.
  78. *
  79. * Restore one of the GPIO banks that was saved during suspend. This is
  80. * not as simple as once thought, due to the possibility of glitches
  81. * from the order that the CON and DAT registers are set in.
  82. *
  83. * The three states the pin can be are {IN,OUT,SFN} which gives us 9
  84. * combinations of changes to check. Three of these, if the pin stays
  85. * in the same configuration can be discounted. This leaves us with
  86. * the following:
  87. *
  88. * { IN => OUT } Change DAT first
  89. * { IN => SFN } Change CON first
  90. * { OUT => SFN } Change CON first, so new data will not glitch
  91. * { OUT => IN } Change CON first, so new data will not glitch
  92. * { SFN => IN } Change CON first
  93. * { SFN => OUT } Change DAT first, so new data will not glitch [1]
  94. *
  95. * We do not currently deal with the UP registers as these control
  96. * weak resistors, so a small delay in change should not need to bring
  97. * these into the calculations.
  98. *
  99. * [1] this assumes that writing to a pin DAT whilst in SFN will set the
  100. * state for when it is next output.
  101. */
  102. static void s3c_gpio_pm_2bit_resume(struct s3c_gpio_chip *chip)
  103. {
  104. void __iomem *base = chip->base;
  105. u32 old_gpcon = __raw_readl(base + OFFS_CON);
  106. u32 old_gpdat = __raw_readl(base + OFFS_DAT);
  107. u32 gps_gpcon = chip->pm_save[0];
  108. u32 gps_gpdat = chip->pm_save[1];
  109. u32 gpcon, old, new, mask;
  110. u32 change_mask = 0x0;
  111. int nr;
  112. /* restore GPIO pull-up settings */
  113. __raw_writel(chip->pm_save[2], base + OFFS_UP);
  114. /* Create a change_mask of all the items that need to have
  115. * their CON value changed before their DAT value, so that
  116. * we minimise the work between the two settings.
  117. */
  118. for (nr = 0, mask = 0x03; nr < 32; nr += 2, mask <<= 2) {
  119. old = (old_gpcon & mask) >> nr;
  120. new = (gps_gpcon & mask) >> nr;
  121. /* If there is no change, then skip */
  122. if (old == new)
  123. continue;
  124. /* If both are special function, then skip */
  125. if (is_sfn(old) && is_sfn(new))
  126. continue;
  127. /* Change is IN => OUT, do not change now */
  128. if (is_in(old) && is_out(new))
  129. continue;
  130. /* Change is SFN => OUT, do not change now */
  131. if (is_sfn(old) && is_out(new))
  132. continue;
  133. /* We should now be at the case of IN=>SFN,
  134. * OUT=>SFN, OUT=>IN, SFN=>IN. */
  135. change_mask |= mask;
  136. }
  137. /* Write the new CON settings */
  138. gpcon = old_gpcon & ~change_mask;
  139. gpcon |= gps_gpcon & change_mask;
  140. __raw_writel(gpcon, base + OFFS_CON);
  141. /* Now change any items that require DAT,CON */
  142. __raw_writel(gps_gpdat, base + OFFS_DAT);
  143. __raw_writel(gps_gpcon, base + OFFS_CON);
  144. S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
  145. chip->chip.label, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
  146. }
  147. struct s3c_gpio_pm s3c_gpio_pm_2bit = {
  148. .save = s3c_gpio_pm_2bit_save,
  149. .resume = s3c_gpio_pm_2bit_resume,
  150. };
  151. #if defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_PLAT_S5P)
  152. static void s3c_gpio_pm_4bit_save(struct s3c_gpio_chip *chip)
  153. {
  154. chip->pm_save[1] = __raw_readl(chip->base + OFFS_CON);
  155. chip->pm_save[2] = __raw_readl(chip->base + OFFS_DAT);
  156. chip->pm_save[3] = __raw_readl(chip->base + OFFS_UP);
  157. if (chip->chip.ngpio > 8)
  158. chip->pm_save[0] = __raw_readl(chip->base - 4);
  159. }
  160. static u32 s3c_gpio_pm_4bit_mask(u32 old_gpcon, u32 gps_gpcon)
  161. {
  162. u32 old, new, mask;
  163. u32 change_mask = 0x0;
  164. int nr;
  165. for (nr = 0, mask = 0x0f; nr < 16; nr += 4, mask <<= 4) {
  166. old = (old_gpcon & mask) >> nr;
  167. new = (gps_gpcon & mask) >> nr;
  168. /* If there is no change, then skip */
  169. if (old == new)
  170. continue;
  171. /* If both are special function, then skip */
  172. if (is_sfn(old) && is_sfn(new))
  173. continue;
  174. /* Change is IN => OUT, do not change now */
  175. if (is_in(old) && is_out(new))
  176. continue;
  177. /* Change is SFN => OUT, do not change now */
  178. if (is_sfn(old) && is_out(new))
  179. continue;
  180. /* We should now be at the case of IN=>SFN,
  181. * OUT=>SFN, OUT=>IN, SFN=>IN. */
  182. change_mask |= mask;
  183. }
  184. return change_mask;
  185. }
  186. static void s3c_gpio_pm_4bit_con(struct s3c_gpio_chip *chip, int index)
  187. {
  188. void __iomem *con = chip->base + (index * 4);
  189. u32 old_gpcon = __raw_readl(con);
  190. u32 gps_gpcon = chip->pm_save[index + 1];
  191. u32 gpcon, mask;
  192. mask = s3c_gpio_pm_4bit_mask(old_gpcon, gps_gpcon);
  193. gpcon = old_gpcon & ~mask;
  194. gpcon |= gps_gpcon & mask;
  195. __raw_writel(gpcon, con);
  196. }
  197. static void s3c_gpio_pm_4bit_resume(struct s3c_gpio_chip *chip)
  198. {
  199. void __iomem *base = chip->base;
  200. u32 old_gpcon[2];
  201. u32 old_gpdat = __raw_readl(base + OFFS_DAT);
  202. u32 gps_gpdat = chip->pm_save[2];
  203. /* First, modify the CON settings */
  204. old_gpcon[0] = 0;
  205. old_gpcon[1] = __raw_readl(base + OFFS_CON);
  206. s3c_gpio_pm_4bit_con(chip, 0);
  207. if (chip->chip.ngpio > 8) {
  208. old_gpcon[0] = __raw_readl(base - 4);
  209. s3c_gpio_pm_4bit_con(chip, -1);
  210. }
  211. /* Now change the configurations that require DAT,CON */
  212. __raw_writel(chip->pm_save[2], base + OFFS_DAT);
  213. __raw_writel(chip->pm_save[1], base + OFFS_CON);
  214. if (chip->chip.ngpio > 8)
  215. __raw_writel(chip->pm_save[0], base - 4);
  216. __raw_writel(chip->pm_save[2], base + OFFS_DAT);
  217. __raw_writel(chip->pm_save[3], base + OFFS_UP);
  218. if (chip->chip.ngpio > 8) {
  219. S3C_PMDBG("%s: CON4 %08x,%08x => %08x,%08x, DAT %08x => %08x\n",
  220. chip->chip.label, old_gpcon[0], old_gpcon[1],
  221. __raw_readl(base - 4),
  222. __raw_readl(base + OFFS_CON),
  223. old_gpdat, gps_gpdat);
  224. } else
  225. S3C_PMDBG("%s: CON4 %08x => %08x, DAT %08x => %08x\n",
  226. chip->chip.label, old_gpcon[1],
  227. __raw_readl(base + OFFS_CON),
  228. old_gpdat, gps_gpdat);
  229. }
  230. struct s3c_gpio_pm s3c_gpio_pm_4bit = {
  231. .save = s3c_gpio_pm_4bit_save,
  232. .resume = s3c_gpio_pm_4bit_resume,
  233. };
  234. #endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P */
  235. /**
  236. * s3c_pm_save_gpio() - save gpio chip data for suspend
  237. * @ourchip: The chip for suspend.
  238. */
  239. static void s3c_pm_save_gpio(struct s3c_gpio_chip *ourchip)
  240. {
  241. struct s3c_gpio_pm *pm = ourchip->pm;
  242. if (pm == NULL || pm->save == NULL)
  243. S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
  244. else
  245. pm->save(ourchip);
  246. }
  247. /**
  248. * s3c_pm_save_gpios() - Save the state of the GPIO banks.
  249. *
  250. * For all the GPIO banks, save the state of each one ready for going
  251. * into a suspend mode.
  252. */
  253. void s3c_pm_save_gpios(void)
  254. {
  255. struct s3c_gpio_chip *ourchip;
  256. unsigned int gpio_nr;
  257. for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
  258. ourchip = s3c_gpiolib_getchip(gpio_nr);
  259. if (!ourchip) {
  260. gpio_nr++;
  261. continue;
  262. }
  263. s3c_pm_save_gpio(ourchip);
  264. S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
  265. ourchip->chip.label,
  266. ourchip->pm_save[0],
  267. ourchip->pm_save[1],
  268. ourchip->pm_save[2],
  269. ourchip->pm_save[3]);
  270. gpio_nr += ourchip->chip.ngpio;
  271. gpio_nr += CONFIG_S3C_GPIO_SPACE;
  272. }
  273. }
  274. /**
  275. * s3c_pm_resume_gpio() - restore gpio chip data after suspend
  276. * @ourchip: The suspended chip.
  277. */
  278. static void s3c_pm_resume_gpio(struct s3c_gpio_chip *ourchip)
  279. {
  280. struct s3c_gpio_pm *pm = ourchip->pm;
  281. if (pm == NULL || pm->resume == NULL)
  282. S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
  283. else
  284. pm->resume(ourchip);
  285. }
  286. void s3c_pm_restore_gpios(void)
  287. {
  288. struct s3c_gpio_chip *ourchip;
  289. unsigned int gpio_nr;
  290. for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
  291. ourchip = s3c_gpiolib_getchip(gpio_nr);
  292. if (!ourchip) {
  293. gpio_nr++;
  294. continue;
  295. }
  296. s3c_pm_resume_gpio(ourchip);
  297. gpio_nr += ourchip->chip.ngpio;
  298. gpio_nr += CONFIG_S3C_GPIO_SPACE;
  299. }
  300. }