gpio-config.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* linux/arch/arm/plat-s3c/gpio-config.c
  2. *
  3. * Copyright 2008 Openmoko, Inc.
  4. * Copyright 2008-2010 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * S3C series GPIO configuration core
  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/module.h>
  16. #include <linux/gpio.h>
  17. #include <linux/io.h>
  18. #include <plat/gpio-core.h>
  19. #include <plat/gpio-cfg.h>
  20. #include <plat/gpio-cfg-helpers.h>
  21. int s3c_gpio_cfgpin(unsigned int pin, unsigned int config)
  22. {
  23. struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
  24. unsigned long flags;
  25. int offset;
  26. int ret;
  27. if (!chip)
  28. return -EINVAL;
  29. offset = pin - chip->chip.base;
  30. s3c_gpio_lock(chip, flags);
  31. ret = s3c_gpio_do_setcfg(chip, offset, config);
  32. s3c_gpio_unlock(chip, flags);
  33. return ret;
  34. }
  35. EXPORT_SYMBOL(s3c_gpio_cfgpin);
  36. int s3c_gpio_cfgpin_range(unsigned int start, unsigned int nr,
  37. unsigned int cfg)
  38. {
  39. int ret;
  40. for (; nr > 0; nr--, start++) {
  41. ret = s3c_gpio_cfgpin(start, cfg);
  42. if (ret != 0)
  43. return ret;
  44. }
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(s3c_gpio_cfgpin_range);
  48. int s3c_gpio_cfgall_range(unsigned int start, unsigned int nr,
  49. unsigned int cfg, s3c_gpio_pull_t pull)
  50. {
  51. int ret;
  52. for (; nr > 0; nr--, start++) {
  53. s3c_gpio_setpull(start, pull);
  54. ret = s3c_gpio_cfgpin(start, cfg);
  55. if (ret != 0)
  56. return ret;
  57. }
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(s3c_gpio_cfgall_range);
  61. unsigned s3c_gpio_getcfg(unsigned int pin)
  62. {
  63. struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
  64. unsigned long flags;
  65. unsigned ret = 0;
  66. int offset;
  67. if (chip) {
  68. offset = pin - chip->chip.base;
  69. s3c_gpio_lock(chip, flags);
  70. ret = s3c_gpio_do_getcfg(chip, offset);
  71. s3c_gpio_unlock(chip, flags);
  72. }
  73. return ret;
  74. }
  75. EXPORT_SYMBOL(s3c_gpio_getcfg);
  76. int s3c_gpio_setpull(unsigned int pin, s3c_gpio_pull_t pull)
  77. {
  78. struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
  79. unsigned long flags;
  80. int offset, ret;
  81. if (!chip)
  82. return -EINVAL;
  83. offset = pin - chip->chip.base;
  84. s3c_gpio_lock(chip, flags);
  85. ret = s3c_gpio_do_setpull(chip, offset, pull);
  86. s3c_gpio_unlock(chip, flags);
  87. return ret;
  88. }
  89. EXPORT_SYMBOL(s3c_gpio_setpull);
  90. s3c_gpio_pull_t s3c_gpio_getpull(unsigned int pin)
  91. {
  92. struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
  93. unsigned long flags;
  94. int offset;
  95. u32 pup = 0;
  96. if (chip) {
  97. offset = pin - chip->chip.base;
  98. s3c_gpio_lock(chip, flags);
  99. pup = s3c_gpio_do_getpull(chip, offset);
  100. s3c_gpio_unlock(chip, flags);
  101. }
  102. return (__force s3c_gpio_pull_t)pup;
  103. }
  104. EXPORT_SYMBOL(s3c_gpio_getpull);
  105. #ifdef CONFIG_S3C_GPIO_CFG_S3C24XX
  106. int s3c_gpio_setcfg_s3c24xx_a(struct s3c_gpio_chip *chip,
  107. unsigned int off, unsigned int cfg)
  108. {
  109. void __iomem *reg = chip->base;
  110. unsigned int shift = off;
  111. u32 con;
  112. if (s3c_gpio_is_cfg_special(cfg)) {
  113. cfg &= 0xf;
  114. /* Map output to 0, and SFN2 to 1 */
  115. cfg -= 1;
  116. if (cfg > 1)
  117. return -EINVAL;
  118. cfg <<= shift;
  119. }
  120. con = __raw_readl(reg);
  121. con &= ~(0x1 << shift);
  122. con |= cfg;
  123. __raw_writel(con, reg);
  124. return 0;
  125. }
  126. unsigned s3c_gpio_getcfg_s3c24xx_a(struct s3c_gpio_chip *chip,
  127. unsigned int off)
  128. {
  129. u32 con;
  130. con = __raw_readl(chip->base);
  131. con >>= off;
  132. con &= 1;
  133. con++;
  134. return S3C_GPIO_SFN(con);
  135. }
  136. int s3c_gpio_setcfg_s3c24xx(struct s3c_gpio_chip *chip,
  137. unsigned int off, unsigned int cfg)
  138. {
  139. void __iomem *reg = chip->base;
  140. unsigned int shift = off * 2;
  141. u32 con;
  142. if (s3c_gpio_is_cfg_special(cfg)) {
  143. cfg &= 0xf;
  144. if (cfg > 3)
  145. return -EINVAL;
  146. cfg <<= shift;
  147. }
  148. con = __raw_readl(reg);
  149. con &= ~(0x3 << shift);
  150. con |= cfg;
  151. __raw_writel(con, reg);
  152. return 0;
  153. }
  154. unsigned int s3c_gpio_getcfg_s3c24xx(struct s3c_gpio_chip *chip,
  155. unsigned int off)
  156. {
  157. u32 con;
  158. con = __raw_readl(chip->base);
  159. con >>= off * 2;
  160. con &= 3;
  161. /* this conversion works for IN and OUT as well as special mode */
  162. return S3C_GPIO_SPECIAL(con);
  163. }
  164. #endif
  165. #ifdef CONFIG_S3C_GPIO_CFG_S3C64XX
  166. int s3c_gpio_setcfg_s3c64xx_4bit(struct s3c_gpio_chip *chip,
  167. unsigned int off, unsigned int cfg)
  168. {
  169. void __iomem *reg = chip->base;
  170. unsigned int shift = (off & 7) * 4;
  171. u32 con;
  172. if (off < 8 && chip->chip.ngpio > 8)
  173. reg -= 4;
  174. if (s3c_gpio_is_cfg_special(cfg)) {
  175. cfg &= 0xf;
  176. cfg <<= shift;
  177. }
  178. con = __raw_readl(reg);
  179. con &= ~(0xf << shift);
  180. con |= cfg;
  181. __raw_writel(con, reg);
  182. return 0;
  183. }
  184. unsigned s3c_gpio_getcfg_s3c64xx_4bit(struct s3c_gpio_chip *chip,
  185. unsigned int off)
  186. {
  187. void __iomem *reg = chip->base;
  188. unsigned int shift = (off & 7) * 4;
  189. u32 con;
  190. if (off < 8 && chip->chip.ngpio > 8)
  191. reg -= 4;
  192. con = __raw_readl(reg);
  193. con >>= shift;
  194. con &= 0xf;
  195. /* this conversion works for IN and OUT as well as special mode */
  196. return S3C_GPIO_SPECIAL(con);
  197. }
  198. #endif /* CONFIG_S3C_GPIO_CFG_S3C64XX */
  199. #ifdef CONFIG_S3C_GPIO_PULL_UPDOWN
  200. int s3c_gpio_setpull_updown(struct s3c_gpio_chip *chip,
  201. unsigned int off, s3c_gpio_pull_t pull)
  202. {
  203. void __iomem *reg = chip->base + 0x08;
  204. int shift = off * 2;
  205. u32 pup;
  206. pup = __raw_readl(reg);
  207. pup &= ~(3 << shift);
  208. pup |= pull << shift;
  209. __raw_writel(pup, reg);
  210. return 0;
  211. }
  212. s3c_gpio_pull_t s3c_gpio_getpull_updown(struct s3c_gpio_chip *chip,
  213. unsigned int off)
  214. {
  215. void __iomem *reg = chip->base + 0x08;
  216. int shift = off * 2;
  217. u32 pup = __raw_readl(reg);
  218. pup >>= shift;
  219. pup &= 0x3;
  220. return (__force s3c_gpio_pull_t)pup;
  221. }
  222. #ifdef CONFIG_S3C_GPIO_PULL_S3C2443
  223. int s3c_gpio_setpull_s3c2443(struct s3c_gpio_chip *chip,
  224. unsigned int off, s3c_gpio_pull_t pull)
  225. {
  226. switch (pull) {
  227. case S3C_GPIO_PULL_NONE:
  228. pull = 0x01;
  229. break;
  230. case S3C_GPIO_PULL_UP:
  231. pull = 0x00;
  232. break;
  233. case S3C_GPIO_PULL_DOWN:
  234. pull = 0x02;
  235. break;
  236. }
  237. return s3c_gpio_setpull_updown(chip, off, pull);
  238. }
  239. s3c_gpio_pull_t s3c_gpio_getpull_s3c2443(struct s3c_gpio_chip *chip,
  240. unsigned int off)
  241. {
  242. s3c_gpio_pull_t pull;
  243. pull = s3c_gpio_getpull_updown(chip, off);
  244. switch (pull) {
  245. case 0x00:
  246. pull = S3C_GPIO_PULL_UP;
  247. break;
  248. case 0x01:
  249. case 0x03:
  250. pull = S3C_GPIO_PULL_NONE;
  251. break;
  252. case 0x02:
  253. pull = S3C_GPIO_PULL_DOWN;
  254. break;
  255. }
  256. return pull;
  257. }
  258. #endif
  259. #endif
  260. #if defined(CONFIG_S3C_GPIO_PULL_UP) || defined(CONFIG_S3C_GPIO_PULL_DOWN)
  261. static int s3c_gpio_setpull_1(struct s3c_gpio_chip *chip,
  262. unsigned int off, s3c_gpio_pull_t pull,
  263. s3c_gpio_pull_t updown)
  264. {
  265. void __iomem *reg = chip->base + 0x08;
  266. u32 pup = __raw_readl(reg);
  267. if (pull == updown)
  268. pup &= ~(1 << off);
  269. else if (pull == S3C_GPIO_PULL_NONE)
  270. pup |= (1 << off);
  271. else
  272. return -EINVAL;
  273. __raw_writel(pup, reg);
  274. return 0;
  275. }
  276. static s3c_gpio_pull_t s3c_gpio_getpull_1(struct s3c_gpio_chip *chip,
  277. unsigned int off, s3c_gpio_pull_t updown)
  278. {
  279. void __iomem *reg = chip->base + 0x08;
  280. u32 pup = __raw_readl(reg);
  281. pup &= (1 << off);
  282. return pup ? S3C_GPIO_PULL_NONE : updown;
  283. }
  284. #endif /* CONFIG_S3C_GPIO_PULL_UP || CONFIG_S3C_GPIO_PULL_DOWN */
  285. #ifdef CONFIG_S3C_GPIO_PULL_UP
  286. s3c_gpio_pull_t s3c_gpio_getpull_1up(struct s3c_gpio_chip *chip,
  287. unsigned int off)
  288. {
  289. return s3c_gpio_getpull_1(chip, off, S3C_GPIO_PULL_UP);
  290. }
  291. int s3c_gpio_setpull_1up(struct s3c_gpio_chip *chip,
  292. unsigned int off, s3c_gpio_pull_t pull)
  293. {
  294. return s3c_gpio_setpull_1(chip, off, pull, S3C_GPIO_PULL_UP);
  295. }
  296. #endif /* CONFIG_S3C_GPIO_PULL_UP */
  297. #ifdef CONFIG_S3C_GPIO_PULL_DOWN
  298. s3c_gpio_pull_t s3c_gpio_getpull_1down(struct s3c_gpio_chip *chip,
  299. unsigned int off)
  300. {
  301. return s3c_gpio_getpull_1(chip, off, S3C_GPIO_PULL_DOWN);
  302. }
  303. int s3c_gpio_setpull_1down(struct s3c_gpio_chip *chip,
  304. unsigned int off, s3c_gpio_pull_t pull)
  305. {
  306. return s3c_gpio_setpull_1(chip, off, pull, S3C_GPIO_PULL_DOWN);
  307. }
  308. #endif /* CONFIG_S3C_GPIO_PULL_DOWN */
  309. #ifdef CONFIG_S5P_GPIO_DRVSTR
  310. s5p_gpio_drvstr_t s5p_gpio_get_drvstr(unsigned int pin)
  311. {
  312. struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
  313. unsigned int off;
  314. void __iomem *reg;
  315. int shift;
  316. u32 drvstr;
  317. if (!chip)
  318. return -EINVAL;
  319. off = pin - chip->chip.base;
  320. shift = off * 2;
  321. reg = chip->base + 0x0C;
  322. drvstr = __raw_readl(reg);
  323. drvstr = drvstr >> shift;
  324. drvstr &= 0x3;
  325. return (__force s5p_gpio_drvstr_t)drvstr;
  326. }
  327. EXPORT_SYMBOL(s5p_gpio_get_drvstr);
  328. int s5p_gpio_set_drvstr(unsigned int pin, s5p_gpio_drvstr_t drvstr)
  329. {
  330. struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
  331. unsigned int off;
  332. void __iomem *reg;
  333. int shift;
  334. u32 tmp;
  335. if (!chip)
  336. return -EINVAL;
  337. off = pin - chip->chip.base;
  338. shift = off * 2;
  339. reg = chip->base + 0x0C;
  340. tmp = __raw_readl(reg);
  341. tmp &= ~(0x3 << shift);
  342. tmp |= drvstr << shift;
  343. __raw_writel(tmp, reg);
  344. return 0;
  345. }
  346. EXPORT_SYMBOL(s5p_gpio_set_drvstr);
  347. #endif /* CONFIG_S5P_GPIO_DRVSTR */