mdio_device.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Framework for MDIO devices, other than PHYs.
  3. *
  4. * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/gpio.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mdio.h>
  15. #include <linux/mii.h>
  16. #include <linux/module.h>
  17. #include <linux/phy.h>
  18. #include <linux/reset.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <linux/unistd.h>
  22. #include <linux/property.h>
  23. void mdio_device_free(struct mdio_device *mdiodev)
  24. {
  25. put_device(&mdiodev->dev);
  26. }
  27. EXPORT_SYMBOL(mdio_device_free);
  28. static void mdio_device_release(struct device *dev)
  29. {
  30. fwnode_handle_put(dev->fwnode);
  31. kfree(to_mdio_device(dev));
  32. }
  33. int mdio_device_bus_match(struct device *dev, const struct device_driver *drv)
  34. {
  35. struct mdio_device *mdiodev = to_mdio_device(dev);
  36. const struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  37. if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
  38. return 0;
  39. return strcmp(mdiodev->modalias, drv->name) == 0;
  40. }
  41. struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
  42. {
  43. struct mdio_device *mdiodev;
  44. /* We allocate the device, and initialize the default values */
  45. mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
  46. if (!mdiodev)
  47. return ERR_PTR(-ENOMEM);
  48. mdiodev->dev.release = mdio_device_release;
  49. mdiodev->dev.parent = &bus->dev;
  50. mdiodev->dev.bus = &mdio_bus_type;
  51. mdiodev->device_free = mdio_device_free;
  52. mdiodev->device_remove = mdio_device_remove;
  53. mdiodev->bus = bus;
  54. mdiodev->addr = addr;
  55. mdiodev->reset_state = -1;
  56. dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
  57. device_initialize(&mdiodev->dev);
  58. return mdiodev;
  59. }
  60. EXPORT_SYMBOL(mdio_device_create);
  61. /**
  62. * mdio_device_register - Register the mdio device on the MDIO bus
  63. * @mdiodev: mdio_device structure to be added to the MDIO bus
  64. */
  65. int mdio_device_register(struct mdio_device *mdiodev)
  66. {
  67. int err;
  68. dev_dbg(&mdiodev->dev, "%s\n", __func__);
  69. err = mdiobus_register_device(mdiodev);
  70. if (err)
  71. return err;
  72. err = device_add(&mdiodev->dev);
  73. if (err) {
  74. pr_err("MDIO %d failed to add\n", mdiodev->addr);
  75. goto out;
  76. }
  77. return 0;
  78. out:
  79. mdiobus_unregister_device(mdiodev);
  80. return err;
  81. }
  82. EXPORT_SYMBOL(mdio_device_register);
  83. /**
  84. * mdio_device_remove - Remove a previously registered mdio device from the
  85. * MDIO bus
  86. * @mdiodev: mdio_device structure to remove
  87. *
  88. * This doesn't free the mdio_device itself, it merely reverses the effects
  89. * of mdio_device_register(). Use mdio_device_free() to free the device
  90. * after calling this function.
  91. */
  92. void mdio_device_remove(struct mdio_device *mdiodev)
  93. {
  94. device_del(&mdiodev->dev);
  95. mdiobus_unregister_device(mdiodev);
  96. }
  97. EXPORT_SYMBOL(mdio_device_remove);
  98. void mdio_device_reset(struct mdio_device *mdiodev, int value)
  99. {
  100. unsigned int d;
  101. if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
  102. return;
  103. if (mdiodev->reset_state == value)
  104. return;
  105. if (mdiodev->reset_gpio)
  106. gpiod_set_value_cansleep(mdiodev->reset_gpio, value);
  107. if (mdiodev->reset_ctrl) {
  108. if (value)
  109. reset_control_assert(mdiodev->reset_ctrl);
  110. else
  111. reset_control_deassert(mdiodev->reset_ctrl);
  112. }
  113. d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
  114. if (d)
  115. fsleep(d);
  116. mdiodev->reset_state = value;
  117. }
  118. EXPORT_SYMBOL(mdio_device_reset);
  119. /**
  120. * mdio_probe - probe an MDIO device
  121. * @dev: device to probe
  122. *
  123. * Description: Take care of setting up the mdio_device structure
  124. * and calling the driver to probe the device.
  125. */
  126. static int mdio_probe(struct device *dev)
  127. {
  128. struct mdio_device *mdiodev = to_mdio_device(dev);
  129. struct device_driver *drv = mdiodev->dev.driver;
  130. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  131. int err = 0;
  132. /* Deassert the reset signal */
  133. mdio_device_reset(mdiodev, 0);
  134. if (mdiodrv->probe) {
  135. err = mdiodrv->probe(mdiodev);
  136. if (err) {
  137. /* Assert the reset signal */
  138. mdio_device_reset(mdiodev, 1);
  139. }
  140. }
  141. return err;
  142. }
  143. static int mdio_remove(struct device *dev)
  144. {
  145. struct mdio_device *mdiodev = to_mdio_device(dev);
  146. struct device_driver *drv = mdiodev->dev.driver;
  147. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  148. if (mdiodrv->remove)
  149. mdiodrv->remove(mdiodev);
  150. /* Assert the reset signal */
  151. mdio_device_reset(mdiodev, 1);
  152. return 0;
  153. }
  154. static void mdio_shutdown(struct device *dev)
  155. {
  156. struct mdio_device *mdiodev = to_mdio_device(dev);
  157. struct device_driver *drv = mdiodev->dev.driver;
  158. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  159. if (mdiodrv->shutdown)
  160. mdiodrv->shutdown(mdiodev);
  161. }
  162. /**
  163. * mdio_driver_register - register an mdio_driver with the MDIO layer
  164. * @drv: new mdio_driver to register
  165. */
  166. int mdio_driver_register(struct mdio_driver *drv)
  167. {
  168. struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
  169. int retval;
  170. pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
  171. mdiodrv->driver.bus = &mdio_bus_type;
  172. mdiodrv->driver.probe = mdio_probe;
  173. mdiodrv->driver.remove = mdio_remove;
  174. mdiodrv->driver.shutdown = mdio_shutdown;
  175. retval = driver_register(&mdiodrv->driver);
  176. if (retval) {
  177. pr_err("%s: Error %d in registering driver\n",
  178. mdiodrv->driver.name, retval);
  179. return retval;
  180. }
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(mdio_driver_register);
  184. void mdio_driver_unregister(struct mdio_driver *drv)
  185. {
  186. struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
  187. driver_unregister(&mdiodrv->driver);
  188. }
  189. EXPORT_SYMBOL(mdio_driver_unregister);