gpio-lpc32xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * arch/arm/mach-lpc32xx/gpiolib.c
  3. *
  4. * Author: Kevin Wells <kevin.wells@nxp.com>
  5. *
  6. * Copyright (C) 2010 NXP Semiconductors
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <linux/errno.h>
  22. #include <linux/gpio.h>
  23. #include <mach/hardware.h>
  24. #include <mach/platform.h>
  25. #include <mach/gpio-lpc32xx.h>
  26. #define LPC32XX_GPIO_P3_INP_STATE _GPREG(0x000)
  27. #define LPC32XX_GPIO_P3_OUTP_SET _GPREG(0x004)
  28. #define LPC32XX_GPIO_P3_OUTP_CLR _GPREG(0x008)
  29. #define LPC32XX_GPIO_P3_OUTP_STATE _GPREG(0x00C)
  30. #define LPC32XX_GPIO_P2_DIR_SET _GPREG(0x010)
  31. #define LPC32XX_GPIO_P2_DIR_CLR _GPREG(0x014)
  32. #define LPC32XX_GPIO_P2_DIR_STATE _GPREG(0x018)
  33. #define LPC32XX_GPIO_P2_INP_STATE _GPREG(0x01C)
  34. #define LPC32XX_GPIO_P2_OUTP_SET _GPREG(0x020)
  35. #define LPC32XX_GPIO_P2_OUTP_CLR _GPREG(0x024)
  36. #define LPC32XX_GPIO_P2_MUX_SET _GPREG(0x028)
  37. #define LPC32XX_GPIO_P2_MUX_CLR _GPREG(0x02C)
  38. #define LPC32XX_GPIO_P2_MUX_STATE _GPREG(0x030)
  39. #define LPC32XX_GPIO_P0_INP_STATE _GPREG(0x040)
  40. #define LPC32XX_GPIO_P0_OUTP_SET _GPREG(0x044)
  41. #define LPC32XX_GPIO_P0_OUTP_CLR _GPREG(0x048)
  42. #define LPC32XX_GPIO_P0_OUTP_STATE _GPREG(0x04C)
  43. #define LPC32XX_GPIO_P0_DIR_SET _GPREG(0x050)
  44. #define LPC32XX_GPIO_P0_DIR_CLR _GPREG(0x054)
  45. #define LPC32XX_GPIO_P0_DIR_STATE _GPREG(0x058)
  46. #define LPC32XX_GPIO_P1_INP_STATE _GPREG(0x060)
  47. #define LPC32XX_GPIO_P1_OUTP_SET _GPREG(0x064)
  48. #define LPC32XX_GPIO_P1_OUTP_CLR _GPREG(0x068)
  49. #define LPC32XX_GPIO_P1_OUTP_STATE _GPREG(0x06C)
  50. #define LPC32XX_GPIO_P1_DIR_SET _GPREG(0x070)
  51. #define LPC32XX_GPIO_P1_DIR_CLR _GPREG(0x074)
  52. #define LPC32XX_GPIO_P1_DIR_STATE _GPREG(0x078)
  53. #define GPIO012_PIN_TO_BIT(x) (1 << (x))
  54. #define GPIO3_PIN_TO_BIT(x) (1 << ((x) + 25))
  55. #define GPO3_PIN_TO_BIT(x) (1 << (x))
  56. #define GPIO012_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
  57. #define GPIO3_PIN_IN_SHIFT(x) ((x) == 5 ? 24 : 10 + (x))
  58. #define GPIO3_PIN_IN_SEL(x, y) (((x) >> GPIO3_PIN_IN_SHIFT(y)) & 1)
  59. #define GPIO3_PIN5_IN_SEL(x) (((x) >> 24) & 1)
  60. #define GPI3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
  61. #define GPO3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
  62. struct gpio_regs {
  63. void __iomem *inp_state;
  64. void __iomem *outp_state;
  65. void __iomem *outp_set;
  66. void __iomem *outp_clr;
  67. void __iomem *dir_set;
  68. void __iomem *dir_clr;
  69. };
  70. /*
  71. * GPIO names
  72. */
  73. static const char *gpio_p0_names[LPC32XX_GPIO_P0_MAX] = {
  74. "p0.0", "p0.1", "p0.2", "p0.3",
  75. "p0.4", "p0.5", "p0.6", "p0.7"
  76. };
  77. static const char *gpio_p1_names[LPC32XX_GPIO_P1_MAX] = {
  78. "p1.0", "p1.1", "p1.2", "p1.3",
  79. "p1.4", "p1.5", "p1.6", "p1.7",
  80. "p1.8", "p1.9", "p1.10", "p1.11",
  81. "p1.12", "p1.13", "p1.14", "p1.15",
  82. "p1.16", "p1.17", "p1.18", "p1.19",
  83. "p1.20", "p1.21", "p1.22", "p1.23",
  84. };
  85. static const char *gpio_p2_names[LPC32XX_GPIO_P2_MAX] = {
  86. "p2.0", "p2.1", "p2.2", "p2.3",
  87. "p2.4", "p2.5", "p2.6", "p2.7",
  88. "p2.8", "p2.9", "p2.10", "p2.11",
  89. "p2.12"
  90. };
  91. static const char *gpio_p3_names[LPC32XX_GPIO_P3_MAX] = {
  92. "gpio00", "gpio01", "gpio02", "gpio03",
  93. "gpio04", "gpio05"
  94. };
  95. static const char *gpi_p3_names[LPC32XX_GPI_P3_MAX] = {
  96. "gpi00", "gpi01", "gpi02", "gpi03",
  97. "gpi04", "gpi05", "gpi06", "gpi07",
  98. "gpi08", "gpi09", NULL, NULL,
  99. NULL, NULL, NULL, "gpi15",
  100. "gpi16", "gpi17", "gpi18", "gpi19",
  101. "gpi20", "gpi21", "gpi22", "gpi23",
  102. "gpi24", "gpi25", "gpi26", "gpi27"
  103. };
  104. static const char *gpo_p3_names[LPC32XX_GPO_P3_MAX] = {
  105. "gpo00", "gpo01", "gpo02", "gpo03",
  106. "gpo04", "gpo05", "gpo06", "gpo07",
  107. "gpo08", "gpo09", "gpo10", "gpo11",
  108. "gpo12", "gpo13", "gpo14", "gpo15",
  109. "gpo16", "gpo17", "gpo18", "gpo19",
  110. "gpo20", "gpo21", "gpo22", "gpo23"
  111. };
  112. static struct gpio_regs gpio_grp_regs_p0 = {
  113. .inp_state = LPC32XX_GPIO_P0_INP_STATE,
  114. .outp_set = LPC32XX_GPIO_P0_OUTP_SET,
  115. .outp_clr = LPC32XX_GPIO_P0_OUTP_CLR,
  116. .dir_set = LPC32XX_GPIO_P0_DIR_SET,
  117. .dir_clr = LPC32XX_GPIO_P0_DIR_CLR,
  118. };
  119. static struct gpio_regs gpio_grp_regs_p1 = {
  120. .inp_state = LPC32XX_GPIO_P1_INP_STATE,
  121. .outp_set = LPC32XX_GPIO_P1_OUTP_SET,
  122. .outp_clr = LPC32XX_GPIO_P1_OUTP_CLR,
  123. .dir_set = LPC32XX_GPIO_P1_DIR_SET,
  124. .dir_clr = LPC32XX_GPIO_P1_DIR_CLR,
  125. };
  126. static struct gpio_regs gpio_grp_regs_p2 = {
  127. .inp_state = LPC32XX_GPIO_P2_INP_STATE,
  128. .outp_set = LPC32XX_GPIO_P2_OUTP_SET,
  129. .outp_clr = LPC32XX_GPIO_P2_OUTP_CLR,
  130. .dir_set = LPC32XX_GPIO_P2_DIR_SET,
  131. .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
  132. };
  133. static struct gpio_regs gpio_grp_regs_p3 = {
  134. .inp_state = LPC32XX_GPIO_P3_INP_STATE,
  135. .outp_state = LPC32XX_GPIO_P3_OUTP_STATE,
  136. .outp_set = LPC32XX_GPIO_P3_OUTP_SET,
  137. .outp_clr = LPC32XX_GPIO_P3_OUTP_CLR,
  138. .dir_set = LPC32XX_GPIO_P2_DIR_SET,
  139. .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
  140. };
  141. struct lpc32xx_gpio_chip {
  142. struct gpio_chip chip;
  143. struct gpio_regs *gpio_grp;
  144. };
  145. static inline struct lpc32xx_gpio_chip *to_lpc32xx_gpio(
  146. struct gpio_chip *gpc)
  147. {
  148. return container_of(gpc, struct lpc32xx_gpio_chip, chip);
  149. }
  150. static void __set_gpio_dir_p012(struct lpc32xx_gpio_chip *group,
  151. unsigned pin, int input)
  152. {
  153. if (input)
  154. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  155. group->gpio_grp->dir_clr);
  156. else
  157. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  158. group->gpio_grp->dir_set);
  159. }
  160. static void __set_gpio_dir_p3(struct lpc32xx_gpio_chip *group,
  161. unsigned pin, int input)
  162. {
  163. u32 u = GPIO3_PIN_TO_BIT(pin);
  164. if (input)
  165. __raw_writel(u, group->gpio_grp->dir_clr);
  166. else
  167. __raw_writel(u, group->gpio_grp->dir_set);
  168. }
  169. static void __set_gpio_level_p012(struct lpc32xx_gpio_chip *group,
  170. unsigned pin, int high)
  171. {
  172. if (high)
  173. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  174. group->gpio_grp->outp_set);
  175. else
  176. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  177. group->gpio_grp->outp_clr);
  178. }
  179. static void __set_gpio_level_p3(struct lpc32xx_gpio_chip *group,
  180. unsigned pin, int high)
  181. {
  182. u32 u = GPIO3_PIN_TO_BIT(pin);
  183. if (high)
  184. __raw_writel(u, group->gpio_grp->outp_set);
  185. else
  186. __raw_writel(u, group->gpio_grp->outp_clr);
  187. }
  188. static void __set_gpo_level_p3(struct lpc32xx_gpio_chip *group,
  189. unsigned pin, int high)
  190. {
  191. if (high)
  192. __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_set);
  193. else
  194. __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_clr);
  195. }
  196. static int __get_gpio_state_p012(struct lpc32xx_gpio_chip *group,
  197. unsigned pin)
  198. {
  199. return GPIO012_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state),
  200. pin);
  201. }
  202. static int __get_gpio_state_p3(struct lpc32xx_gpio_chip *group,
  203. unsigned pin)
  204. {
  205. int state = __raw_readl(group->gpio_grp->inp_state);
  206. /*
  207. * P3 GPIO pin input mapping is not contiguous, GPIOP3-0..4 is mapped
  208. * to bits 10..14, while GPIOP3-5 is mapped to bit 24.
  209. */
  210. return GPIO3_PIN_IN_SEL(state, pin);
  211. }
  212. static int __get_gpi_state_p3(struct lpc32xx_gpio_chip *group,
  213. unsigned pin)
  214. {
  215. return GPI3_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state), pin);
  216. }
  217. static int __get_gpo_state_p3(struct lpc32xx_gpio_chip *group,
  218. unsigned pin)
  219. {
  220. return GPO3_PIN_IN_SEL(__raw_readl(group->gpio_grp->outp_state), pin);
  221. }
  222. /*
  223. * GENERIC_GPIO primitives.
  224. */
  225. static int lpc32xx_gpio_dir_input_p012(struct gpio_chip *chip,
  226. unsigned pin)
  227. {
  228. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  229. __set_gpio_dir_p012(group, pin, 1);
  230. return 0;
  231. }
  232. static int lpc32xx_gpio_dir_input_p3(struct gpio_chip *chip,
  233. unsigned pin)
  234. {
  235. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  236. __set_gpio_dir_p3(group, pin, 1);
  237. return 0;
  238. }
  239. static int lpc32xx_gpio_dir_in_always(struct gpio_chip *chip,
  240. unsigned pin)
  241. {
  242. return 0;
  243. }
  244. static int lpc32xx_gpio_get_value_p012(struct gpio_chip *chip, unsigned pin)
  245. {
  246. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  247. return __get_gpio_state_p012(group, pin);
  248. }
  249. static int lpc32xx_gpio_get_value_p3(struct gpio_chip *chip, unsigned pin)
  250. {
  251. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  252. return __get_gpio_state_p3(group, pin);
  253. }
  254. static int lpc32xx_gpi_get_value(struct gpio_chip *chip, unsigned pin)
  255. {
  256. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  257. return __get_gpi_state_p3(group, pin);
  258. }
  259. static int lpc32xx_gpio_dir_output_p012(struct gpio_chip *chip, unsigned pin,
  260. int value)
  261. {
  262. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  263. __set_gpio_level_p012(group, pin, value);
  264. __set_gpio_dir_p012(group, pin, 0);
  265. return 0;
  266. }
  267. static int lpc32xx_gpio_dir_output_p3(struct gpio_chip *chip, unsigned pin,
  268. int value)
  269. {
  270. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  271. __set_gpio_level_p3(group, pin, value);
  272. __set_gpio_dir_p3(group, pin, 0);
  273. return 0;
  274. }
  275. static int lpc32xx_gpio_dir_out_always(struct gpio_chip *chip, unsigned pin,
  276. int value)
  277. {
  278. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  279. __set_gpo_level_p3(group, pin, value);
  280. return 0;
  281. }
  282. static void lpc32xx_gpio_set_value_p012(struct gpio_chip *chip, unsigned pin,
  283. int value)
  284. {
  285. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  286. __set_gpio_level_p012(group, pin, value);
  287. }
  288. static void lpc32xx_gpio_set_value_p3(struct gpio_chip *chip, unsigned pin,
  289. int value)
  290. {
  291. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  292. __set_gpio_level_p3(group, pin, value);
  293. }
  294. static void lpc32xx_gpo_set_value(struct gpio_chip *chip, unsigned pin,
  295. int value)
  296. {
  297. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  298. __set_gpo_level_p3(group, pin, value);
  299. }
  300. static int lpc32xx_gpo_get_value(struct gpio_chip *chip, unsigned pin)
  301. {
  302. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  303. return __get_gpo_state_p3(group, pin);
  304. }
  305. static int lpc32xx_gpio_request(struct gpio_chip *chip, unsigned pin)
  306. {
  307. if (pin < chip->ngpio)
  308. return 0;
  309. return -EINVAL;
  310. }
  311. static struct lpc32xx_gpio_chip lpc32xx_gpiochip[] = {
  312. {
  313. .chip = {
  314. .label = "gpio_p0",
  315. .direction_input = lpc32xx_gpio_dir_input_p012,
  316. .get = lpc32xx_gpio_get_value_p012,
  317. .direction_output = lpc32xx_gpio_dir_output_p012,
  318. .set = lpc32xx_gpio_set_value_p012,
  319. .request = lpc32xx_gpio_request,
  320. .base = LPC32XX_GPIO_P0_GRP,
  321. .ngpio = LPC32XX_GPIO_P0_MAX,
  322. .names = gpio_p0_names,
  323. .can_sleep = 0,
  324. },
  325. .gpio_grp = &gpio_grp_regs_p0,
  326. },
  327. {
  328. .chip = {
  329. .label = "gpio_p1",
  330. .direction_input = lpc32xx_gpio_dir_input_p012,
  331. .get = lpc32xx_gpio_get_value_p012,
  332. .direction_output = lpc32xx_gpio_dir_output_p012,
  333. .set = lpc32xx_gpio_set_value_p012,
  334. .request = lpc32xx_gpio_request,
  335. .base = LPC32XX_GPIO_P1_GRP,
  336. .ngpio = LPC32XX_GPIO_P1_MAX,
  337. .names = gpio_p1_names,
  338. .can_sleep = 0,
  339. },
  340. .gpio_grp = &gpio_grp_regs_p1,
  341. },
  342. {
  343. .chip = {
  344. .label = "gpio_p2",
  345. .direction_input = lpc32xx_gpio_dir_input_p012,
  346. .get = lpc32xx_gpio_get_value_p012,
  347. .direction_output = lpc32xx_gpio_dir_output_p012,
  348. .set = lpc32xx_gpio_set_value_p012,
  349. .request = lpc32xx_gpio_request,
  350. .base = LPC32XX_GPIO_P2_GRP,
  351. .ngpio = LPC32XX_GPIO_P2_MAX,
  352. .names = gpio_p2_names,
  353. .can_sleep = 0,
  354. },
  355. .gpio_grp = &gpio_grp_regs_p2,
  356. },
  357. {
  358. .chip = {
  359. .label = "gpio_p3",
  360. .direction_input = lpc32xx_gpio_dir_input_p3,
  361. .get = lpc32xx_gpio_get_value_p3,
  362. .direction_output = lpc32xx_gpio_dir_output_p3,
  363. .set = lpc32xx_gpio_set_value_p3,
  364. .request = lpc32xx_gpio_request,
  365. .base = LPC32XX_GPIO_P3_GRP,
  366. .ngpio = LPC32XX_GPIO_P3_MAX,
  367. .names = gpio_p3_names,
  368. .can_sleep = 0,
  369. },
  370. .gpio_grp = &gpio_grp_regs_p3,
  371. },
  372. {
  373. .chip = {
  374. .label = "gpi_p3",
  375. .direction_input = lpc32xx_gpio_dir_in_always,
  376. .get = lpc32xx_gpi_get_value,
  377. .request = lpc32xx_gpio_request,
  378. .base = LPC32XX_GPI_P3_GRP,
  379. .ngpio = LPC32XX_GPI_P3_MAX,
  380. .names = gpi_p3_names,
  381. .can_sleep = 0,
  382. },
  383. .gpio_grp = &gpio_grp_regs_p3,
  384. },
  385. {
  386. .chip = {
  387. .label = "gpo_p3",
  388. .direction_output = lpc32xx_gpio_dir_out_always,
  389. .set = lpc32xx_gpo_set_value,
  390. .get = lpc32xx_gpo_get_value,
  391. .request = lpc32xx_gpio_request,
  392. .base = LPC32XX_GPO_P3_GRP,
  393. .ngpio = LPC32XX_GPO_P3_MAX,
  394. .names = gpo_p3_names,
  395. .can_sleep = 0,
  396. },
  397. .gpio_grp = &gpio_grp_regs_p3,
  398. },
  399. };
  400. void __init lpc32xx_gpio_init(void)
  401. {
  402. int i;
  403. for (i = 0; i < ARRAY_SIZE(lpc32xx_gpiochip); i++)
  404. gpiochip_add(&lpc32xx_gpiochip[i].chip);
  405. }