mdio-gpio.c 6.4 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_platform.h>
  32. #endif
  33. struct mdio_gpio_info {
  34. struct mdiobb_ctrl ctrl;
  35. int mdc, mdio;
  36. };
  37. static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
  38. {
  39. struct mdio_gpio_info *bitbang =
  40. container_of(ctrl, struct mdio_gpio_info, ctrl);
  41. if (dir)
  42. gpio_direction_output(bitbang->mdio, 1);
  43. else
  44. gpio_direction_input(bitbang->mdio);
  45. }
  46. static int mdio_get(struct mdiobb_ctrl *ctrl)
  47. {
  48. struct mdio_gpio_info *bitbang =
  49. container_of(ctrl, struct mdio_gpio_info, ctrl);
  50. return gpio_get_value(bitbang->mdio);
  51. }
  52. static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
  53. {
  54. struct mdio_gpio_info *bitbang =
  55. container_of(ctrl, struct mdio_gpio_info, ctrl);
  56. gpio_set_value(bitbang->mdio, what);
  57. }
  58. static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
  59. {
  60. struct mdio_gpio_info *bitbang =
  61. container_of(ctrl, struct mdio_gpio_info, ctrl);
  62. gpio_set_value(bitbang->mdc, what);
  63. }
  64. static struct mdiobb_ops mdio_gpio_ops = {
  65. .owner = THIS_MODULE,
  66. .set_mdc = mdc_set,
  67. .set_mdio_dir = mdio_dir,
  68. .set_mdio_data = mdio_set,
  69. .get_mdio_data = mdio_get,
  70. };
  71. static int __devinit mdio_gpio_bus_init(struct device *dev,
  72. struct mdio_gpio_platform_data *pdata,
  73. int bus_id)
  74. {
  75. struct mii_bus *new_bus;
  76. struct mdio_gpio_info *bitbang;
  77. int ret = -ENOMEM;
  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->mdc = pdata->mdc;
  84. bitbang->mdio = pdata->mdio;
  85. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  86. if (!new_bus)
  87. goto out_free_bitbang;
  88. new_bus->name = "GPIO Bitbanged MDIO",
  89. ret = -ENODEV;
  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, "%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. ret = mdiobus_register(new_bus);
  106. if (ret)
  107. goto out_free_all;
  108. return 0;
  109. out_free_all:
  110. dev_set_drvdata(dev, NULL);
  111. gpio_free(bitbang->mdio);
  112. out_free_mdc:
  113. gpio_free(bitbang->mdc);
  114. out_free_bus:
  115. free_mdio_bitbang(new_bus);
  116. out_free_bitbang:
  117. kfree(bitbang);
  118. out:
  119. return ret;
  120. }
  121. static void __devexit mdio_gpio_bus_destroy(struct device *dev)
  122. {
  123. struct mii_bus *bus = dev_get_drvdata(dev);
  124. struct mdio_gpio_info *bitbang = bus->priv;
  125. mdiobus_unregister(bus);
  126. free_mdio_bitbang(bus);
  127. dev_set_drvdata(dev, NULL);
  128. gpio_free(bitbang->mdc);
  129. gpio_free(bitbang->mdio);
  130. kfree(bitbang);
  131. }
  132. static int __devinit mdio_gpio_probe(struct platform_device *pdev)
  133. {
  134. struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
  135. if (!pdata)
  136. return -ENODEV;
  137. return mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
  138. }
  139. static int __devexit mdio_gpio_remove(struct platform_device *pdev)
  140. {
  141. mdio_gpio_bus_destroy(&pdev->dev);
  142. return 0;
  143. }
  144. #ifdef CONFIG_OF_GPIO
  145. static void __devinit add_phy(struct mdio_gpio_platform_data *pdata,
  146. struct device_node *np)
  147. {
  148. const u32 *data;
  149. int len, id, irq;
  150. data = of_get_property(np, "reg", &len);
  151. if (!data || len != 4)
  152. return;
  153. id = *data;
  154. pdata->phy_mask &= ~(1 << id);
  155. irq = of_irq_to_resource(np, 0, NULL);
  156. if (irq)
  157. pdata->irqs[id] = irq;
  158. }
  159. static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,
  160. const struct of_device_id *match)
  161. {
  162. struct device_node *np = NULL;
  163. struct mdio_gpio_platform_data *pdata;
  164. int ret;
  165. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  166. if (!pdata)
  167. return -ENOMEM;
  168. ret = of_get_gpio(ofdev->node, 0);
  169. if (ret < 0)
  170. goto out_free;
  171. pdata->mdc = ret;
  172. ret = of_get_gpio(ofdev->node, 1);
  173. if (ret < 0)
  174. goto out_free;
  175. pdata->mdio = ret;
  176. while ((np = of_get_next_child(ofdev->node, np)))
  177. if (!strcmp(np->type, "ethernet-phy"))
  178. add_phy(pdata, np);
  179. return mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
  180. out_free:
  181. kfree(pdata);
  182. return -ENODEV;
  183. }
  184. static int __devexit mdio_ofgpio_remove(struct of_device *ofdev)
  185. {
  186. mdio_gpio_bus_destroy(&ofdev->dev);
  187. kfree(ofdev->dev.platform_data);
  188. return 0;
  189. }
  190. static struct of_device_id mdio_ofgpio_match[] = {
  191. {
  192. .compatible = "virtual,mdio-gpio",
  193. },
  194. {},
  195. };
  196. static struct of_platform_driver mdio_ofgpio_driver = {
  197. .name = "mdio-gpio",
  198. .match_table = mdio_ofgpio_match,
  199. .probe = mdio_ofgpio_probe,
  200. .remove = __devexit_p(mdio_ofgpio_remove),
  201. };
  202. static inline int __init mdio_ofgpio_init(void)
  203. {
  204. return of_register_platform_driver(&mdio_ofgpio_driver);
  205. }
  206. static inline void __exit mdio_ofgpio_exit(void)
  207. {
  208. of_unregister_platform_driver(&mdio_ofgpio_driver);
  209. }
  210. #else
  211. static inline int __init mdio_ofgpio_init(void) { return 0; }
  212. static inline void __exit mdio_ofgpio_exit(void) { }
  213. #endif /* CONFIG_OF_GPIO */
  214. static struct platform_driver mdio_gpio_driver = {
  215. .probe = mdio_gpio_probe,
  216. .remove = __devexit_p(mdio_gpio_remove),
  217. .driver = {
  218. .name = "mdio-gpio",
  219. .owner = THIS_MODULE,
  220. },
  221. };
  222. static int __init mdio_gpio_init(void)
  223. {
  224. int ret;
  225. ret = mdio_ofgpio_init();
  226. if (ret)
  227. return ret;
  228. ret = platform_driver_register(&mdio_gpio_driver);
  229. if (ret)
  230. mdio_ofgpio_exit();
  231. return ret;
  232. }
  233. module_init(mdio_gpio_init);
  234. static void __exit mdio_gpio_exit(void)
  235. {
  236. platform_driver_unregister(&mdio_gpio_driver);
  237. mdio_ofgpio_exit();
  238. }
  239. module_exit(mdio_gpio_exit);
  240. MODULE_ALIAS("platform:mdio-gpio");
  241. MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
  242. MODULE_LICENSE("GPL");
  243. MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");