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>
24 #include <linux/property.h>
26 void mdio_device_free(struct mdio_device *mdiodev)
28 put_device(&mdiodev->dev);
30 EXPORT_SYMBOL(mdio_device_free);
32 static void mdio_device_release(struct device *dev)
34 fwnode_handle_put(dev->fwnode);
35 kfree(to_mdio_device(dev));
38 int mdio_device_bus_match(struct device *dev, const struct device_driver *drv)
40 struct mdio_device *mdiodev = to_mdio_device(dev);
41 const struct mdio_driver *mdiodrv = to_mdio_driver(drv);
43 if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
46 return strcmp(mdiodev->modalias, drv->name) == 0;
48 EXPORT_SYMBOL_GPL(mdio_device_bus_match);
50 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
52 struct mdio_device *mdiodev;
54 /* We allocate the device, and initialize the default values */
55 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
57 return ERR_PTR(-ENOMEM);
59 mdiodev->dev.release = mdio_device_release;
60 mdiodev->dev.parent = &bus->dev;
61 mdiodev->dev.bus = &mdio_bus_type;
62 mdiodev->device_free = mdio_device_free;
63 mdiodev->device_remove = mdio_device_remove;
66 mdiodev->reset_state = -1;
68 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
70 device_initialize(&mdiodev->dev);
74 EXPORT_SYMBOL(mdio_device_create);
77 * mdio_device_register - Register the mdio device on the MDIO bus
78 * @mdiodev: mdio_device structure to be added to the MDIO bus
80 int mdio_device_register(struct mdio_device *mdiodev)
84 dev_dbg(&mdiodev->dev, "%s\n", __func__);
86 err = mdiobus_register_device(mdiodev);
90 err = device_add(&mdiodev->dev);
92 pr_err("MDIO %d failed to add\n", mdiodev->addr);
99 mdiobus_unregister_device(mdiodev);
102 EXPORT_SYMBOL(mdio_device_register);
105 * mdio_device_remove - Remove a previously registered mdio device from the
107 * @mdiodev: mdio_device structure to remove
109 * This doesn't free the mdio_device itself, it merely reverses the effects
110 * of mdio_device_register(). Use mdio_device_free() to free the device
111 * after calling this function.
113 void mdio_device_remove(struct mdio_device *mdiodev)
115 device_del(&mdiodev->dev);
116 mdiobus_unregister_device(mdiodev);
118 EXPORT_SYMBOL(mdio_device_remove);
120 void mdio_device_reset(struct mdio_device *mdiodev, int value)
124 if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
127 if (mdiodev->reset_state == value)
130 if (mdiodev->reset_gpio)
131 gpiod_set_value_cansleep(mdiodev->reset_gpio, value);
133 if (mdiodev->reset_ctrl) {
135 reset_control_assert(mdiodev->reset_ctrl);
137 reset_control_deassert(mdiodev->reset_ctrl);
140 d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
144 mdiodev->reset_state = value;
146 EXPORT_SYMBOL(mdio_device_reset);
149 * mdio_probe - probe an MDIO device
150 * @dev: device to probe
152 * Description: Take care of setting up the mdio_device structure
153 * and calling the driver to probe the device.
155 static int mdio_probe(struct device *dev)
157 struct mdio_device *mdiodev = to_mdio_device(dev);
158 struct device_driver *drv = mdiodev->dev.driver;
159 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
162 /* Deassert the reset signal */
163 mdio_device_reset(mdiodev, 0);
165 if (mdiodrv->probe) {
166 err = mdiodrv->probe(mdiodev);
168 /* Assert the reset signal */
169 mdio_device_reset(mdiodev, 1);
176 static int mdio_remove(struct device *dev)
178 struct mdio_device *mdiodev = to_mdio_device(dev);
179 struct device_driver *drv = mdiodev->dev.driver;
180 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
183 mdiodrv->remove(mdiodev);
185 /* Assert the reset signal */
186 mdio_device_reset(mdiodev, 1);
191 static void mdio_shutdown(struct device *dev)
193 struct mdio_device *mdiodev = to_mdio_device(dev);
194 struct device_driver *drv = mdiodev->dev.driver;
195 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
197 if (mdiodrv->shutdown)
198 mdiodrv->shutdown(mdiodev);
202 * mdio_driver_register - register an mdio_driver with the MDIO layer
203 * @drv: new mdio_driver to register
205 int mdio_driver_register(struct mdio_driver *drv)
207 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
210 pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
212 mdiodrv->driver.bus = &mdio_bus_type;
213 mdiodrv->driver.probe = mdio_probe;
214 mdiodrv->driver.remove = mdio_remove;
215 mdiodrv->driver.shutdown = mdio_shutdown;
217 retval = driver_register(&mdiodrv->driver);
219 pr_err("%s: Error %d in registering driver\n",
220 mdiodrv->driver.name, retval);
227 EXPORT_SYMBOL(mdio_driver_register);
229 void mdio_driver_unregister(struct mdio_driver *drv)
231 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
233 driver_unregister(&mdiodrv->driver);
235 EXPORT_SYMBOL(mdio_driver_unregister);