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