gpio-lpc32xx.c 13 KB

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