1 /* Framework for MDIO devices, other than PHYs.
3 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mdio.h>
19 #include <linux/mii.h>
20 #include <linux/module.h>
21 #include <linux/phy.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/unistd.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 kfree(to_mdio_device(dev));
37 int mdio_device_bus_match(struct device *dev, struct device_driver *drv)
39 struct mdio_device *mdiodev = to_mdio_device(dev);
40 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
42 if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
45 return strcmp(mdiodev->modalias, drv->name) == 0;
48 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
50 struct mdio_device *mdiodev;
52 /* We allocate the device, and initialize the default values */
53 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
55 return ERR_PTR(-ENOMEM);
57 mdiodev->dev.release = mdio_device_release;
58 mdiodev->dev.parent = &bus->dev;
59 mdiodev->dev.bus = &mdio_bus_type;
60 mdiodev->device_free = mdio_device_free;
61 mdiodev->device_remove = mdio_device_remove;
65 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
67 device_initialize(&mdiodev->dev);
71 EXPORT_SYMBOL(mdio_device_create);
74 * mdio_device_register - Register the mdio device on the MDIO bus
75 * @mdiodev: mdio_device structure to be added to the MDIO bus
77 int mdio_device_register(struct mdio_device *mdiodev)
81 dev_dbg(&mdiodev->dev, "mdio_device_register\n");
83 err = mdiobus_register_device(mdiodev);
87 err = device_add(&mdiodev->dev);
89 pr_err("MDIO %d failed to add\n", mdiodev->addr);
96 mdiobus_unregister_device(mdiodev);
99 EXPORT_SYMBOL(mdio_device_register);
102 * mdio_device_remove - Remove a previously registered mdio device from the
104 * @mdiodev: mdio_device structure to remove
106 * This doesn't free the mdio_device itself, it merely reverses the effects
107 * of mdio_device_register(). Use mdio_device_free() to free the device
108 * after calling this function.
110 void mdio_device_remove(struct mdio_device *mdiodev)
112 device_del(&mdiodev->dev);
113 mdiobus_unregister_device(mdiodev);
115 EXPORT_SYMBOL(mdio_device_remove);
118 * mdio_probe - probe an MDIO device
119 * @dev: device to probe
121 * Description: Take care of setting up the mdio_device structure
122 * and calling the driver to probe the device.
124 static int mdio_probe(struct device *dev)
126 struct mdio_device *mdiodev = to_mdio_device(dev);
127 struct device_driver *drv = mdiodev->dev.driver;
128 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
132 err = mdiodrv->probe(mdiodev);
137 static int mdio_remove(struct device *dev)
139 struct mdio_device *mdiodev = to_mdio_device(dev);
140 struct device_driver *drv = mdiodev->dev.driver;
141 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
144 mdiodrv->remove(mdiodev);
149 static void mdio_shutdown(struct device *dev)
151 struct mdio_device *mdiodev = to_mdio_device(dev);
152 struct device_driver *drv = mdiodev->dev.driver;
153 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
155 if (mdiodrv->shutdown)
156 mdiodrv->shutdown(mdiodev);
160 * mdio_driver_register - register an mdio_driver with the MDIO layer
161 * @new_driver: new mdio_driver to register
163 int mdio_driver_register(struct mdio_driver *drv)
165 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
168 pr_debug("mdio_driver_register: %s\n", mdiodrv->driver.name);
170 mdiodrv->driver.bus = &mdio_bus_type;
171 mdiodrv->driver.probe = mdio_probe;
172 mdiodrv->driver.remove = mdio_remove;
173 mdiodrv->driver.shutdown = mdio_shutdown;
175 retval = driver_register(&mdiodrv->driver);
177 pr_err("%s: Error %d in registering driver\n",
178 mdiodrv->driver.name, retval);
185 EXPORT_SYMBOL(mdio_driver_register);
187 void mdio_driver_unregister(struct mdio_driver *drv)
189 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
191 driver_unregister(&mdiodrv->driver);
193 EXPORT_SYMBOL(mdio_driver_unregister);