1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/device.h>
4 #include <linux/of_mdio.h>
6 #include <linux/stddef.h>
8 struct mdiobus_devres
{
12 static void devm_mdiobus_free(struct device
*dev
, void *this)
14 struct mdiobus_devres
*dr
= this;
16 mdiobus_free(dr
->mii
);
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.
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
);
38 dr
->mii
= mdiobus_alloc_size(sizeof_priv
);
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
;
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
,
76 struct mdiobus_devres
*dr
;
79 if (WARN_ON(!devres_find(dev
, devm_mdiobus_free
,
80 mdiobus_devres_match
, bus
)))
83 dr
= devres_alloc(devm_mdiobus_unregister
, sizeof(*dr
), GFP_KERNEL
);
87 ret
= __mdiobus_register(bus
, owner
);
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
;
112 if (WARN_ON(!devres_find(dev
, devm_mdiobus_free
,
113 mdiobus_devres_match
, mdio
)))
116 dr
= devres_alloc(devm_mdiobus_unregister
, sizeof(*dr
), GFP_KERNEL
);
120 ret
= of_mdiobus_register(mdio
, np
);
130 EXPORT_SYMBOL(devm_of_mdiobus_register
);
131 #endif /* CONFIG_OF_MDIO */
133 MODULE_LICENSE("GPL");