gpiolib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 "common.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))
  59. #define GPIO3_PIN5_IN_SEL(x) (((x) >> 24) & 1)
  60. #define GPI3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
  61. struct gpio_regs {
  62. void __iomem *inp_state;
  63. void __iomem *outp_set;
  64. void __iomem *outp_clr;
  65. void __iomem *dir_set;
  66. void __iomem *dir_clr;
  67. };
  68. /*
  69. * GPIO names
  70. */
  71. static const char *gpio_p0_names[LPC32XX_GPIO_P0_MAX] = {
  72. "p0.0", "p0.1", "p0.2", "p0.3",
  73. "p0.4", "p0.5", "p0.6", "p0.7"
  74. };
  75. static const char *gpio_p1_names[LPC32XX_GPIO_P1_MAX] = {
  76. "p1.0", "p1.1", "p1.2", "p1.3",
  77. "p1.4", "p1.5", "p1.6", "p1.7",
  78. "p1.8", "p1.9", "p1.10", "p1.11",
  79. "p1.12", "p1.13", "p1.14", "p1.15",
  80. "p1.16", "p1.17", "p1.18", "p1.19",
  81. "p1.20", "p1.21", "p1.22", "p1.23",
  82. };
  83. static const char *gpio_p2_names[LPC32XX_GPIO_P2_MAX] = {
  84. "p2.0", "p2.1", "p2.2", "p2.3",
  85. "p2.4", "p2.5", "p2.6", "p2.7",
  86. "p2.8", "p2.9", "p2.10", "p2.11",
  87. "p2.12"
  88. };
  89. static const char *gpio_p3_names[LPC32XX_GPIO_P3_MAX] = {
  90. "gpi000", "gpio01", "gpio02", "gpio03",
  91. "gpio04", "gpio05"
  92. };
  93. static const char *gpi_p3_names[LPC32XX_GPI_P3_MAX] = {
  94. "gpi00", "gpi01", "gpi02", "gpi03",
  95. "gpi04", "gpi05", "gpi06", "gpi07",
  96. "gpi08", "gpi09", NULL, NULL,
  97. NULL, NULL, NULL, "gpi15",
  98. "gpi16", "gpi17", "gpi18", "gpi19",
  99. "gpi20", "gpi21", "gpi22", "gpi23",
  100. "gpi24", "gpi25", "gpi26", "gpi27"
  101. };
  102. static const char *gpo_p3_names[LPC32XX_GPO_P3_MAX] = {
  103. "gpo00", "gpo01", "gpo02", "gpo03",
  104. "gpo04", "gpo05", "gpo06", "gpo07",
  105. "gpo08", "gpo09", "gpo10", "gpo11",
  106. "gpo12", "gpo13", "gpo14", "gpo15",
  107. "gpo16", "gpo17", "gpo18", "gpo19",
  108. "gpo20", "gpo21", "gpo22", "gpo23"
  109. };
  110. static struct gpio_regs gpio_grp_regs_p0 = {
  111. .inp_state = LPC32XX_GPIO_P0_INP_STATE,
  112. .outp_set = LPC32XX_GPIO_P0_OUTP_SET,
  113. .outp_clr = LPC32XX_GPIO_P0_OUTP_CLR,
  114. .dir_set = LPC32XX_GPIO_P0_DIR_SET,
  115. .dir_clr = LPC32XX_GPIO_P0_DIR_CLR,
  116. };
  117. static struct gpio_regs gpio_grp_regs_p1 = {
  118. .inp_state = LPC32XX_GPIO_P1_INP_STATE,
  119. .outp_set = LPC32XX_GPIO_P1_OUTP_SET,
  120. .outp_clr = LPC32XX_GPIO_P1_OUTP_CLR,
  121. .dir_set = LPC32XX_GPIO_P1_DIR_SET,
  122. .dir_clr = LPC32XX_GPIO_P1_DIR_CLR,
  123. };
  124. static struct gpio_regs gpio_grp_regs_p2 = {
  125. .inp_state = LPC32XX_GPIO_P2_INP_STATE,
  126. .outp_set = LPC32XX_GPIO_P2_OUTP_SET,
  127. .outp_clr = LPC32XX_GPIO_P2_OUTP_CLR,
  128. .dir_set = LPC32XX_GPIO_P2_DIR_SET,
  129. .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
  130. };
  131. static struct gpio_regs gpio_grp_regs_p3 = {
  132. .inp_state = LPC32XX_GPIO_P3_INP_STATE,
  133. .outp_set = LPC32XX_GPIO_P3_OUTP_SET,
  134. .outp_clr = LPC32XX_GPIO_P3_OUTP_CLR,
  135. .dir_set = LPC32XX_GPIO_P2_DIR_SET,
  136. .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
  137. };
  138. struct lpc32xx_gpio_chip {
  139. struct gpio_chip chip;
  140. struct gpio_regs *gpio_grp;
  141. };
  142. static inline struct lpc32xx_gpio_chip *to_lpc32xx_gpio(
  143. struct gpio_chip *gpc)
  144. {
  145. return container_of(gpc, struct lpc32xx_gpio_chip, chip);
  146. }
  147. static void __set_gpio_dir_p012(struct lpc32xx_gpio_chip *group,
  148. unsigned pin, int input)
  149. {
  150. if (input)
  151. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  152. group->gpio_grp->dir_clr);
  153. else
  154. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  155. group->gpio_grp->dir_set);
  156. }
  157. static void __set_gpio_dir_p3(struct lpc32xx_gpio_chip *group,
  158. unsigned pin, int input)
  159. {
  160. u32 u = GPIO3_PIN_TO_BIT(pin);
  161. if (input)
  162. __raw_writel(u, group->gpio_grp->dir_clr);
  163. else
  164. __raw_writel(u, group->gpio_grp->dir_set);
  165. }
  166. static void __set_gpio_level_p012(struct lpc32xx_gpio_chip *group,
  167. unsigned pin, int high)
  168. {
  169. if (high)
  170. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  171. group->gpio_grp->outp_set);
  172. else
  173. __raw_writel(GPIO012_PIN_TO_BIT(pin),
  174. group->gpio_grp->outp_clr);
  175. }
  176. static void __set_gpio_level_p3(struct lpc32xx_gpio_chip *group,
  177. unsigned pin, int high)
  178. {
  179. u32 u = GPIO3_PIN_TO_BIT(pin);
  180. if (high)
  181. __raw_writel(u, group->gpio_grp->outp_set);
  182. else
  183. __raw_writel(u, group->gpio_grp->outp_clr);
  184. }
  185. static void __set_gpo_level_p3(struct lpc32xx_gpio_chip *group,
  186. unsigned pin, int high)
  187. {
  188. if (high)
  189. __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_set);
  190. else
  191. __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_clr);
  192. }
  193. static int __get_gpio_state_p012(struct lpc32xx_gpio_chip *group,
  194. unsigned pin)
  195. {
  196. return GPIO012_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state),
  197. pin);
  198. }
  199. static int __get_gpio_state_p3(struct lpc32xx_gpio_chip *group,
  200. unsigned pin)
  201. {
  202. int state = __raw_readl(group->gpio_grp->inp_state);
  203. /*
  204. * P3 GPIO pin input mapping is not contiguous, GPIOP3-0..4 is mapped
  205. * to bits 10..14, while GPIOP3-5 is mapped to bit 24.
  206. */
  207. return GPIO3_PIN_IN_SEL(state, pin);
  208. }
  209. static int __get_gpi_state_p3(struct lpc32xx_gpio_chip *group,
  210. unsigned pin)
  211. {
  212. return GPI3_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state), pin);
  213. }
  214. /*
  215. * GENERIC_GPIO primitives.
  216. */
  217. static int lpc32xx_gpio_dir_input_p012(struct gpio_chip *chip,
  218. unsigned pin)
  219. {
  220. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  221. __set_gpio_dir_p012(group, pin, 1);
  222. return 0;
  223. }
  224. static int lpc32xx_gpio_dir_input_p3(struct gpio_chip *chip,
  225. unsigned pin)
  226. {
  227. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  228. __set_gpio_dir_p3(group, pin, 1);
  229. return 0;
  230. }
  231. static int lpc32xx_gpio_dir_in_always(struct gpio_chip *chip,
  232. unsigned pin)
  233. {
  234. return 0;
  235. }
  236. static int lpc32xx_gpio_get_value_p012(struct gpio_chip *chip, unsigned pin)
  237. {
  238. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  239. return __get_gpio_state_p012(group, pin);
  240. }
  241. static int lpc32xx_gpio_get_value_p3(struct gpio_chip *chip, unsigned pin)
  242. {
  243. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  244. return __get_gpio_state_p3(group, pin);
  245. }
  246. static int lpc32xx_gpi_get_value(struct gpio_chip *chip, unsigned pin)
  247. {
  248. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  249. return __get_gpi_state_p3(group, pin);
  250. }
  251. static int lpc32xx_gpio_dir_output_p012(struct gpio_chip *chip, unsigned pin,
  252. int value)
  253. {
  254. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  255. __set_gpio_dir_p012(group, pin, 0);
  256. return 0;
  257. }
  258. static int lpc32xx_gpio_dir_output_p3(struct gpio_chip *chip, unsigned pin,
  259. int value)
  260. {
  261. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  262. __set_gpio_dir_p3(group, pin, 0);
  263. return 0;
  264. }
  265. static int lpc32xx_gpio_dir_out_always(struct gpio_chip *chip, unsigned pin,
  266. int value)
  267. {
  268. return 0;
  269. }
  270. static void lpc32xx_gpio_set_value_p012(struct gpio_chip *chip, unsigned pin,
  271. int value)
  272. {
  273. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  274. __set_gpio_level_p012(group, pin, value);
  275. }
  276. static void lpc32xx_gpio_set_value_p3(struct gpio_chip *chip, unsigned pin,
  277. int value)
  278. {
  279. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  280. __set_gpio_level_p3(group, pin, value);
  281. }
  282. static void lpc32xx_gpo_set_value(struct gpio_chip *chip, unsigned pin,
  283. int value)
  284. {
  285. struct lpc32xx_gpio_chip *group = to_lpc32xx_gpio(chip);
  286. __set_gpo_level_p3(group, pin, value);
  287. }
  288. static int lpc32xx_gpio_request(struct gpio_chip *chip, unsigned pin)
  289. {
  290. if (pin < chip->ngpio)
  291. return 0;
  292. return -EINVAL;
  293. }
  294. static struct lpc32xx_gpio_chip lpc32xx_gpiochip[] = {
  295. {
  296. .chip = {
  297. .label = "gpio_p0",
  298. .direction_input = lpc32xx_gpio_dir_input_p012,
  299. .get = lpc32xx_gpio_get_value_p012,
  300. .direction_output = lpc32xx_gpio_dir_output_p012,
  301. .set = lpc32xx_gpio_set_value_p012,
  302. .request = lpc32xx_gpio_request,
  303. .base = LPC32XX_GPIO_P0_GRP,
  304. .ngpio = LPC32XX_GPIO_P0_MAX,
  305. .names = gpio_p0_names,
  306. .can_sleep = 0,
  307. },
  308. .gpio_grp = &gpio_grp_regs_p0,
  309. },
  310. {
  311. .chip = {
  312. .label = "gpio_p1",
  313. .direction_input = lpc32xx_gpio_dir_input_p012,
  314. .get = lpc32xx_gpio_get_value_p012,
  315. .direction_output = lpc32xx_gpio_dir_output_p012,
  316. .set = lpc32xx_gpio_set_value_p012,
  317. .request = lpc32xx_gpio_request,
  318. .base = LPC32XX_GPIO_P1_GRP,
  319. .ngpio = LPC32XX_GPIO_P1_MAX,
  320. .names = gpio_p1_names,
  321. .can_sleep = 0,
  322. },
  323. .gpio_grp = &gpio_grp_regs_p1,
  324. },
  325. {
  326. .chip = {
  327. .label = "gpio_p2",
  328. .direction_input = lpc32xx_gpio_dir_input_p012,
  329. .get = lpc32xx_gpio_get_value_p012,
  330. .direction_output = lpc32xx_gpio_dir_output_p012,
  331. .set = lpc32xx_gpio_set_value_p012,
  332. .request = lpc32xx_gpio_request,
  333. .base = LPC32XX_GPIO_P2_GRP,
  334. .ngpio = LPC32XX_GPIO_P2_MAX,
  335. .names = gpio_p2_names,
  336. .can_sleep = 0,
  337. },
  338. .gpio_grp = &gpio_grp_regs_p2,
  339. },
  340. {
  341. .chip = {
  342. .label = "gpio_p3",
  343. .direction_input = lpc32xx_gpio_dir_input_p3,
  344. .get = lpc32xx_gpio_get_value_p3,
  345. .direction_output = lpc32xx_gpio_dir_output_p3,
  346. .set = lpc32xx_gpio_set_value_p3,
  347. .request = lpc32xx_gpio_request,
  348. .base = LPC32XX_GPIO_P3_GRP,
  349. .ngpio = LPC32XX_GPIO_P3_MAX,
  350. .names = gpio_p3_names,
  351. .can_sleep = 0,
  352. },
  353. .gpio_grp = &gpio_grp_regs_p3,
  354. },
  355. {
  356. .chip = {
  357. .label = "gpi_p3",
  358. .direction_input = lpc32xx_gpio_dir_in_always,
  359. .get = lpc32xx_gpi_get_value,
  360. .request = lpc32xx_gpio_request,
  361. .base = LPC32XX_GPI_P3_GRP,
  362. .ngpio = LPC32XX_GPI_P3_MAX,
  363. .names = gpi_p3_names,
  364. .can_sleep = 0,
  365. },
  366. .gpio_grp = &gpio_grp_regs_p3,
  367. },
  368. {
  369. .chip = {
  370. .label = "gpo_p3",
  371. .direction_output = lpc32xx_gpio_dir_out_always,
  372. .set = lpc32xx_gpo_set_value,
  373. .request = lpc32xx_gpio_request,
  374. .base = LPC32XX_GPO_P3_GRP,
  375. .ngpio = LPC32XX_GPO_P3_MAX,
  376. .names = gpo_p3_names,
  377. .can_sleep = 0,
  378. },
  379. .gpio_grp = &gpio_grp_regs_p3,
  380. },
  381. };
  382. void __init lpc32xx_gpio_init(void)
  383. {
  384. int i;
  385. for (i = 0; i < ARRAY_SIZE(lpc32xx_gpiochip); i++)
  386. gpiochip_add(&lpc32xx_gpiochip[i].chip);
  387. }