pm-gpio.c 9.7 KB

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