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