gpio-mpc5200.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * MPC52xx gpio driver
  3. *
  4. * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/of.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/io.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/module.h>
  26. #include <asm/mpc52xx.h>
  27. #include <sysdev/fsl_soc.h>
  28. static DEFINE_SPINLOCK(gpio_lock);
  29. struct mpc52xx_gpiochip {
  30. struct of_mm_gpio_chip mmchip;
  31. unsigned int shadow_dvo;
  32. unsigned int shadow_gpioe;
  33. unsigned int shadow_ddr;
  34. };
  35. /*
  36. * GPIO LIB API implementation for wakeup GPIOs.
  37. *
  38. * There's a maximum of 8 wakeup GPIOs. Which of these are available
  39. * for use depends on your board setup.
  40. *
  41. * 0 -> GPIO_WKUP_7
  42. * 1 -> GPIO_WKUP_6
  43. * 2 -> PSC6_1
  44. * 3 -> PSC6_0
  45. * 4 -> ETH_17
  46. * 5 -> PSC3_9
  47. * 6 -> PSC2_4
  48. * 7 -> PSC1_4
  49. *
  50. */
  51. static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  52. {
  53. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  54. struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
  55. unsigned int ret;
  56. ret = (in_8(&regs->wkup_ival) >> (7 - gpio)) & 1;
  57. pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
  58. return ret;
  59. }
  60. static inline void
  61. __mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  62. {
  63. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  64. struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
  65. struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
  66. if (val)
  67. chip->shadow_dvo |= 1 << (7 - gpio);
  68. else
  69. chip->shadow_dvo &= ~(1 << (7 - gpio));
  70. out_8(&regs->wkup_dvo, chip->shadow_dvo);
  71. }
  72. static void
  73. mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  74. {
  75. unsigned long flags;
  76. spin_lock_irqsave(&gpio_lock, flags);
  77. __mpc52xx_wkup_gpio_set(gc, gpio, val);
  78. spin_unlock_irqrestore(&gpio_lock, flags);
  79. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  80. }
  81. static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  82. {
  83. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  84. struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
  85. struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
  86. unsigned long flags;
  87. spin_lock_irqsave(&gpio_lock, flags);
  88. /* set the direction */
  89. chip->shadow_ddr &= ~(1 << (7 - gpio));
  90. out_8(&regs->wkup_ddr, chip->shadow_ddr);
  91. /* and enable the pin */
  92. chip->shadow_gpioe |= 1 << (7 - gpio);
  93. out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
  94. spin_unlock_irqrestore(&gpio_lock, flags);
  95. return 0;
  96. }
  97. static int
  98. mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  99. {
  100. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  101. struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
  102. struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
  103. unsigned long flags;
  104. spin_lock_irqsave(&gpio_lock, flags);
  105. __mpc52xx_wkup_gpio_set(gc, gpio, val);
  106. /* Then set direction */
  107. chip->shadow_ddr |= 1 << (7 - gpio);
  108. out_8(&regs->wkup_ddr, chip->shadow_ddr);
  109. /* Finally enable the pin */
  110. chip->shadow_gpioe |= 1 << (7 - gpio);
  111. out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
  112. spin_unlock_irqrestore(&gpio_lock, flags);
  113. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  114. return 0;
  115. }
  116. static int mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
  117. {
  118. struct mpc52xx_gpiochip *chip;
  119. struct mpc52xx_gpio_wkup __iomem *regs;
  120. struct gpio_chip *gc;
  121. int ret;
  122. chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
  123. if (!chip)
  124. return -ENOMEM;
  125. platform_set_drvdata(ofdev, chip);
  126. gc = &chip->mmchip.gc;
  127. gc->ngpio = 8;
  128. gc->direction_input = mpc52xx_wkup_gpio_dir_in;
  129. gc->direction_output = mpc52xx_wkup_gpio_dir_out;
  130. gc->get = mpc52xx_wkup_gpio_get;
  131. gc->set = mpc52xx_wkup_gpio_set;
  132. ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
  133. if (ret)
  134. return ret;
  135. regs = chip->mmchip.regs;
  136. chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
  137. chip->shadow_ddr = in_8(&regs->wkup_ddr);
  138. chip->shadow_dvo = in_8(&regs->wkup_dvo);
  139. return 0;
  140. }
  141. static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
  142. {
  143. struct mpc52xx_gpiochip *chip = platform_get_drvdata(ofdev);
  144. of_mm_gpiochip_remove(&chip->mmchip);
  145. return 0;
  146. }
  147. static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
  148. { .compatible = "fsl,mpc5200-gpio-wkup", },
  149. {}
  150. };
  151. static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
  152. .driver = {
  153. .name = "mpc5200-gpio-wkup",
  154. .of_match_table = mpc52xx_wkup_gpiochip_match,
  155. },
  156. .probe = mpc52xx_wkup_gpiochip_probe,
  157. .remove = mpc52xx_gpiochip_remove,
  158. };
  159. /*
  160. * GPIO LIB API implementation for simple GPIOs
  161. *
  162. * There's a maximum of 32 simple GPIOs. Which of these are available
  163. * for use depends on your board setup.
  164. * The numbering reflects the bit numbering in the port registers:
  165. *
  166. * 0..1 > reserved
  167. * 2..3 > IRDA
  168. * 4..7 > ETHR
  169. * 8..11 > reserved
  170. * 12..15 > USB
  171. * 16..17 > reserved
  172. * 18..23 > PSC3
  173. * 24..27 > PSC2
  174. * 28..31 > PSC1
  175. */
  176. static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  177. {
  178. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  179. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  180. unsigned int ret;
  181. ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
  182. return ret;
  183. }
  184. static inline void
  185. __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  186. {
  187. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  188. struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
  189. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  190. if (val)
  191. chip->shadow_dvo |= 1 << (31 - gpio);
  192. else
  193. chip->shadow_dvo &= ~(1 << (31 - gpio));
  194. out_be32(&regs->simple_dvo, chip->shadow_dvo);
  195. }
  196. static void
  197. mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  198. {
  199. unsigned long flags;
  200. spin_lock_irqsave(&gpio_lock, flags);
  201. __mpc52xx_simple_gpio_set(gc, gpio, val);
  202. spin_unlock_irqrestore(&gpio_lock, flags);
  203. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  204. }
  205. static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  206. {
  207. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  208. struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
  209. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  210. unsigned long flags;
  211. spin_lock_irqsave(&gpio_lock, flags);
  212. /* set the direction */
  213. chip->shadow_ddr &= ~(1 << (31 - gpio));
  214. out_be32(&regs->simple_ddr, chip->shadow_ddr);
  215. /* and enable the pin */
  216. chip->shadow_gpioe |= 1 << (31 - gpio);
  217. out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
  218. spin_unlock_irqrestore(&gpio_lock, flags);
  219. return 0;
  220. }
  221. static int
  222. mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  223. {
  224. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  225. struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
  226. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  227. unsigned long flags;
  228. spin_lock_irqsave(&gpio_lock, flags);
  229. /* First set initial value */
  230. __mpc52xx_simple_gpio_set(gc, gpio, val);
  231. /* Then set direction */
  232. chip->shadow_ddr |= 1 << (31 - gpio);
  233. out_be32(&regs->simple_ddr, chip->shadow_ddr);
  234. /* Finally enable the pin */
  235. chip->shadow_gpioe |= 1 << (31 - gpio);
  236. out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
  237. spin_unlock_irqrestore(&gpio_lock, flags);
  238. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  239. return 0;
  240. }
  241. static int mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
  242. {
  243. struct mpc52xx_gpiochip *chip;
  244. struct gpio_chip *gc;
  245. struct mpc52xx_gpio __iomem *regs;
  246. int ret;
  247. chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
  248. if (!chip)
  249. return -ENOMEM;
  250. platform_set_drvdata(ofdev, chip);
  251. gc = &chip->mmchip.gc;
  252. gc->ngpio = 32;
  253. gc->direction_input = mpc52xx_simple_gpio_dir_in;
  254. gc->direction_output = mpc52xx_simple_gpio_dir_out;
  255. gc->get = mpc52xx_simple_gpio_get;
  256. gc->set = mpc52xx_simple_gpio_set;
  257. ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
  258. if (ret)
  259. return ret;
  260. regs = chip->mmchip.regs;
  261. chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
  262. chip->shadow_ddr = in_be32(&regs->simple_ddr);
  263. chip->shadow_dvo = in_be32(&regs->simple_dvo);
  264. return 0;
  265. }
  266. static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
  267. { .compatible = "fsl,mpc5200-gpio", },
  268. {}
  269. };
  270. static struct platform_driver mpc52xx_simple_gpiochip_driver = {
  271. .driver = {
  272. .name = "mpc5200-gpio",
  273. .of_match_table = mpc52xx_simple_gpiochip_match,
  274. },
  275. .probe = mpc52xx_simple_gpiochip_probe,
  276. .remove = mpc52xx_gpiochip_remove,
  277. };
  278. static struct platform_driver * const drivers[] = {
  279. &mpc52xx_wkup_gpiochip_driver,
  280. &mpc52xx_simple_gpiochip_driver,
  281. };
  282. static int __init mpc52xx_gpio_init(void)
  283. {
  284. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  285. }
  286. /* Make sure we get initialised before anyone else tries to use us */
  287. subsys_initcall(mpc52xx_gpio_init);
  288. static void __exit mpc52xx_gpio_exit(void)
  289. {
  290. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  291. }
  292. module_exit(mpc52xx_gpio_exit);
  293. MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
  294. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
  295. MODULE_LICENSE("GPL v2");