mdio-gpio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. dev_set_drvdata(dev, new_bus);
  104. ret = mdiobus_register(new_bus);
  105. if (ret)
  106. goto out_free_all;
  107. return 0;
  108. out_free_all:
  109. dev_set_drvdata(dev, NULL);
  110. gpio_free(bitbang->mdio);
  111. out_free_mdc:
  112. gpio_free(bitbang->mdc);
  113. out_free_bus:
  114. free_mdio_bitbang(new_bus);
  115. out_free_bitbang:
  116. kfree(bitbang);
  117. out:
  118. return ret;
  119. }
  120. static void __devexit mdio_gpio_bus_destroy(struct device *dev)
  121. {
  122. struct mii_bus *bus = dev_get_drvdata(dev);
  123. struct mdio_gpio_info *bitbang = bus->priv;
  124. mdiobus_unregister(bus);
  125. free_mdio_bitbang(bus);
  126. dev_set_drvdata(dev, NULL);
  127. gpio_free(bitbang->mdc);
  128. gpio_free(bitbang->mdio);
  129. kfree(bitbang);
  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. if (!pdata)
  135. return -ENODEV;
  136. return mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
  137. }
  138. static int __devexit mdio_gpio_remove(struct platform_device *pdev)
  139. {
  140. mdio_gpio_bus_destroy(&pdev->dev);
  141. return 0;
  142. }
  143. #ifdef CONFIG_OF_GPIO
  144. static void __devinit add_phy(struct mdio_gpio_platform_data *pdata,
  145. struct device_node *np)
  146. {
  147. const u32 *data;
  148. int len, id, irq;
  149. data = of_get_property(np, "reg", &len);
  150. if (!data || len != 4)
  151. return;
  152. id = *data;
  153. pdata->phy_mask &= ~(1 << id);
  154. irq = of_irq_to_resource(np, 0, NULL);
  155. if (irq)
  156. pdata->irqs[id] = irq;
  157. }
  158. static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,
  159. const struct of_device_id *match)
  160. {
  161. struct device_node *np = NULL;
  162. struct mdio_gpio_platform_data *pdata;
  163. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  164. if (!pdata)
  165. return -ENOMEM;
  166. pdata->mdc = of_get_gpio(ofdev->node, 0);
  167. pdata->mdio = of_get_gpio(ofdev->node, 1);
  168. if (pdata->mdc < 0 || pdata->mdio < 0)
  169. goto out_free;
  170. while ((np = of_get_next_child(ofdev->node, np)))
  171. if (!strcmp(np->type, "ethernet-phy"))
  172. add_phy(pdata, np);
  173. return mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
  174. out_free:
  175. kfree(pdata);
  176. return -ENODEV;
  177. }
  178. static int __devexit mdio_ofgpio_remove(struct of_device *ofdev)
  179. {
  180. mdio_gpio_bus_destroy(&ofdev->dev);
  181. kfree(ofdev->dev.platform_data);
  182. return 0;
  183. }
  184. static struct of_device_id mdio_ofgpio_match[] = {
  185. {
  186. .compatible = "virtual,mdio-gpio",
  187. },
  188. {},
  189. };
  190. static struct of_platform_driver mdio_ofgpio_driver = {
  191. .name = "mdio-gpio",
  192. .match_table = mdio_ofgpio_match,
  193. .probe = mdio_ofgpio_probe,
  194. .remove = __devexit_p(mdio_ofgpio_remove),
  195. };
  196. static inline int __init mdio_ofgpio_init(void)
  197. {
  198. return of_register_platform_driver(&mdio_ofgpio_driver);
  199. }
  200. static inline void __exit mdio_ofgpio_exit(void)
  201. {
  202. of_unregister_platform_driver(&mdio_ofgpio_driver);
  203. }
  204. #else
  205. static inline int __init mdio_ofgpio_init(void) { return 0; }
  206. static inline void __exit mdio_ofgpio_exit(void) { }
  207. #endif /* CONFIG_OF_GPIO */
  208. static struct platform_driver mdio_gpio_driver = {
  209. .probe = mdio_gpio_probe,
  210. .remove = __devexit_p(mdio_gpio_remove),
  211. .driver = {
  212. .name = "mdio-gpio",
  213. .owner = THIS_MODULE,
  214. },
  215. };
  216. static int __init mdio_gpio_init(void)
  217. {
  218. int ret;
  219. ret = mdio_ofgpio_init();
  220. if (ret)
  221. return ret;
  222. ret = platform_driver_register(&mdio_gpio_driver);
  223. if (ret)
  224. mdio_ofgpio_exit();
  225. return ret;
  226. }
  227. module_init(mdio_gpio_init);
  228. static void __exit mdio_gpio_exit(void)
  229. {
  230. platform_driver_unregister(&mdio_gpio_driver);
  231. mdio_ofgpio_exit();
  232. }
  233. module_exit(mdio_gpio_exit);
  234. MODULE_ALIAS("platform:mdio-gpio");
  235. MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
  236. MODULE_LICENSE("GPL");
  237. MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");