mdio_device.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Framework for MDIO devices, other than PHYs.
  3. *
  4. * Copyright (c) 2016 Andrew Lunn <[email protected]>
  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, struct device_driver *drv)
  34. {
  35. struct mdio_device *mdiodev = to_mdio_device(dev);
  36. 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. dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
  56. device_initialize(&mdiodev->dev);
  57. return mdiodev;
  58. }
  59. EXPORT_SYMBOL(mdio_device_create);
  60. /**
  61. * mdio_device_register - Register the mdio device on the MDIO bus
  62. * @mdiodev: mdio_device structure to be added to the MDIO bus
  63. */
  64. int mdio_device_register(struct mdio_device *mdiodev)
  65. {
  66. int err;
  67. dev_dbg(&mdiodev->dev, "%s\n", __func__);
  68. err = mdiobus_register_device(mdiodev);
  69. if (err)
  70. return err;
  71. err = device_add(&mdiodev->dev);
  72. if (err) {
  73. pr_err("MDIO %d failed to add\n", mdiodev->addr);
  74. goto out;
  75. }
  76. return 0;
  77. out:
  78. mdiobus_unregister_device(mdiodev);
  79. return err;
  80. }
  81. EXPORT_SYMBOL(mdio_device_register);
  82. /**
  83. * mdio_device_remove - Remove a previously registered mdio device from the
  84. * MDIO bus
  85. * @mdiodev: mdio_device structure to remove
  86. *
  87. * This doesn't free the mdio_device itself, it merely reverses the effects
  88. * of mdio_device_register(). Use mdio_device_free() to free the device
  89. * after calling this function.
  90. */
  91. void mdio_device_remove(struct mdio_device *mdiodev)
  92. {
  93. device_del(&mdiodev->dev);
  94. mdiobus_unregister_device(mdiodev);
  95. }
  96. EXPORT_SYMBOL(mdio_device_remove);
  97. void mdio_device_reset(struct mdio_device *mdiodev, int value)
  98. {
  99. unsigned int d;
  100. if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
  101. return;
  102. if (mdiodev->reset_gpio)
  103. gpiod_set_value_cansleep(mdiodev->reset_gpio, value);
  104. if (mdiodev->reset_ctrl) {
  105. if (value)
  106. reset_control_assert(mdiodev->reset_ctrl);
  107. else
  108. reset_control_deassert(mdiodev->reset_ctrl);
  109. }
  110. d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
  111. if (d)
  112. fsleep(d);
  113. }
  114. EXPORT_SYMBOL(mdio_device_reset);
  115. /**
  116. * mdio_probe - probe an MDIO device
  117. * @dev: device to probe
  118. *
  119. * Description: Take care of setting up the mdio_device structure
  120. * and calling the driver to probe the device.
  121. */
  122. static int mdio_probe(struct device *dev)
  123. {
  124. struct mdio_device *mdiodev = to_mdio_device(dev);
  125. struct device_driver *drv = mdiodev->dev.driver;
  126. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  127. int err = 0;
  128. /* Deassert the reset signal */
  129. mdio_device_reset(mdiodev, 0);
  130. if (mdiodrv->probe) {
  131. err = mdiodrv->probe(mdiodev);
  132. if (err) {
  133. /* Assert the reset signal */
  134. mdio_device_reset(mdiodev, 1);
  135. }
  136. }
  137. return err;
  138. }
  139. static int mdio_remove(struct device *dev)
  140. {
  141. struct mdio_device *mdiodev = to_mdio_device(dev);
  142. struct device_driver *drv = mdiodev->dev.driver;
  143. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  144. if (mdiodrv->remove)
  145. mdiodrv->remove(mdiodev);
  146. /* Assert the reset signal */
  147. mdio_device_reset(mdiodev, 1);
  148. return 0;
  149. }
  150. static void mdio_shutdown(struct device *dev)
  151. {
  152. struct mdio_device *mdiodev = to_mdio_device(dev);
  153. struct device_driver *drv = mdiodev->dev.driver;
  154. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  155. if (mdiodrv->shutdown)
  156. mdiodrv->shutdown(mdiodev);
  157. }
  158. /**
  159. * mdio_driver_register - register an mdio_driver with the MDIO layer
  160. * @drv: new mdio_driver to register
  161. */
  162. int mdio_driver_register(struct mdio_driver *drv)
  163. {
  164. struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
  165. int retval;
  166. pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
  167. mdiodrv->driver.bus = &mdio_bus_type;
  168. mdiodrv->driver.probe = mdio_probe;
  169. mdiodrv->driver.remove = mdio_remove;
  170. mdiodrv->driver.shutdown = mdio_shutdown;
  171. retval = driver_register(&mdiodrv->driver);
  172. if (retval) {
  173. pr_err("%s: Error %d in registering driver\n",
  174. mdiodrv->driver.name, retval);
  175. return retval;
  176. }
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(mdio_driver_register);
  180. void mdio_driver_unregister(struct mdio_driver *drv)
  181. {
  182. struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
  183. driver_unregister(&mdiodrv->driver);
  184. }
  185. EXPORT_SYMBOL(mdio_driver_unregister);