gpiolib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * arch/arm/plat-s5pc1xx/gpiolib.c
  3. *
  4. * Copyright 2009 Samsung Electronics Co
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. *
  7. * S5PC1XX - GPIOlib support
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/irq.h>
  15. #include <linux/io.h>
  16. #include <linux/gpio.h>
  17. #include <mach/map.h>
  18. #include <mach/gpio-core.h>
  19. #include <plat/gpio-cfg.h>
  20. #include <plat/gpio-cfg-helpers.h>
  21. #include <plat/regs-gpio.h>
  22. /* S5PC100 GPIO bank summary:
  23. *
  24. * Bank GPIOs Style INT Type
  25. * A0 8 4Bit GPIO_INT0
  26. * A1 5 4Bit GPIO_INT1
  27. * B 8 4Bit GPIO_INT2
  28. * C 5 4Bit GPIO_INT3
  29. * D 7 4Bit GPIO_INT4
  30. * E0 8 4Bit GPIO_INT5
  31. * E1 6 4Bit GPIO_INT6
  32. * F0 8 4Bit GPIO_INT7
  33. * F1 8 4Bit GPIO_INT8
  34. * F2 8 4Bit GPIO_INT9
  35. * F3 4 4Bit GPIO_INT10
  36. * G0 8 4Bit GPIO_INT11
  37. * G1 3 4Bit GPIO_INT12
  38. * G2 7 4Bit GPIO_INT13
  39. * G3 7 4Bit GPIO_INT14
  40. * H0 8 4Bit WKUP_INT
  41. * H1 8 4Bit WKUP_INT
  42. * H2 8 4Bit WKUP_INT
  43. * H3 8 4Bit WKUP_INT
  44. * I 8 4Bit GPIO_INT15
  45. * J0 8 4Bit GPIO_INT16
  46. * J1 5 4Bit GPIO_INT17
  47. * J2 8 4Bit GPIO_INT18
  48. * J3 8 4Bit GPIO_INT19
  49. * J4 4 4Bit GPIO_INT20
  50. * K0 8 4Bit None
  51. * K1 6 4Bit None
  52. * K2 8 4Bit None
  53. * K3 8 4Bit None
  54. * L0 8 4Bit None
  55. * L1 8 4Bit None
  56. * L2 8 4Bit None
  57. * L3 8 4Bit None
  58. */
  59. #define OFF_GPCON (0x00)
  60. #define OFF_GPDAT (0x04)
  61. #define con_4bit_shift(__off) ((__off) * 4)
  62. #if 1
  63. #define gpio_dbg(x...) do { } while (0)
  64. #else
  65. #define gpio_dbg(x...) printk(KERN_DEBUG x)
  66. #endif
  67. /* The s5pc1xx_gpiolib routines are to control the gpio banks where
  68. * the gpio configuration register (GPxCON) has 4 bits per GPIO, as the
  69. * following example:
  70. *
  71. * base + 0x00: Control register, 4 bits per gpio
  72. * gpio n: 4 bits starting at (4*n)
  73. * 0000 = input, 0001 = output, others mean special-function
  74. * base + 0x04: Data register, 1 bit per gpio
  75. * bit n: data bit n
  76. *
  77. * Note, since the data register is one bit per gpio and is at base + 0x4
  78. * we can use s3c_gpiolib_get and s3c_gpiolib_set to change the state of
  79. * the output.
  80. */
  81. static int s5pc1xx_gpiolib_input(struct gpio_chip *chip, unsigned offset)
  82. {
  83. struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
  84. void __iomem *base = ourchip->base;
  85. unsigned long con;
  86. con = __raw_readl(base + OFF_GPCON);
  87. con &= ~(0xf << con_4bit_shift(offset));
  88. __raw_writel(con, base + OFF_GPCON);
  89. gpio_dbg("%s: %p: CON now %08lx\n", __func__, base, con);
  90. return 0;
  91. }
  92. static int s5pc1xx_gpiolib_output(struct gpio_chip *chip,
  93. unsigned offset, int value)
  94. {
  95. struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
  96. void __iomem *base = ourchip->base;
  97. unsigned long con;
  98. unsigned long dat;
  99. con = __raw_readl(base + OFF_GPCON);
  100. con &= ~(0xf << con_4bit_shift(offset));
  101. con |= 0x1 << con_4bit_shift(offset);
  102. dat = __raw_readl(base + OFF_GPDAT);
  103. if (value)
  104. dat |= 1 << offset;
  105. else
  106. dat &= ~(1 << offset);
  107. __raw_writel(dat, base + OFF_GPDAT);
  108. __raw_writel(con, base + OFF_GPCON);
  109. __raw_writel(dat, base + OFF_GPDAT);
  110. gpio_dbg("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);
  111. return 0;
  112. }
  113. static int s5pc1xx_gpiolib_to_irq(struct gpio_chip *chip, unsigned int offset)
  114. {
  115. return S3C_IRQ_GPIO(chip->base + offset);
  116. }
  117. static int s5pc1xx_gpiolib_to_eint(struct gpio_chip *chip, unsigned int offset)
  118. {
  119. int base;
  120. base = chip->base - S5PC100_GPH0(0);
  121. if (base == 0)
  122. return IRQ_EINT(offset);
  123. base = chip->base - S5PC100_GPH1(0);
  124. if (base == 0)
  125. return IRQ_EINT(8 + offset);
  126. base = chip->base - S5PC100_GPH2(0);
  127. if (base == 0)
  128. return IRQ_EINT(16 + offset);
  129. base = chip->base - S5PC100_GPH3(0);
  130. if (base == 0)
  131. return IRQ_EINT(24 + offset);
  132. return -EINVAL;
  133. }
  134. static struct s3c_gpio_cfg gpio_cfg = {
  135. .set_config = s3c_gpio_setcfg_s3c64xx_4bit,
  136. .set_pull = s3c_gpio_setpull_updown,
  137. .get_pull = s3c_gpio_getpull_updown,
  138. };
  139. static struct s3c_gpio_cfg gpio_cfg_eint = {
  140. .cfg_eint = 0xf,
  141. .set_config = s3c_gpio_setcfg_s3c64xx_4bit,
  142. .set_pull = s3c_gpio_setpull_updown,
  143. .get_pull = s3c_gpio_getpull_updown,
  144. };
  145. static struct s3c_gpio_cfg gpio_cfg_noint = {
  146. .set_config = s3c_gpio_setcfg_s3c64xx_4bit,
  147. .set_pull = s3c_gpio_setpull_updown,
  148. .get_pull = s3c_gpio_getpull_updown,
  149. };
  150. static struct s3c_gpio_chip s5pc100_gpio_chips[] = {
  151. {
  152. .base = S5PC100_GPA0_BASE,
  153. .config = &gpio_cfg,
  154. .chip = {
  155. .base = S5PC100_GPA0(0),
  156. .ngpio = S5PC100_GPIO_A0_NR,
  157. .label = "GPA0",
  158. },
  159. }, {
  160. .base = S5PC100_GPA1_BASE,
  161. .config = &gpio_cfg,
  162. .chip = {
  163. .base = S5PC100_GPA1(0),
  164. .ngpio = S5PC100_GPIO_A1_NR,
  165. .label = "GPA1",
  166. },
  167. }, {
  168. .base = S5PC100_GPB_BASE,
  169. .config = &gpio_cfg,
  170. .chip = {
  171. .base = S5PC100_GPB(0),
  172. .ngpio = S5PC100_GPIO_B_NR,
  173. .label = "GPB",
  174. },
  175. }, {
  176. .base = S5PC100_GPC_BASE,
  177. .config = &gpio_cfg,
  178. .chip = {
  179. .base = S5PC100_GPC(0),
  180. .ngpio = S5PC100_GPIO_C_NR,
  181. .label = "GPC",
  182. },
  183. }, {
  184. .base = S5PC100_GPD_BASE,
  185. .config = &gpio_cfg,
  186. .chip = {
  187. .base = S5PC100_GPD(0),
  188. .ngpio = S5PC100_GPIO_D_NR,
  189. .label = "GPD",
  190. },
  191. }, {
  192. .base = S5PC100_GPE0_BASE,
  193. .config = &gpio_cfg,
  194. .chip = {
  195. .base = S5PC100_GPE0(0),
  196. .ngpio = S5PC100_GPIO_E0_NR,
  197. .label = "GPE0",
  198. },
  199. }, {
  200. .base = S5PC100_GPE1_BASE,
  201. .config = &gpio_cfg,
  202. .chip = {
  203. .base = S5PC100_GPE1(0),
  204. .ngpio = S5PC100_GPIO_E1_NR,
  205. .label = "GPE1",
  206. },
  207. }, {
  208. .base = S5PC100_GPF0_BASE,
  209. .config = &gpio_cfg,
  210. .chip = {
  211. .base = S5PC100_GPF0(0),
  212. .ngpio = S5PC100_GPIO_F0_NR,
  213. .label = "GPF0",
  214. },
  215. }, {
  216. .base = S5PC100_GPF1_BASE,
  217. .config = &gpio_cfg,
  218. .chip = {
  219. .base = S5PC100_GPF1(0),
  220. .ngpio = S5PC100_GPIO_F1_NR,
  221. .label = "GPF1",
  222. },
  223. }, {
  224. .base = S5PC100_GPF2_BASE,
  225. .config = &gpio_cfg,
  226. .chip = {
  227. .base = S5PC100_GPF2(0),
  228. .ngpio = S5PC100_GPIO_F2_NR,
  229. .label = "GPF2",
  230. },
  231. }, {
  232. .base = S5PC100_GPF3_BASE,
  233. .config = &gpio_cfg,
  234. .chip = {
  235. .base = S5PC100_GPF3(0),
  236. .ngpio = S5PC100_GPIO_F3_NR,
  237. .label = "GPF3",
  238. },
  239. }, {
  240. .base = S5PC100_GPG0_BASE,
  241. .config = &gpio_cfg,
  242. .chip = {
  243. .base = S5PC100_GPG0(0),
  244. .ngpio = S5PC100_GPIO_G0_NR,
  245. .label = "GPG0",
  246. },
  247. }, {
  248. .base = S5PC100_GPG1_BASE,
  249. .config = &gpio_cfg,
  250. .chip = {
  251. .base = S5PC100_GPG1(0),
  252. .ngpio = S5PC100_GPIO_G1_NR,
  253. .label = "GPG1",
  254. },
  255. }, {
  256. .base = S5PC100_GPG2_BASE,
  257. .config = &gpio_cfg,
  258. .chip = {
  259. .base = S5PC100_GPG2(0),
  260. .ngpio = S5PC100_GPIO_G2_NR,
  261. .label = "GPG2",
  262. },
  263. }, {
  264. .base = S5PC100_GPG3_BASE,
  265. .config = &gpio_cfg,
  266. .chip = {
  267. .base = S5PC100_GPG3(0),
  268. .ngpio = S5PC100_GPIO_G3_NR,
  269. .label = "GPG3",
  270. },
  271. }, {
  272. .base = S5PC100_GPH0_BASE,
  273. .config = &gpio_cfg_eint,
  274. .chip = {
  275. .base = S5PC100_GPH0(0),
  276. .ngpio = S5PC100_GPIO_H0_NR,
  277. .label = "GPH0",
  278. },
  279. }, {
  280. .base = S5PC100_GPH1_BASE,
  281. .config = &gpio_cfg_eint,
  282. .chip = {
  283. .base = S5PC100_GPH1(0),
  284. .ngpio = S5PC100_GPIO_H1_NR,
  285. .label = "GPH1",
  286. },
  287. }, {
  288. .base = S5PC100_GPH2_BASE,
  289. .config = &gpio_cfg_eint,
  290. .chip = {
  291. .base = S5PC100_GPH2(0),
  292. .ngpio = S5PC100_GPIO_H2_NR,
  293. .label = "GPH2",
  294. },
  295. }, {
  296. .base = S5PC100_GPH3_BASE,
  297. .config = &gpio_cfg_eint,
  298. .chip = {
  299. .base = S5PC100_GPH3(0),
  300. .ngpio = S5PC100_GPIO_H3_NR,
  301. .label = "GPH3",
  302. },
  303. }, {
  304. .base = S5PC100_GPI_BASE,
  305. .config = &gpio_cfg,
  306. .chip = {
  307. .base = S5PC100_GPI(0),
  308. .ngpio = S5PC100_GPIO_I_NR,
  309. .label = "GPI",
  310. },
  311. }, {
  312. .base = S5PC100_GPJ0_BASE,
  313. .config = &gpio_cfg,
  314. .chip = {
  315. .base = S5PC100_GPJ0(0),
  316. .ngpio = S5PC100_GPIO_J0_NR,
  317. .label = "GPJ0",
  318. },
  319. }, {
  320. .base = S5PC100_GPJ1_BASE,
  321. .config = &gpio_cfg,
  322. .chip = {
  323. .base = S5PC100_GPJ1(0),
  324. .ngpio = S5PC100_GPIO_J1_NR,
  325. .label = "GPJ1",
  326. },
  327. }, {
  328. .base = S5PC100_GPJ2_BASE,
  329. .config = &gpio_cfg,
  330. .chip = {
  331. .base = S5PC100_GPJ2(0),
  332. .ngpio = S5PC100_GPIO_J2_NR,
  333. .label = "GPJ2",
  334. },
  335. }, {
  336. .base = S5PC100_GPJ3_BASE,
  337. .config = &gpio_cfg,
  338. .chip = {
  339. .base = S5PC100_GPJ3(0),
  340. .ngpio = S5PC100_GPIO_J3_NR,
  341. .label = "GPJ3",
  342. },
  343. }, {
  344. .base = S5PC100_GPJ4_BASE,
  345. .config = &gpio_cfg,
  346. .chip = {
  347. .base = S5PC100_GPJ4(0),
  348. .ngpio = S5PC100_GPIO_J4_NR,
  349. .label = "GPJ4",
  350. },
  351. }, {
  352. .base = S5PC100_GPK0_BASE,
  353. .config = &gpio_cfg_noint,
  354. .chip = {
  355. .base = S5PC100_GPK0(0),
  356. .ngpio = S5PC100_GPIO_K0_NR,
  357. .label = "GPK0",
  358. },
  359. }, {
  360. .base = S5PC100_GPK1_BASE,
  361. .config = &gpio_cfg_noint,
  362. .chip = {
  363. .base = S5PC100_GPK1(0),
  364. .ngpio = S5PC100_GPIO_K1_NR,
  365. .label = "GPK1",
  366. },
  367. }, {
  368. .base = S5PC100_GPK2_BASE,
  369. .config = &gpio_cfg_noint,
  370. .chip = {
  371. .base = S5PC100_GPK2(0),
  372. .ngpio = S5PC100_GPIO_K2_NR,
  373. .label = "GPK2",
  374. },
  375. }, {
  376. .base = S5PC100_GPK3_BASE,
  377. .config = &gpio_cfg_noint,
  378. .chip = {
  379. .base = S5PC100_GPK3(0),
  380. .ngpio = S5PC100_GPIO_K3_NR,
  381. .label = "GPK3",
  382. },
  383. }, {
  384. .base = S5PC100_GPL0_BASE,
  385. .config = &gpio_cfg_noint,
  386. .chip = {
  387. .base = S5PC100_GPL0(0),
  388. .ngpio = S5PC100_GPIO_L0_NR,
  389. .label = "GPL0",
  390. },
  391. }, {
  392. .base = S5PC100_GPL1_BASE,
  393. .config = &gpio_cfg_noint,
  394. .chip = {
  395. .base = S5PC100_GPL1(0),
  396. .ngpio = S5PC100_GPIO_L1_NR,
  397. .label = "GPL1",
  398. },
  399. }, {
  400. .base = S5PC100_GPL2_BASE,
  401. .config = &gpio_cfg_noint,
  402. .chip = {
  403. .base = S5PC100_GPL2(0),
  404. .ngpio = S5PC100_GPIO_L2_NR,
  405. .label = "GPL2",
  406. },
  407. }, {
  408. .base = S5PC100_GPL3_BASE,
  409. .config = &gpio_cfg_noint,
  410. .chip = {
  411. .base = S5PC100_GPL3(0),
  412. .ngpio = S5PC100_GPIO_L3_NR,
  413. .label = "GPL3",
  414. },
  415. }, {
  416. .base = S5PC100_GPL4_BASE,
  417. .config = &gpio_cfg_noint,
  418. .chip = {
  419. .base = S5PC100_GPL4(0),
  420. .ngpio = S5PC100_GPIO_L4_NR,
  421. .label = "GPL4",
  422. },
  423. },
  424. };
  425. /* FIXME move from irq-gpio.c */
  426. extern struct irq_chip s5pc1xx_gpioint;
  427. extern void s5pc1xx_irq_gpioint_handler(unsigned int irq, struct irq_desc *desc);
  428. static __init void s5pc1xx_gpiolib_link(struct s3c_gpio_chip *chip)
  429. {
  430. chip->chip.direction_input = s5pc1xx_gpiolib_input;
  431. chip->chip.direction_output = s5pc1xx_gpiolib_output;
  432. chip->pm = __gpio_pm(&s3c_gpio_pm_4bit);
  433. /* Interrupt */
  434. if (chip->config == &gpio_cfg) {
  435. int i, irq;
  436. chip->chip.to_irq = s5pc1xx_gpiolib_to_irq;
  437. for (i = 0; i < chip->chip.ngpio; i++) {
  438. irq = S3C_IRQ_GPIO_BASE + chip->chip.base + i;
  439. set_irq_chip(irq, &s5pc1xx_gpioint);
  440. set_irq_data(irq, &chip->chip);
  441. set_irq_handler(irq, handle_level_irq);
  442. set_irq_flags(irq, IRQF_VALID);
  443. }
  444. } else if (chip->config == &gpio_cfg_eint)
  445. chip->chip.to_irq = s5pc1xx_gpiolib_to_eint;
  446. }
  447. static __init void s5pc1xx_gpiolib_add(struct s3c_gpio_chip *chips,
  448. int nr_chips,
  449. void (*fn)(struct s3c_gpio_chip *))
  450. {
  451. for (; nr_chips > 0; nr_chips--, chips++) {
  452. if (fn)
  453. (fn)(chips);
  454. s3c_gpiolib_add(chips);
  455. }
  456. }
  457. static __init int s5pc1xx_gpiolib_init(void)
  458. {
  459. struct s3c_gpio_chip *chips;
  460. int nr_chips;
  461. chips = s5pc100_gpio_chips;
  462. nr_chips = ARRAY_SIZE(s5pc100_gpio_chips);
  463. s5pc1xx_gpiolib_add(chips, nr_chips, s5pc1xx_gpiolib_link);
  464. /* Interrupt */
  465. set_irq_chained_handler(IRQ_GPIOINT, s5pc1xx_irq_gpioint_handler);
  466. return 0;
  467. }
  468. core_initcall(s5pc1xx_gpiolib_init);