gpiolib.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* arch/arm/plat-s3c64xx/gpiolib.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. * S3C64XX - GPIOlib support
  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/irq.h>
  16. #include <linux/io.h>
  17. #include <mach/map.h>
  18. #include <mach/gpio.h>
  19. #include <mach/gpio-core.h>
  20. #include <plat/gpio-cfg.h>
  21. #include <plat/gpio-cfg-helpers.h>
  22. #include <plat/regs-gpio.h>
  23. /* GPIO bank summary:
  24. *
  25. * Bank GPIOs Style SlpCon ExtInt Group
  26. * A 8 4Bit Yes 1
  27. * B 7 4Bit Yes 1
  28. * C 8 4Bit Yes 2
  29. * D 5 4Bit Yes 3
  30. * E 5 4Bit Yes None
  31. * F 16 2Bit Yes 4 [1]
  32. * G 7 4Bit Yes 5
  33. * H 10 4Bit[2] Yes 6
  34. * I 16 2Bit Yes None
  35. * J 12 2Bit Yes None
  36. * K 16 4Bit[2] No None
  37. * L 15 4Bit[2] No None
  38. * M 6 4Bit No IRQ_EINT
  39. * N 16 2Bit No IRQ_EINT
  40. * O 16 2Bit Yes 7
  41. * P 15 2Bit Yes 8
  42. * Q 9 2Bit Yes 9
  43. *
  44. * [1] BANKF pins 14,15 do not form part of the external interrupt sources
  45. * [2] BANK has two control registers, GPxCON0 and GPxCON1
  46. */
  47. #define OFF_GPCON (0x00)
  48. #define OFF_GPDAT (0x04)
  49. #define con_4bit_shift(__off) ((__off) * 4)
  50. #if 1
  51. #define gpio_dbg(x...) do { } while(0)
  52. #else
  53. #define gpio_dbg(x...) printk(KERN_DEBUG x)
  54. #endif
  55. /* The s3c64xx_gpiolib_4bit routines are to control the gpio banks where
  56. * the gpio configuration register (GPxCON) has 4 bits per GPIO, as the
  57. * following example:
  58. *
  59. * base + 0x00: Control register, 4 bits per gpio
  60. * gpio n: 4 bits starting at (4*n)
  61. * 0000 = input, 0001 = output, others mean special-function
  62. * base + 0x04: Data register, 1 bit per gpio
  63. * bit n: data bit n
  64. *
  65. * Note, since the data register is one bit per gpio and is at base + 0x4
  66. * we can use s3c_gpiolib_get and s3c_gpiolib_set to change the state of
  67. * the output.
  68. */
  69. static int s3c64xx_gpiolib_4bit_input(struct gpio_chip *chip, unsigned offset)
  70. {
  71. struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
  72. void __iomem *base = ourchip->base;
  73. unsigned long con;
  74. con = __raw_readl(base + OFF_GPCON);
  75. con &= ~(0xf << con_4bit_shift(offset));
  76. __raw_writel(con, base + OFF_GPCON);
  77. gpio_dbg("%s: %p: CON now %08lx\n", __func__, base, con);
  78. return 0;
  79. }
  80. static int s3c64xx_gpiolib_4bit_output(struct gpio_chip *chip,
  81. unsigned offset, int value)
  82. {
  83. struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
  84. void __iomem *base = ourchip->base;
  85. unsigned long con;
  86. unsigned long dat;
  87. con = __raw_readl(base + OFF_GPCON);
  88. con &= ~(0xf << con_4bit_shift(offset));
  89. con |= 0x1 << con_4bit_shift(offset);
  90. dat = __raw_readl(base + OFF_GPDAT);
  91. if (value)
  92. dat |= 1 << offset;
  93. else
  94. dat &= ~(1 << offset);
  95. __raw_writel(dat, base + OFF_GPDAT);
  96. __raw_writel(con, base + OFF_GPCON);
  97. __raw_writel(dat, base + OFF_GPDAT);
  98. gpio_dbg("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);
  99. return 0;
  100. }
  101. /* The next set of routines are for the case where the GPIO configuration
  102. * registers are 4 bits per GPIO but there is more than one register (the
  103. * bank has more than 8 GPIOs.
  104. *
  105. * This case is the similar to the 4 bit case, but the registers are as
  106. * follows:
  107. *
  108. * base + 0x00: Control register, 4 bits per gpio (lower 8 GPIOs)
  109. * gpio n: 4 bits starting at (4*n)
  110. * 0000 = input, 0001 = output, others mean special-function
  111. * base + 0x04: Control register, 4 bits per gpio (up to 8 additions GPIOs)
  112. * gpio n: 4 bits starting at (4*n)
  113. * 0000 = input, 0001 = output, others mean special-function
  114. * base + 0x08: Data register, 1 bit per gpio
  115. * bit n: data bit n
  116. *
  117. * To allow us to use the s3c_gpiolib_get and s3c_gpiolib_set routines we
  118. * store the 'base + 0x4' address so that these routines see the data
  119. * register at ourchip->base + 0x04.
  120. */
  121. static int s3c64xx_gpiolib_4bit2_input(struct gpio_chip *chip, unsigned offset)
  122. {
  123. struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
  124. void __iomem *base = ourchip->base;
  125. void __iomem *regcon = base;
  126. unsigned long con;
  127. if (offset > 7)
  128. offset -= 8;
  129. else
  130. regcon -= 4;
  131. con = __raw_readl(regcon);
  132. con &= ~(0xf << con_4bit_shift(offset));
  133. __raw_writel(con, regcon);
  134. gpio_dbg("%s: %p: CON %08lx\n", __func__, base, con);
  135. return 0;
  136. }
  137. static int s3c64xx_gpiolib_4bit2_output(struct gpio_chip *chip,
  138. unsigned offset, int value)
  139. {
  140. struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
  141. void __iomem *base = ourchip->base;
  142. void __iomem *regcon = base;
  143. unsigned long con;
  144. unsigned long dat;
  145. if (offset > 7)
  146. offset -= 8;
  147. else
  148. regcon -= 4;
  149. con = __raw_readl(regcon);
  150. con &= ~(0xf << con_4bit_shift(offset));
  151. con |= 0x1 << con_4bit_shift(offset);
  152. dat = __raw_readl(base + OFF_GPDAT);
  153. if (value)
  154. dat |= 1 << offset;
  155. else
  156. dat &= ~(1 << offset);
  157. __raw_writel(dat, base + OFF_GPDAT);
  158. __raw_writel(con, regcon);
  159. __raw_writel(dat, base + OFF_GPDAT);
  160. gpio_dbg("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);
  161. return 0;
  162. }
  163. static struct s3c_gpio_cfg gpio_4bit_cfg_noint = {
  164. .set_config = s3c_gpio_setcfg_s3c64xx_4bit,
  165. .set_pull = s3c_gpio_setpull_updown,
  166. .get_pull = s3c_gpio_getpull_updown,
  167. };
  168. static struct s3c_gpio_cfg gpio_4bit_cfg_eint0111 = {
  169. .cfg_eint = 7,
  170. .set_config = s3c_gpio_setcfg_s3c64xx_4bit,
  171. .set_pull = s3c_gpio_setpull_updown,
  172. .get_pull = s3c_gpio_getpull_updown,
  173. };
  174. static struct s3c_gpio_cfg gpio_4bit_cfg_eint0011 = {
  175. .cfg_eint = 3,
  176. .set_config = s3c_gpio_setcfg_s3c64xx_4bit,
  177. .set_pull = s3c_gpio_setpull_updown,
  178. .get_pull = s3c_gpio_getpull_updown,
  179. };
  180. static struct s3c_gpio_chip gpio_4bit[] = {
  181. {
  182. .base = S3C64XX_GPA_BASE,
  183. .config = &gpio_4bit_cfg_eint0111,
  184. .chip = {
  185. .base = S3C64XX_GPA(0),
  186. .ngpio = S3C64XX_GPIO_A_NR,
  187. .label = "GPA",
  188. },
  189. }, {
  190. .base = S3C64XX_GPB_BASE,
  191. .config = &gpio_4bit_cfg_eint0111,
  192. .chip = {
  193. .base = S3C64XX_GPB(0),
  194. .ngpio = S3C64XX_GPIO_B_NR,
  195. .label = "GPB",
  196. },
  197. }, {
  198. .base = S3C64XX_GPC_BASE,
  199. .config = &gpio_4bit_cfg_eint0111,
  200. .chip = {
  201. .base = S3C64XX_GPC(0),
  202. .ngpio = S3C64XX_GPIO_C_NR,
  203. .label = "GPC",
  204. },
  205. }, {
  206. .base = S3C64XX_GPD_BASE,
  207. .config = &gpio_4bit_cfg_eint0111,
  208. .chip = {
  209. .base = S3C64XX_GPD(0),
  210. .ngpio = S3C64XX_GPIO_D_NR,
  211. .label = "GPD",
  212. },
  213. }, {
  214. .base = S3C64XX_GPE_BASE,
  215. .config = &gpio_4bit_cfg_noint,
  216. .chip = {
  217. .base = S3C64XX_GPE(0),
  218. .ngpio = S3C64XX_GPIO_E_NR,
  219. .label = "GPE",
  220. },
  221. }, {
  222. .base = S3C64XX_GPG_BASE,
  223. .config = &gpio_4bit_cfg_eint0111,
  224. .chip = {
  225. .base = S3C64XX_GPG(0),
  226. .ngpio = S3C64XX_GPIO_G_NR,
  227. .label = "GPG",
  228. },
  229. }, {
  230. .base = S3C64XX_GPM_BASE,
  231. .config = &gpio_4bit_cfg_eint0011,
  232. .chip = {
  233. .base = S3C64XX_GPM(0),
  234. .ngpio = S3C64XX_GPIO_M_NR,
  235. .label = "GPM",
  236. },
  237. },
  238. };
  239. static struct s3c_gpio_chip gpio_4bit2[] = {
  240. {
  241. .base = S3C64XX_GPH_BASE + 0x4,
  242. .config = &gpio_4bit_cfg_eint0111,
  243. .chip = {
  244. .base = S3C64XX_GPH(0),
  245. .ngpio = S3C64XX_GPIO_H_NR,
  246. .label = "GPH",
  247. },
  248. }, {
  249. .base = S3C64XX_GPK_BASE + 0x4,
  250. .config = &gpio_4bit_cfg_noint,
  251. .chip = {
  252. .base = S3C64XX_GPK(0),
  253. .ngpio = S3C64XX_GPIO_K_NR,
  254. .label = "GPK",
  255. },
  256. }, {
  257. .base = S3C64XX_GPL_BASE + 0x4,
  258. .config = &gpio_4bit_cfg_eint0011,
  259. .chip = {
  260. .base = S3C64XX_GPL(0),
  261. .ngpio = S3C64XX_GPIO_L_NR,
  262. .label = "GPL",
  263. },
  264. },
  265. };
  266. static struct s3c_gpio_cfg gpio_2bit_cfg_noint = {
  267. .set_config = s3c_gpio_setcfg_s3c24xx,
  268. .set_pull = s3c_gpio_setpull_updown,
  269. .get_pull = s3c_gpio_getpull_updown,
  270. };
  271. static struct s3c_gpio_cfg gpio_2bit_cfg_eint10 = {
  272. .cfg_eint = 2,
  273. .set_config = s3c_gpio_setcfg_s3c24xx,
  274. .set_pull = s3c_gpio_setpull_updown,
  275. .get_pull = s3c_gpio_getpull_updown,
  276. };
  277. static struct s3c_gpio_cfg gpio_2bit_cfg_eint11 = {
  278. .cfg_eint = 3,
  279. .set_config = s3c_gpio_setcfg_s3c24xx,
  280. .set_pull = s3c_gpio_setpull_updown,
  281. .get_pull = s3c_gpio_getpull_updown,
  282. };
  283. static struct s3c_gpio_chip gpio_2bit[] = {
  284. {
  285. .base = S3C64XX_GPF_BASE,
  286. .config = &gpio_2bit_cfg_eint11,
  287. .chip = {
  288. .base = S3C64XX_GPF(0),
  289. .ngpio = S3C64XX_GPIO_F_NR,
  290. .label = "GPF",
  291. },
  292. }, {
  293. .base = S3C64XX_GPI_BASE,
  294. .config = &gpio_2bit_cfg_noint,
  295. .chip = {
  296. .base = S3C64XX_GPI(0),
  297. .ngpio = S3C64XX_GPIO_I_NR,
  298. .label = "GPI",
  299. },
  300. }, {
  301. .base = S3C64XX_GPJ_BASE,
  302. .config = &gpio_2bit_cfg_noint,
  303. .chip = {
  304. .base = S3C64XX_GPJ(0),
  305. .ngpio = S3C64XX_GPIO_J_NR,
  306. .label = "GPJ",
  307. },
  308. }, {
  309. .base = S3C64XX_GPN_BASE,
  310. .config = &gpio_2bit_cfg_eint10,
  311. .chip = {
  312. .base = S3C64XX_GPN(0),
  313. .ngpio = S3C64XX_GPIO_N_NR,
  314. .label = "GPN",
  315. },
  316. }, {
  317. .base = S3C64XX_GPO_BASE,
  318. .config = &gpio_2bit_cfg_eint11,
  319. .chip = {
  320. .base = S3C64XX_GPO(0),
  321. .ngpio = S3C64XX_GPIO_O_NR,
  322. .label = "GPO",
  323. },
  324. }, {
  325. .base = S3C64XX_GPP_BASE,
  326. .config = &gpio_2bit_cfg_eint11,
  327. .chip = {
  328. .base = S3C64XX_GPP(0),
  329. .ngpio = S3C64XX_GPIO_P_NR,
  330. .label = "GPP",
  331. },
  332. }, {
  333. .base = S3C64XX_GPQ_BASE,
  334. .config = &gpio_2bit_cfg_eint11,
  335. .chip = {
  336. .base = S3C64XX_GPQ(0),
  337. .ngpio = S3C64XX_GPIO_Q_NR,
  338. .label = "GPQ",
  339. },
  340. },
  341. };
  342. static __init void s3c64xx_gpiolib_add_4bit(struct s3c_gpio_chip *chip)
  343. {
  344. chip->chip.direction_input = s3c64xx_gpiolib_4bit_input;
  345. chip->chip.direction_output = s3c64xx_gpiolib_4bit_output;
  346. }
  347. static __init void s3c64xx_gpiolib_add_4bit2(struct s3c_gpio_chip *chip)
  348. {
  349. chip->chip.direction_input = s3c64xx_gpiolib_4bit2_input;
  350. chip->chip.direction_output = s3c64xx_gpiolib_4bit2_output;
  351. }
  352. static __init void s3c64xx_gpiolib_add(struct s3c_gpio_chip *chips,
  353. int nr_chips,
  354. void (*fn)(struct s3c_gpio_chip *))
  355. {
  356. for (; nr_chips > 0; nr_chips--, chips++) {
  357. if (fn)
  358. (fn)(chips);
  359. s3c_gpiolib_add(chips);
  360. }
  361. }
  362. static __init int s3c64xx_gpiolib_init(void)
  363. {
  364. s3c64xx_gpiolib_add(gpio_4bit, ARRAY_SIZE(gpio_4bit),
  365. s3c64xx_gpiolib_add_4bit);
  366. s3c64xx_gpiolib_add(gpio_4bit2, ARRAY_SIZE(gpio_4bit2),
  367. s3c64xx_gpiolib_add_4bit2);
  368. s3c64xx_gpiolib_add(gpio_2bit, ARRAY_SIZE(gpio_2bit), NULL);
  369. return 0;
  370. }
  371. core_initcall(s3c64xx_gpiolib_init);