mdio-gpio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * GPIO based MDIO bitbang driver.
  3. * Supports OpenFirmware.
  4. *
  5. * Copyright (c) 2008 CSE Semaphore Belgium.
  6. * by Laurent Pinchart <laurentp@cse-semaphore.com>
  7. *
  8. * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  9. *
  10. * Based on earlier work by
  11. *
  12. * Copyright (c) 2003 Intracom S.A.
  13. * by Pantelis Antoniou <panto@intracom.gr>
  14. *
  15. * 2005 (c) MontaVista Software, Inc.
  16. * Vitaly Bordug <vbordug@ru.mvista.com>
  17. *
  18. * This file is licensed under the terms of the GNU General Public License
  19. * version 2. This program is licensed "as is" without any warranty of any
  20. * kind, whether express or implied.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/gpio.h>
  28. #include <linux/mdio-gpio.h>
  29. #ifdef CONFIG_OF_GPIO
  30. #include <linux/of_gpio.h>
  31. #include <linux/of_mdio.h>
  32. #include <linux/of_platform.h>
  33. #endif
  34. struct mdio_gpio_info {
  35. struct mdiobb_ctrl ctrl;
  36. int mdc, mdio;
  37. };
  38. static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
  39. {
  40. struct mdio_gpio_info *bitbang =
  41. container_of(ctrl, struct mdio_gpio_info, ctrl);
  42. if (dir)
  43. gpio_direction_output(bitbang->mdio, 1);
  44. else
  45. gpio_direction_input(bitbang->mdio);
  46. }
  47. static int mdio_get(struct mdiobb_ctrl *ctrl)
  48. {
  49. struct mdio_gpio_info *bitbang =
  50. container_of(ctrl, struct mdio_gpio_info, ctrl);
  51. return gpio_get_value(bitbang->mdio);
  52. }
  53. static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
  54. {
  55. struct mdio_gpio_info *bitbang =
  56. container_of(ctrl, struct mdio_gpio_info, ctrl);
  57. gpio_set_value(bitbang->mdio, what);
  58. }
  59. static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
  60. {
  61. struct mdio_gpio_info *bitbang =
  62. container_of(ctrl, struct mdio_gpio_info, ctrl);
  63. gpio_set_value(bitbang->mdc, what);
  64. }
  65. static struct mdiobb_ops mdio_gpio_ops = {
  66. .owner = THIS_MODULE,
  67. .set_mdc = mdc_set,
  68. .set_mdio_dir = mdio_dir,
  69. .set_mdio_data = mdio_set,
  70. .get_mdio_data = mdio_get,
  71. };
  72. static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev,
  73. struct mdio_gpio_platform_data *pdata,
  74. int bus_id)
  75. {
  76. struct mii_bus *new_bus;
  77. struct mdio_gpio_info *bitbang;
  78. int i;
  79. bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
  80. if (!bitbang)
  81. goto out;
  82. bitbang->ctrl.ops = &mdio_gpio_ops;
  83. bitbang->ctrl.reset = pdata->reset;
  84. bitbang->mdc = pdata->mdc;
  85. bitbang->mdio = pdata->mdio;
  86. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  87. if (!new_bus)
  88. goto out_free_bitbang;
  89. new_bus->name = "GPIO Bitbanged MDIO",
  90. new_bus->phy_mask = pdata->phy_mask;
  91. new_bus->irq = pdata->irqs;
  92. new_bus->parent = dev;
  93. if (new_bus->phy_mask == ~0)
  94. goto out_free_bus;
  95. for (i = 0; i < PHY_MAX_ADDR; i++)
  96. if (!new_bus->irq[i])
  97. new_bus->irq[i] = PHY_POLL;
  98. snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
  99. if (gpio_request(bitbang->mdc, "mdc"))
  100. goto out_free_bus;
  101. if (gpio_request(bitbang->mdio, "mdio"))
  102. goto out_free_mdc;
  103. gpio_direction_output(bitbang->mdc, 0);
  104. dev_set_drvdata(dev, new_bus);
  105. return new_bus;
  106. out_free_mdc:
  107. gpio_free(bitbang->mdc);
  108. out_free_bus:
  109. free_mdio_bitbang(new_bus);
  110. out_free_bitbang:
  111. kfree(bitbang);
  112. out:
  113. return NULL;
  114. }
  115. static void mdio_gpio_bus_deinit(struct device *dev)
  116. {
  117. struct mii_bus *bus = dev_get_drvdata(dev);
  118. struct mdio_gpio_info *bitbang = bus->priv;
  119. dev_set_drvdata(dev, NULL);
  120. gpio_free(bitbang->mdio);
  121. gpio_free(bitbang->mdc);
  122. free_mdio_bitbang(bus);
  123. kfree(bitbang);
  124. }
  125. static void __devexit mdio_gpio_bus_destroy(struct device *dev)
  126. {
  127. struct mii_bus *bus = dev_get_drvdata(dev);
  128. mdiobus_unregister(bus);
  129. mdio_gpio_bus_deinit(dev);
  130. }
  131. static int __devinit mdio_gpio_probe(struct platform_device *pdev)
  132. {
  133. struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
  134. struct mii_bus *new_bus;
  135. int ret;
  136. if (!pdata)
  137. return -ENODEV;
  138. new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
  139. if (!new_bus)
  140. return -ENODEV;
  141. ret = mdiobus_register(new_bus);
  142. if (ret)
  143. mdio_gpio_bus_deinit(&pdev->dev);
  144. return ret;
  145. }
  146. static int __devexit mdio_gpio_remove(struct platform_device *pdev)
  147. {
  148. mdio_gpio_bus_destroy(&pdev->dev);
  149. return 0;
  150. }
  151. #ifdef CONFIG_OF_GPIO
  152. static int __devinit mdio_ofgpio_probe(struct platform_device *ofdev)
  153. {
  154. struct mdio_gpio_platform_data *pdata;
  155. struct mii_bus *new_bus;
  156. int ret;
  157. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  158. if (!pdata)
  159. return -ENOMEM;
  160. ret = of_get_gpio(ofdev->dev.of_node, 0);
  161. if (ret < 0)
  162. goto out_free;
  163. pdata->mdc = ret;
  164. ret = of_get_gpio(ofdev->dev.of_node, 1);
  165. if (ret < 0)
  166. goto out_free;
  167. pdata->mdio = ret;
  168. new_bus = mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
  169. if (!new_bus)
  170. goto out_free;
  171. ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
  172. if (ret)
  173. mdio_gpio_bus_deinit(&ofdev->dev);
  174. return ret;
  175. out_free:
  176. kfree(pdata);
  177. return -ENODEV;
  178. }
  179. static int __devexit mdio_ofgpio_remove(struct platform_device *ofdev)
  180. {
  181. mdio_gpio_bus_destroy(&ofdev->dev);
  182. kfree(ofdev->dev.platform_data);
  183. return 0;
  184. }
  185. static struct of_device_id mdio_ofgpio_match[] = {
  186. {
  187. .compatible = "virtual,mdio-gpio",
  188. },
  189. {},
  190. };
  191. MODULE_DEVICE_TABLE(of, mdio_ofgpio_match);
  192. static struct platform_driver mdio_ofgpio_driver = {
  193. .driver = {
  194. .name = "mdio-ofgpio",
  195. .owner = THIS_MODULE,
  196. .of_match_table = mdio_ofgpio_match,
  197. },
  198. .probe = mdio_ofgpio_probe,
  199. .remove = __devexit_p(mdio_ofgpio_remove),
  200. };
  201. static inline int __init mdio_ofgpio_init(void)
  202. {
  203. return platform_driver_register(&mdio_ofgpio_driver);
  204. }
  205. static inline void mdio_ofgpio_exit(void)
  206. {
  207. platform_driver_unregister(&mdio_ofgpio_driver);
  208. }
  209. #else
  210. static inline int __init mdio_ofgpio_init(void) { return 0; }
  211. static inline void mdio_ofgpio_exit(void) { }
  212. #endif /* CONFIG_OF_GPIO */
  213. static struct platform_driver mdio_gpio_driver = {
  214. .probe = mdio_gpio_probe,
  215. .remove = __devexit_p(mdio_gpio_remove),
  216. .driver = {
  217. .name = "mdio-gpio",
  218. .owner = THIS_MODULE,
  219. },
  220. };
  221. static int __init mdio_gpio_init(void)
  222. {
  223. int ret;
  224. ret = mdio_ofgpio_init();
  225. if (ret)
  226. return ret;
  227. ret = platform_driver_register(&mdio_gpio_driver);
  228. if (ret)
  229. mdio_ofgpio_exit();
  230. return ret;
  231. }
  232. module_init(mdio_gpio_init);
  233. static void __exit mdio_gpio_exit(void)
  234. {
  235. platform_driver_unregister(&mdio_gpio_driver);
  236. mdio_ofgpio_exit();
  237. }
  238. module_exit(mdio_gpio_exit);
  239. MODULE_ALIAS("platform:mdio-gpio");
  240. MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
  241. MODULE_LICENSE("GPL");
  242. MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");