pm-gpio.c 9.5 KB

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