mdio-gpio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/gpio.h>
  27. #include <linux/platform_data/mdio-gpio.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/of_mdio.h>
  30. struct mdio_gpio_info {
  31. struct mdiobb_ctrl ctrl;
  32. int mdc, mdio, mdo;
  33. int mdc_active_low, mdio_active_low, mdo_active_low;
  34. };
  35. static void *mdio_gpio_of_get_data(struct platform_device *pdev)
  36. {
  37. struct device_node *np = pdev->dev.of_node;
  38. struct mdio_gpio_platform_data *pdata;
  39. enum of_gpio_flags flags;
  40. int ret;
  41. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  42. if (!pdata)
  43. return NULL;
  44. ret = of_get_gpio_flags(np, 0, &flags);
  45. if (ret < 0)
  46. return NULL;
  47. pdata->mdc = ret;
  48. pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW;
  49. ret = of_get_gpio_flags(np, 1, &flags);
  50. if (ret < 0)
  51. return NULL;
  52. pdata->mdio = ret;
  53. pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW;
  54. ret = of_get_gpio_flags(np, 2, &flags);
  55. if (ret > 0) {
  56. pdata->mdo = ret;
  57. pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW;
  58. }
  59. return pdata;
  60. }
  61. static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
  62. {
  63. struct mdio_gpio_info *bitbang =
  64. container_of(ctrl, struct mdio_gpio_info, ctrl);
  65. if (bitbang->mdo) {
  66. /* Separate output pin. Always set its value to high
  67. * when changing direction. If direction is input,
  68. * assume the pin serves as pull-up. If direction is
  69. * output, the default value is high.
  70. */
  71. gpio_set_value_cansleep(bitbang->mdo,
  72. 1 ^ bitbang->mdo_active_low);
  73. return;
  74. }
  75. if (dir)
  76. gpio_direction_output(bitbang->mdio,
  77. 1 ^ bitbang->mdio_active_low);
  78. else
  79. gpio_direction_input(bitbang->mdio);
  80. }
  81. static int mdio_get(struct mdiobb_ctrl *ctrl)
  82. {
  83. struct mdio_gpio_info *bitbang =
  84. container_of(ctrl, struct mdio_gpio_info, ctrl);
  85. return gpio_get_value_cansleep(bitbang->mdio) ^
  86. bitbang->mdio_active_low;
  87. }
  88. static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
  89. {
  90. struct mdio_gpio_info *bitbang =
  91. container_of(ctrl, struct mdio_gpio_info, ctrl);
  92. if (bitbang->mdo)
  93. gpio_set_value_cansleep(bitbang->mdo,
  94. what ^ bitbang->mdo_active_low);
  95. else
  96. gpio_set_value_cansleep(bitbang->mdio,
  97. what ^ bitbang->mdio_active_low);
  98. }
  99. static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
  100. {
  101. struct mdio_gpio_info *bitbang =
  102. container_of(ctrl, struct mdio_gpio_info, ctrl);
  103. gpio_set_value_cansleep(bitbang->mdc, what ^ bitbang->mdc_active_low);
  104. }
  105. static struct mdiobb_ops mdio_gpio_ops = {
  106. .owner = THIS_MODULE,
  107. .set_mdc = mdc_set,
  108. .set_mdio_dir = mdio_dir,
  109. .set_mdio_data = mdio_set,
  110. .get_mdio_data = mdio_get,
  111. };
  112. static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
  113. struct mdio_gpio_platform_data *pdata,
  114. int bus_id)
  115. {
  116. struct mii_bus *new_bus;
  117. struct mdio_gpio_info *bitbang;
  118. int i;
  119. bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
  120. if (!bitbang)
  121. goto out;
  122. bitbang->ctrl.ops = &mdio_gpio_ops;
  123. bitbang->ctrl.reset = pdata->reset;
  124. bitbang->mdc = pdata->mdc;
  125. bitbang->mdc_active_low = pdata->mdc_active_low;
  126. bitbang->mdio = pdata->mdio;
  127. bitbang->mdio_active_low = pdata->mdio_active_low;
  128. bitbang->mdo = pdata->mdo;
  129. bitbang->mdo_active_low = pdata->mdo_active_low;
  130. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  131. if (!new_bus)
  132. goto out;
  133. new_bus->name = "GPIO Bitbanged MDIO",
  134. new_bus->phy_mask = pdata->phy_mask;
  135. new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
  136. new_bus->irq = pdata->irqs;
  137. new_bus->parent = dev;
  138. if (new_bus->phy_mask == ~0)
  139. goto out_free_bus;
  140. for (i = 0; i < PHY_MAX_ADDR; i++)
  141. if (!new_bus->irq[i])
  142. new_bus->irq[i] = PHY_POLL;
  143. if (bus_id != -1)
  144. snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
  145. else
  146. strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
  147. if (devm_gpio_request(dev, bitbang->mdc, "mdc"))
  148. goto out_free_bus;
  149. if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
  150. goto out_free_bus;
  151. if (bitbang->mdo) {
  152. if (devm_gpio_request(dev, bitbang->mdo, "mdo"))
  153. goto out_free_bus;
  154. gpio_direction_output(bitbang->mdo, 1);
  155. gpio_direction_input(bitbang->mdio);
  156. }
  157. gpio_direction_output(bitbang->mdc, 0);
  158. dev_set_drvdata(dev, new_bus);
  159. return new_bus;
  160. out_free_bus:
  161. free_mdio_bitbang(new_bus);
  162. out:
  163. return NULL;
  164. }
  165. static void mdio_gpio_bus_deinit(struct device *dev)
  166. {
  167. struct mii_bus *bus = dev_get_drvdata(dev);
  168. free_mdio_bitbang(bus);
  169. }
  170. static void mdio_gpio_bus_destroy(struct device *dev)
  171. {
  172. struct mii_bus *bus = dev_get_drvdata(dev);
  173. mdiobus_unregister(bus);
  174. mdio_gpio_bus_deinit(dev);
  175. }
  176. static int mdio_gpio_probe(struct platform_device *pdev)
  177. {
  178. struct mdio_gpio_platform_data *pdata;
  179. struct mii_bus *new_bus;
  180. int ret, bus_id;
  181. if (pdev->dev.of_node) {
  182. pdata = mdio_gpio_of_get_data(pdev);
  183. bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
  184. if (bus_id < 0) {
  185. dev_warn(&pdev->dev, "failed to get alias id\n");
  186. bus_id = 0;
  187. }
  188. } else {
  189. pdata = dev_get_platdata(&pdev->dev);
  190. bus_id = pdev->id;
  191. }
  192. if (!pdata)
  193. return -ENODEV;
  194. new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
  195. if (!new_bus)
  196. return -ENODEV;
  197. if (pdev->dev.of_node)
  198. ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
  199. else
  200. ret = mdiobus_register(new_bus);
  201. if (ret)
  202. mdio_gpio_bus_deinit(&pdev->dev);
  203. return ret;
  204. }
  205. static int mdio_gpio_remove(struct platform_device *pdev)
  206. {
  207. mdio_gpio_bus_destroy(&pdev->dev);
  208. return 0;
  209. }
  210. static const struct of_device_id mdio_gpio_of_match[] = {
  211. { .compatible = "virtual,mdio-gpio", },
  212. { /* sentinel */ }
  213. };
  214. MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
  215. static struct platform_driver mdio_gpio_driver = {
  216. .probe = mdio_gpio_probe,
  217. .remove = mdio_gpio_remove,
  218. .driver = {
  219. .name = "mdio-gpio",
  220. .of_match_table = mdio_gpio_of_match,
  221. },
  222. };
  223. module_platform_driver(mdio_gpio_driver);
  224. MODULE_ALIAS("platform:mdio-gpio");
  225. MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
  226. MODULE_LICENSE("GPL");
  227. MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");