Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / phy / mdio_devres.c
blobb560e99695dfd3f4ca3db7a8f9080a1629a26408
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/device.h>
4 #include <linux/of_mdio.h>
5 #include <linux/phy.h>
6 #include <linux/stddef.h>
8 struct mdiobus_devres {
9 struct mii_bus *mii;
12 static void devm_mdiobus_free(struct device *dev, void *this)
14 struct mdiobus_devres *dr = this;
16 mdiobus_free(dr->mii);
19 /**
20 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
21 * @dev: Device to allocate mii_bus for
22 * @sizeof_priv: Space to allocate for private structure
24 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
25 * automatically freed on driver detach.
27 * RETURNS:
28 * Pointer to allocated mii_bus on success, NULL on out-of-memory error.
30 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
32 struct mdiobus_devres *dr;
34 dr = devres_alloc(devm_mdiobus_free, sizeof(*dr), GFP_KERNEL);
35 if (!dr)
36 return NULL;
38 dr->mii = mdiobus_alloc_size(sizeof_priv);
39 if (!dr->mii) {
40 devres_free(dr);
41 return NULL;
44 devres_add(dev, dr);
45 return dr->mii;
47 EXPORT_SYMBOL(devm_mdiobus_alloc_size);
49 static void devm_mdiobus_unregister(struct device *dev, void *this)
51 struct mdiobus_devres *dr = this;
53 mdiobus_unregister(dr->mii);
56 static int mdiobus_devres_match(struct device *dev,
57 void *this, void *match_data)
59 struct mdiobus_devres *res = this;
60 struct mii_bus *mii = match_data;
62 return mii == res->mii;
65 /**
66 * __devm_mdiobus_register - Resource-managed variant of mdiobus_register()
67 * @dev: Device to register mii_bus for
68 * @bus: MII bus structure to register
69 * @owner: Owning module
71 * Returns 0 on success, negative error number on failure.
73 int __devm_mdiobus_register(struct device *dev, struct mii_bus *bus,
74 struct module *owner)
76 struct mdiobus_devres *dr;
77 int ret;
79 if (WARN_ON(!devres_find(dev, devm_mdiobus_free,
80 mdiobus_devres_match, bus)))
81 return -EINVAL;
83 dr = devres_alloc(devm_mdiobus_unregister, sizeof(*dr), GFP_KERNEL);
84 if (!dr)
85 return -ENOMEM;
87 ret = __mdiobus_register(bus, owner);
88 if (ret) {
89 devres_free(dr);
90 return ret;
93 dr->mii = bus;
94 devres_add(dev, dr);
95 return 0;
97 EXPORT_SYMBOL(__devm_mdiobus_register);
99 #if IS_ENABLED(CONFIG_OF_MDIO)
101 * devm_of_mdiobus_register - Resource managed variant of of_mdiobus_register()
102 * @dev: Device to register mii_bus for
103 * @mdio: MII bus structure to register
104 * @np: Device node to parse
106 int devm_of_mdiobus_register(struct device *dev, struct mii_bus *mdio,
107 struct device_node *np)
109 struct mdiobus_devres *dr;
110 int ret;
112 if (WARN_ON(!devres_find(dev, devm_mdiobus_free,
113 mdiobus_devres_match, mdio)))
114 return -EINVAL;
116 dr = devres_alloc(devm_mdiobus_unregister, sizeof(*dr), GFP_KERNEL);
117 if (!dr)
118 return -ENOMEM;
120 ret = of_mdiobus_register(mdio, np);
121 if (ret) {
122 devres_free(dr);
123 return ret;
126 dr->mii = mdio;
127 devres_add(dev, dr);
128 return 0;
130 EXPORT_SYMBOL(devm_of_mdiobus_register);
131 #endif /* CONFIG_OF_MDIO */
133 MODULE_LICENSE("GPL");