/kern_oII/drivers/net/phy/mdio-gpio.c

http://omnia2droid.googlecode.com/ · C · 298 lines · 217 code · 60 blank · 21 comment · 19 complexity · e1e967abfc2d531d5b0fe502338ad6a1 MD5 · raw file

  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->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. new_bus->phy_mask = pdata->phy_mask;
  90. new_bus->irq = pdata->irqs;
  91. new_bus->parent = dev;
  92. if (new_bus->phy_mask == ~0)
  93. goto out_free_bus;
  94. for (i = 0; i < PHY_MAX_ADDR; i++)
  95. if (!new_bus->irq[i])
  96. new_bus->irq[i] = PHY_POLL;
  97. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);
  98. if (gpio_request(bitbang->mdc, "mdc"))
  99. goto out_free_bus;
  100. if (gpio_request(bitbang->mdio, "mdio"))
  101. goto out_free_mdc;
  102. gpio_direction_output(bitbang->mdc, 0);
  103. dev_set_drvdata(dev, new_bus);
  104. return new_bus;
  105. out_free_mdc:
  106. gpio_free(bitbang->mdc);
  107. out_free_bus:
  108. free_mdio_bitbang(new_bus);
  109. out_free_bitbang:
  110. kfree(bitbang);
  111. out:
  112. return NULL;
  113. }
  114. static void __devinit mdio_gpio_bus_deinit(struct device *dev)
  115. {
  116. struct mii_bus *bus = dev_get_drvdata(dev);
  117. struct mdio_gpio_info *bitbang = bus->priv;
  118. dev_set_drvdata(dev, NULL);
  119. gpio_free(bitbang->mdio);
  120. gpio_free(bitbang->mdc);
  121. free_mdio_bitbang(bus);
  122. kfree(bitbang);
  123. }
  124. static void __devexit mdio_gpio_bus_destroy(struct device *dev)
  125. {
  126. struct mii_bus *bus = dev_get_drvdata(dev);
  127. mdiobus_unregister(bus);
  128. mdio_gpio_bus_deinit(dev);
  129. }
  130. static int __devinit mdio_gpio_probe(struct platform_device *pdev)
  131. {
  132. struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
  133. struct mii_bus *new_bus;
  134. int ret;
  135. if (!pdata)
  136. return -ENODEV;
  137. new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
  138. if (!new_bus)
  139. return -ENODEV;
  140. ret = mdiobus_register(new_bus);
  141. if (ret)
  142. mdio_gpio_bus_deinit(&pdev->dev);
  143. return ret;
  144. }
  145. static int __devexit mdio_gpio_remove(struct platform_device *pdev)
  146. {
  147. mdio_gpio_bus_destroy(&pdev->dev);
  148. return 0;
  149. }
  150. #ifdef CONFIG_OF_GPIO
  151. static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,
  152. const struct of_device_id *match)
  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->node, 0);
  161. if (ret < 0)
  162. goto out_free;
  163. pdata->mdc = ret;
  164. ret = of_get_gpio(ofdev->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. return -ENODEV;
  171. ret = of_mdiobus_register(new_bus, ofdev->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 of_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. static struct of_platform_driver mdio_ofgpio_driver = {
  192. .name = "mdio-gpio",
  193. .match_table = mdio_ofgpio_match,
  194. .probe = mdio_ofgpio_probe,
  195. .remove = __devexit_p(mdio_ofgpio_remove),
  196. };
  197. static inline int __init mdio_ofgpio_init(void)
  198. {
  199. return of_register_platform_driver(&mdio_ofgpio_driver);
  200. }
  201. static inline void __exit mdio_ofgpio_exit(void)
  202. {
  203. of_unregister_platform_driver(&mdio_ofgpio_driver);
  204. }
  205. #else
  206. static inline int __init mdio_ofgpio_init(void) { return 0; }
  207. static inline void __exit mdio_ofgpio_exit(void) { }
  208. #endif /* CONFIG_OF_GPIO */
  209. static struct platform_driver mdio_gpio_driver = {
  210. .probe = mdio_gpio_probe,
  211. .remove = __devexit_p(mdio_gpio_remove),
  212. .driver = {
  213. .name = "mdio-gpio",
  214. .owner = THIS_MODULE,
  215. },
  216. };
  217. static int __init mdio_gpio_init(void)
  218. {
  219. int ret;
  220. ret = mdio_ofgpio_init();
  221. if (ret)
  222. return ret;
  223. ret = platform_driver_register(&mdio_gpio_driver);
  224. if (ret)
  225. mdio_ofgpio_exit();
  226. return ret;
  227. }
  228. module_init(mdio_gpio_init);
  229. static void __exit mdio_gpio_exit(void)
  230. {
  231. platform_driver_unregister(&mdio_gpio_driver);
  232. mdio_ofgpio_exit();
  233. }
  234. module_exit(mdio_gpio_exit);
  235. MODULE_ALIAS("platform:mdio-gpio");
  236. MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
  237. MODULE_LICENSE("GPL");
  238. MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");