1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for MDIO devices, other than PHYs.
4 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/gpio.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/mdio.h>
17 #include <linux/mii.h>
18 #include <linux/module.h>
19 #include <linux/phy.h>
20 #include <linux/reset.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/unistd.h>
25 void mdio_device_free(struct mdio_device *mdiodev)
27 put_device(&mdiodev->dev);
29 EXPORT_SYMBOL(mdio_device_free);
31 static void mdio_device_release(struct device *dev)
33 kfree(to_mdio_device(dev));
36 int mdio_device_bus_match(struct device *dev, struct device_driver *drv)
38 struct mdio_device *mdiodev = to_mdio_device(dev);
39 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
41 if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
44 return strcmp(mdiodev->modalias, drv->name) == 0;
47 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
49 struct mdio_device *mdiodev;
51 /* We allocate the device, and initialize the default values */
52 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
54 return ERR_PTR(-ENOMEM);
56 mdiodev->dev.release = mdio_device_release;
57 mdiodev->dev.parent = &bus->dev;
58 mdiodev->dev.bus = &mdio_bus_type;
59 mdiodev->device_free = mdio_device_free;
60 mdiodev->device_remove = mdio_device_remove;
64 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
66 device_initialize(&mdiodev->dev);
70 EXPORT_SYMBOL(mdio_device_create);
73 * mdio_device_register - Register the mdio device on the MDIO bus
74 * @mdiodev: mdio_device structure to be added to the MDIO bus
76 int mdio_device_register(struct mdio_device *mdiodev)
80 dev_dbg(&mdiodev->dev, "%s\n", __func__);
82 err = mdiobus_register_device(mdiodev);
86 err = device_add(&mdiodev->dev);
88 pr_err("MDIO %d failed to add\n", mdiodev->addr);
95 mdiobus_unregister_device(mdiodev);
98 EXPORT_SYMBOL(mdio_device_register);
101 * mdio_device_remove - Remove a previously registered mdio device from the
103 * @mdiodev: mdio_device structure to remove
105 * This doesn't free the mdio_device itself, it merely reverses the effects
106 * of mdio_device_register(). Use mdio_device_free() to free the device
107 * after calling this function.
109 void mdio_device_remove(struct mdio_device *mdiodev)
111 device_del(&mdiodev->dev);
112 mdiobus_unregister_device(mdiodev);
114 EXPORT_SYMBOL(mdio_device_remove);
116 void mdio_device_reset(struct mdio_device *mdiodev, int value)
120 if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
123 if (mdiodev->reset_gpio)
124 gpiod_set_value_cansleep(mdiodev->reset_gpio, value);
126 if (mdiodev->reset_ctrl) {
128 reset_control_assert(mdiodev->reset_ctrl);
130 reset_control_deassert(mdiodev->reset_ctrl);
133 d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
137 EXPORT_SYMBOL(mdio_device_reset);
140 * mdio_probe - probe an MDIO device
141 * @dev: device to probe
143 * Description: Take care of setting up the mdio_device structure
144 * and calling the driver to probe the device.
146 static int mdio_probe(struct device *dev)
148 struct mdio_device *mdiodev = to_mdio_device(dev);
149 struct device_driver *drv = mdiodev->dev.driver;
150 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
153 /* Deassert the reset signal */
154 mdio_device_reset(mdiodev, 0);
156 if (mdiodrv->probe) {
157 err = mdiodrv->probe(mdiodev);
159 /* Assert the reset signal */
160 mdio_device_reset(mdiodev, 1);
167 static int mdio_remove(struct device *dev)
169 struct mdio_device *mdiodev = to_mdio_device(dev);
170 struct device_driver *drv = mdiodev->dev.driver;
171 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
174 mdiodrv->remove(mdiodev);
176 /* Assert the reset signal */
177 mdio_device_reset(mdiodev, 1);
182 static void mdio_shutdown(struct device *dev)
184 struct mdio_device *mdiodev = to_mdio_device(dev);
185 struct device_driver *drv = mdiodev->dev.driver;
186 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
188 if (mdiodrv->shutdown)
189 mdiodrv->shutdown(mdiodev);
193 * mdio_driver_register - register an mdio_driver with the MDIO layer
194 * @drv: new mdio_driver to register
196 int mdio_driver_register(struct mdio_driver *drv)
198 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
201 pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
203 mdiodrv->driver.bus = &mdio_bus_type;
204 mdiodrv->driver.probe = mdio_probe;
205 mdiodrv->driver.remove = mdio_remove;
206 mdiodrv->driver.shutdown = mdio_shutdown;
208 retval = driver_register(&mdiodrv->driver);
210 pr_err("%s: Error %d in registering driver\n",
211 mdiodrv->driver.name, retval);
218 EXPORT_SYMBOL(mdio_driver_register);
220 void mdio_driver_unregister(struct mdio_driver *drv)
222 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
224 driver_unregister(&mdiodrv->driver);
226 EXPORT_SYMBOL(mdio_driver_unregister);