mpc52xx_gpio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <asm/gpio.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 = container_of(mm_gc,
  65. struct mpc52xx_gpiochip, mmchip);
  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 = container_of(mm_gc,
  86. struct mpc52xx_gpiochip, mmchip);
  87. struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
  88. unsigned long flags;
  89. spin_lock_irqsave(&gpio_lock, flags);
  90. /* set the direction */
  91. chip->shadow_ddr &= ~(1 << (7 - gpio));
  92. out_8(&regs->wkup_ddr, chip->shadow_ddr);
  93. /* and enable the pin */
  94. chip->shadow_gpioe |= 1 << (7 - gpio);
  95. out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
  96. spin_unlock_irqrestore(&gpio_lock, flags);
  97. return 0;
  98. }
  99. static int
  100. mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  101. {
  102. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  103. struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
  104. struct mpc52xx_gpiochip *chip = container_of(mm_gc,
  105. struct mpc52xx_gpiochip, mmchip);
  106. unsigned long flags;
  107. spin_lock_irqsave(&gpio_lock, flags);
  108. __mpc52xx_wkup_gpio_set(gc, gpio, val);
  109. /* Then set direction */
  110. chip->shadow_ddr |= 1 << (7 - gpio);
  111. out_8(&regs->wkup_ddr, chip->shadow_ddr);
  112. /* Finally enable the pin */
  113. chip->shadow_gpioe |= 1 << (7 - gpio);
  114. out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
  115. spin_unlock_irqrestore(&gpio_lock, flags);
  116. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  117. return 0;
  118. }
  119. static int __devinit mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
  120. {
  121. struct mpc52xx_gpiochip *chip;
  122. struct mpc52xx_gpio_wkup __iomem *regs;
  123. struct gpio_chip *gc;
  124. int ret;
  125. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  126. if (!chip)
  127. return -ENOMEM;
  128. gc = &chip->mmchip.gc;
  129. gc->ngpio = 8;
  130. gc->direction_input = mpc52xx_wkup_gpio_dir_in;
  131. gc->direction_output = mpc52xx_wkup_gpio_dir_out;
  132. gc->get = mpc52xx_wkup_gpio_get;
  133. gc->set = mpc52xx_wkup_gpio_set;
  134. ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
  135. if (ret)
  136. return ret;
  137. regs = chip->mmchip.regs;
  138. chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
  139. chip->shadow_ddr = in_8(&regs->wkup_ddr);
  140. chip->shadow_dvo = in_8(&regs->wkup_dvo);
  141. return 0;
  142. }
  143. static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
  144. {
  145. return -EBUSY;
  146. }
  147. static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
  148. {
  149. .compatible = "fsl,mpc5200-gpio-wkup",
  150. },
  151. {}
  152. };
  153. static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
  154. .driver = {
  155. .name = "gpio_wkup",
  156. .owner = THIS_MODULE,
  157. .of_match_table = mpc52xx_wkup_gpiochip_match,
  158. },
  159. .probe = mpc52xx_wkup_gpiochip_probe,
  160. .remove = mpc52xx_gpiochip_remove,
  161. };
  162. /*
  163. * GPIO LIB API implementation for simple GPIOs
  164. *
  165. * There's a maximum of 32 simple GPIOs. Which of these are available
  166. * for use depends on your board setup.
  167. * The numbering reflects the bit numbering in the port registers:
  168. *
  169. * 0..1 > reserved
  170. * 2..3 > IRDA
  171. * 4..7 > ETHR
  172. * 8..11 > reserved
  173. * 12..15 > USB
  174. * 16..17 > reserved
  175. * 18..23 > PSC3
  176. * 24..27 > PSC2
  177. * 28..31 > PSC1
  178. */
  179. static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  180. {
  181. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  182. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  183. unsigned int ret;
  184. ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
  185. return ret;
  186. }
  187. static inline void
  188. __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  189. {
  190. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  191. struct mpc52xx_gpiochip *chip = container_of(mm_gc,
  192. struct mpc52xx_gpiochip, mmchip);
  193. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  194. if (val)
  195. chip->shadow_dvo |= 1 << (31 - gpio);
  196. else
  197. chip->shadow_dvo &= ~(1 << (31 - gpio));
  198. out_be32(&regs->simple_dvo, chip->shadow_dvo);
  199. }
  200. static void
  201. mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  202. {
  203. unsigned long flags;
  204. spin_lock_irqsave(&gpio_lock, flags);
  205. __mpc52xx_simple_gpio_set(gc, gpio, val);
  206. spin_unlock_irqrestore(&gpio_lock, flags);
  207. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  208. }
  209. static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  210. {
  211. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  212. struct mpc52xx_gpiochip *chip = container_of(mm_gc,
  213. struct mpc52xx_gpiochip, mmchip);
  214. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  215. unsigned long flags;
  216. spin_lock_irqsave(&gpio_lock, flags);
  217. /* set the direction */
  218. chip->shadow_ddr &= ~(1 << (31 - gpio));
  219. out_be32(&regs->simple_ddr, chip->shadow_ddr);
  220. /* and enable the pin */
  221. chip->shadow_gpioe |= 1 << (31 - gpio);
  222. out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
  223. spin_unlock_irqrestore(&gpio_lock, flags);
  224. return 0;
  225. }
  226. static int
  227. mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  228. {
  229. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  230. struct mpc52xx_gpiochip *chip = container_of(mm_gc,
  231. struct mpc52xx_gpiochip, mmchip);
  232. struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
  233. unsigned long flags;
  234. spin_lock_irqsave(&gpio_lock, flags);
  235. /* First set initial value */
  236. __mpc52xx_simple_gpio_set(gc, gpio, val);
  237. /* Then set direction */
  238. chip->shadow_ddr |= 1 << (31 - gpio);
  239. out_be32(&regs->simple_ddr, chip->shadow_ddr);
  240. /* Finally enable the pin */
  241. chip->shadow_gpioe |= 1 << (31 - gpio);
  242. out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
  243. spin_unlock_irqrestore(&gpio_lock, flags);
  244. pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
  245. return 0;
  246. }
  247. static int __devinit mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
  248. {
  249. struct mpc52xx_gpiochip *chip;
  250. struct gpio_chip *gc;
  251. struct mpc52xx_gpio __iomem *regs;
  252. int ret;
  253. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  254. if (!chip)
  255. return -ENOMEM;
  256. gc = &chip->mmchip.gc;
  257. gc->ngpio = 32;
  258. gc->direction_input = mpc52xx_simple_gpio_dir_in;
  259. gc->direction_output = mpc52xx_simple_gpio_dir_out;
  260. gc->get = mpc52xx_simple_gpio_get;
  261. gc->set = mpc52xx_simple_gpio_set;
  262. ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
  263. if (ret)
  264. return ret;
  265. regs = chip->mmchip.regs;
  266. chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
  267. chip->shadow_ddr = in_be32(&regs->simple_ddr);
  268. chip->shadow_dvo = in_be32(&regs->simple_dvo);
  269. return 0;
  270. }
  271. static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
  272. {
  273. .compatible = "fsl,mpc5200-gpio",
  274. },
  275. {}
  276. };
  277. static struct platform_driver mpc52xx_simple_gpiochip_driver = {
  278. .driver = {
  279. .name = "gpio",
  280. .owner = THIS_MODULE,
  281. .of_match_table = mpc52xx_simple_gpiochip_match,
  282. },
  283. .probe = mpc52xx_simple_gpiochip_probe,
  284. .remove = mpc52xx_gpiochip_remove,
  285. };
  286. static int __init mpc52xx_gpio_init(void)
  287. {
  288. if (platform_driver_register(&mpc52xx_wkup_gpiochip_driver))
  289. printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
  290. if (platform_driver_register(&mpc52xx_simple_gpiochip_driver))
  291. printk(KERN_ERR "Unable to register simple GPIO driver\n");
  292. return 0;
  293. }
  294. /* Make sure we get initialised before anyone else tries to use us */
  295. subsys_initcall(mpc52xx_gpio_init);
  296. /* No exit call at the moment as we cannot unregister of gpio chips */
  297. MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
  298. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
  299. MODULE_LICENSE("GPL v2");