xref: /linux/drivers/net/phy/mdio_bus.c (revision 6ebe6dbd6886af07b102aca42e44edbee94a22d9)
1 /* MDIO Bus interface
2  *
3  * Author: Andy Fleming
4  *
5  * Copyright (c) 2004 Freescale Semiconductor, Inc.
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  *
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/gpio.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/of_device.h>
28 #include <linux/of_mdio.h>
29 #include <linux/of_gpio.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/spinlock.h>
34 #include <linux/mm.h>
35 #include <linux/module.h>
36 #include <linux/mii.h>
37 #include <linux/ethtool.h>
38 #include <linux/phy.h>
39 #include <linux/io.h>
40 #include <linux/uaccess.h>
41 #include <linux/gpio/consumer.h>
42 
43 #include <asm/irq.h>
44 
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/mdio.h>
47 
48 #include "mdio-boardinfo.h"
49 
50 int mdiobus_register_device(struct mdio_device *mdiodev)
51 {
52 	struct gpio_desc *gpiod = NULL;
53 
54 	if (mdiodev->bus->mdio_map[mdiodev->addr])
55 		return -EBUSY;
56 
57 	/* Deassert the optional reset signal */
58 	if (mdiodev->dev.of_node)
59 		gpiod = fwnode_get_named_gpiod(&mdiodev->dev.of_node->fwnode,
60 					       "reset-gpios", 0, GPIOD_OUT_LOW,
61 					       "PHY reset");
62 	if (PTR_ERR(gpiod) == -ENOENT)
63 		gpiod = NULL;
64 	else if (IS_ERR(gpiod))
65 		return PTR_ERR(gpiod);
66 
67 	mdiodev->reset = gpiod;
68 
69 	/* Assert the reset signal again */
70 	mdio_device_reset(mdiodev, 1);
71 
72 	mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
73 
74 	return 0;
75 }
76 EXPORT_SYMBOL(mdiobus_register_device);
77 
78 int mdiobus_unregister_device(struct mdio_device *mdiodev)
79 {
80 	if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
81 		return -EINVAL;
82 
83 	mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
84 
85 	return 0;
86 }
87 EXPORT_SYMBOL(mdiobus_unregister_device);
88 
89 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
90 {
91 	struct mdio_device *mdiodev = bus->mdio_map[addr];
92 
93 	if (!mdiodev)
94 		return NULL;
95 
96 	if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
97 		return NULL;
98 
99 	return container_of(mdiodev, struct phy_device, mdio);
100 }
101 EXPORT_SYMBOL(mdiobus_get_phy);
102 
103 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
104 {
105 	return bus->mdio_map[addr];
106 }
107 EXPORT_SYMBOL(mdiobus_is_registered_device);
108 
109 /**
110  * mdiobus_alloc_size - allocate a mii_bus structure
111  * @size: extra amount of memory to allocate for private storage.
112  * If non-zero, then bus->priv is points to that memory.
113  *
114  * Description: called by a bus driver to allocate an mii_bus
115  * structure to fill in.
116  */
117 struct mii_bus *mdiobus_alloc_size(size_t size)
118 {
119 	struct mii_bus *bus;
120 	size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
121 	size_t alloc_size;
122 	int i;
123 
124 	/* If we alloc extra space, it should be aligned */
125 	if (size)
126 		alloc_size = aligned_size + size;
127 	else
128 		alloc_size = sizeof(*bus);
129 
130 	bus = kzalloc(alloc_size, GFP_KERNEL);
131 	if (!bus)
132 		return NULL;
133 
134 	bus->state = MDIOBUS_ALLOCATED;
135 	if (size)
136 		bus->priv = (void *)bus + aligned_size;
137 
138 	/* Initialise the interrupts to polling */
139 	for (i = 0; i < PHY_MAX_ADDR; i++)
140 		bus->irq[i] = PHY_POLL;
141 
142 	return bus;
143 }
144 EXPORT_SYMBOL(mdiobus_alloc_size);
145 
146 static void _devm_mdiobus_free(struct device *dev, void *res)
147 {
148 	mdiobus_free(*(struct mii_bus **)res);
149 }
150 
151 static int devm_mdiobus_match(struct device *dev, void *res, void *data)
152 {
153 	struct mii_bus **r = res;
154 
155 	if (WARN_ON(!r || !*r))
156 		return 0;
157 
158 	return *r == data;
159 }
160 
161 /**
162  * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
163  * @dev:		Device to allocate mii_bus for
164  * @sizeof_priv:	Space to allocate for private structure.
165  *
166  * Managed mdiobus_alloc_size. mii_bus allocated with this function is
167  * automatically freed on driver detach.
168  *
169  * If an mii_bus allocated with this function needs to be freed separately,
170  * devm_mdiobus_free() must be used.
171  *
172  * RETURNS:
173  * Pointer to allocated mii_bus on success, NULL on failure.
174  */
175 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
176 {
177 	struct mii_bus **ptr, *bus;
178 
179 	ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
180 	if (!ptr)
181 		return NULL;
182 
183 	/* use raw alloc_dr for kmalloc caller tracing */
184 	bus = mdiobus_alloc_size(sizeof_priv);
185 	if (bus) {
186 		*ptr = bus;
187 		devres_add(dev, ptr);
188 	} else {
189 		devres_free(ptr);
190 	}
191 
192 	return bus;
193 }
194 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
195 
196 /**
197  * devm_mdiobus_free - Resource-managed mdiobus_free()
198  * @dev:		Device this mii_bus belongs to
199  * @bus:		the mii_bus associated with the device
200  *
201  * Free mii_bus allocated with devm_mdiobus_alloc_size().
202  */
203 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
204 {
205 	int rc;
206 
207 	rc = devres_release(dev, _devm_mdiobus_free,
208 			    devm_mdiobus_match, bus);
209 	WARN_ON(rc);
210 }
211 EXPORT_SYMBOL_GPL(devm_mdiobus_free);
212 
213 /**
214  * mdiobus_release - mii_bus device release callback
215  * @d: the target struct device that contains the mii_bus
216  *
217  * Description: called when the last reference to an mii_bus is
218  * dropped, to free the underlying memory.
219  */
220 static void mdiobus_release(struct device *d)
221 {
222 	struct mii_bus *bus = to_mii_bus(d);
223 	BUG_ON(bus->state != MDIOBUS_RELEASED &&
224 	       /* for compatibility with error handling in drivers */
225 	       bus->state != MDIOBUS_ALLOCATED);
226 	kfree(bus);
227 }
228 
229 static struct class mdio_bus_class = {
230 	.name		= "mdio_bus",
231 	.dev_release	= mdiobus_release,
232 };
233 
234 #if IS_ENABLED(CONFIG_OF_MDIO)
235 /* Helper function for of_mdio_find_bus */
236 static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
237 {
238 	return dev->of_node == mdio_bus_np;
239 }
240 /**
241  * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
242  * @mdio_bus_np: Pointer to the mii_bus.
243  *
244  * Returns a reference to the mii_bus, or NULL if none found.  The
245  * embedded struct device will have its reference count incremented,
246  * and this must be put once the bus is finished with.
247  *
248  * Because the association of a device_node and mii_bus is made via
249  * of_mdiobus_register(), the mii_bus cannot be found before it is
250  * registered with of_mdiobus_register().
251  *
252  */
253 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
254 {
255 	struct device *d;
256 
257 	if (!mdio_bus_np)
258 		return NULL;
259 
260 	d = class_find_device(&mdio_bus_class, NULL,  mdio_bus_np,
261 			      of_mdio_bus_match);
262 
263 	return d ? to_mii_bus(d) : NULL;
264 }
265 EXPORT_SYMBOL(of_mdio_find_bus);
266 
267 /* Walk the list of subnodes of a mdio bus and look for a node that
268  * matches the mdio device's address with its 'reg' property. If
269  * found, set the of_node pointer for the mdio device. This allows
270  * auto-probed phy devices to be supplied with information passed in
271  * via DT.
272  */
273 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
274 				    struct mdio_device *mdiodev)
275 {
276 	struct device *dev = &mdiodev->dev;
277 	struct device_node *child;
278 
279 	if (dev->of_node || !bus->dev.of_node)
280 		return;
281 
282 	for_each_available_child_of_node(bus->dev.of_node, child) {
283 		int addr;
284 
285 		addr = of_mdio_parse_addr(dev, child);
286 		if (addr < 0)
287 			continue;
288 
289 		if (addr == mdiodev->addr) {
290 			dev->of_node = child;
291 			dev->fwnode = of_fwnode_handle(child);
292 			return;
293 		}
294 	}
295 }
296 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
297 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
298 					   struct mdio_device *mdiodev)
299 {
300 }
301 #endif
302 
303 /**
304  * mdiobus_create_device_from_board_info - create a full MDIO device given
305  * a mdio_board_info structure
306  * @bus: MDIO bus to create the devices on
307  * @bi: mdio_board_info structure describing the devices
308  *
309  * Returns 0 on success or < 0 on error.
310  */
311 static int mdiobus_create_device(struct mii_bus *bus,
312 				 struct mdio_board_info *bi)
313 {
314 	struct mdio_device *mdiodev;
315 	int ret = 0;
316 
317 	mdiodev = mdio_device_create(bus, bi->mdio_addr);
318 	if (IS_ERR(mdiodev))
319 		return -ENODEV;
320 
321 	strncpy(mdiodev->modalias, bi->modalias,
322 		sizeof(mdiodev->modalias));
323 	mdiodev->bus_match = mdio_device_bus_match;
324 	mdiodev->dev.platform_data = (void *)bi->platform_data;
325 
326 	ret = mdio_device_register(mdiodev);
327 	if (ret)
328 		mdio_device_free(mdiodev);
329 
330 	return ret;
331 }
332 
333 /**
334  * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
335  * @bus: target mii_bus
336  * @owner: module containing bus accessor functions
337  *
338  * Description: Called by a bus driver to bring up all the PHYs
339  *   on a given bus, and attach them to the bus. Drivers should use
340  *   mdiobus_register() rather than __mdiobus_register() unless they
341  *   need to pass a specific owner module. MDIO devices which are not
342  *   PHYs will not be brought up by this function. They are expected to
343  *   to be explicitly listed in DT and instantiated by of_mdiobus_register().
344  *
345  * Returns 0 on success or < 0 on error.
346  */
347 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
348 {
349 	struct mdio_device *mdiodev;
350 	int i, err;
351 	struct gpio_desc *gpiod;
352 
353 	if (NULL == bus || NULL == bus->name ||
354 	    NULL == bus->read || NULL == bus->write)
355 		return -EINVAL;
356 
357 	BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
358 	       bus->state != MDIOBUS_UNREGISTERED);
359 
360 	bus->owner = owner;
361 	bus->dev.parent = bus->parent;
362 	bus->dev.class = &mdio_bus_class;
363 	bus->dev.groups = NULL;
364 	dev_set_name(&bus->dev, "%s", bus->id);
365 
366 	err = device_register(&bus->dev);
367 	if (err) {
368 		pr_err("mii_bus %s failed to register\n", bus->id);
369 		put_device(&bus->dev);
370 		return -EINVAL;
371 	}
372 
373 	mutex_init(&bus->mdio_lock);
374 
375 	/* de-assert bus level PHY GPIO reset */
376 	gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW);
377 	if (IS_ERR(gpiod)) {
378 		dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n",
379 			bus->id);
380 		return PTR_ERR(gpiod);
381 	} else	if (gpiod) {
382 		bus->reset_gpiod = gpiod;
383 
384 		gpiod_set_value_cansleep(gpiod, 1);
385 		udelay(bus->reset_delay_us);
386 		gpiod_set_value_cansleep(gpiod, 0);
387 	}
388 
389 	if (bus->reset)
390 		bus->reset(bus);
391 
392 	for (i = 0; i < PHY_MAX_ADDR; i++) {
393 		if ((bus->phy_mask & (1 << i)) == 0) {
394 			struct phy_device *phydev;
395 
396 			phydev = mdiobus_scan(bus, i);
397 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
398 				err = PTR_ERR(phydev);
399 				goto error;
400 			}
401 		}
402 	}
403 
404 	mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
405 
406 	bus->state = MDIOBUS_REGISTERED;
407 	pr_info("%s: probed\n", bus->name);
408 	return 0;
409 
410 error:
411 	while (--i >= 0) {
412 		mdiodev = bus->mdio_map[i];
413 		if (!mdiodev)
414 			continue;
415 
416 		mdiodev->device_remove(mdiodev);
417 		mdiodev->device_free(mdiodev);
418 	}
419 
420 	/* Put PHYs in RESET to save power */
421 	if (bus->reset_gpiod)
422 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
423 
424 	device_del(&bus->dev);
425 	return err;
426 }
427 EXPORT_SYMBOL(__mdiobus_register);
428 
429 void mdiobus_unregister(struct mii_bus *bus)
430 {
431 	struct mdio_device *mdiodev;
432 	int i;
433 
434 	BUG_ON(bus->state != MDIOBUS_REGISTERED);
435 	bus->state = MDIOBUS_UNREGISTERED;
436 
437 	for (i = 0; i < PHY_MAX_ADDR; i++) {
438 		mdiodev = bus->mdio_map[i];
439 		if (!mdiodev)
440 			continue;
441 
442 		if (mdiodev->reset)
443 			gpiod_put(mdiodev->reset);
444 
445 		mdiodev->device_remove(mdiodev);
446 		mdiodev->device_free(mdiodev);
447 	}
448 
449 	/* Put PHYs in RESET to save power */
450 	if (bus->reset_gpiod)
451 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
452 
453 	device_del(&bus->dev);
454 }
455 EXPORT_SYMBOL(mdiobus_unregister);
456 
457 /**
458  * mdiobus_free - free a struct mii_bus
459  * @bus: mii_bus to free
460  *
461  * This function releases the reference to the underlying device
462  * object in the mii_bus.  If this is the last reference, the mii_bus
463  * will be freed.
464  */
465 void mdiobus_free(struct mii_bus *bus)
466 {
467 	/* For compatibility with error handling in drivers. */
468 	if (bus->state == MDIOBUS_ALLOCATED) {
469 		kfree(bus);
470 		return;
471 	}
472 
473 	BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
474 	bus->state = MDIOBUS_RELEASED;
475 
476 	put_device(&bus->dev);
477 }
478 EXPORT_SYMBOL(mdiobus_free);
479 
480 /**
481  * mdiobus_scan - scan a bus for MDIO devices.
482  * @bus: mii_bus to scan
483  * @addr: address on bus to scan
484  *
485  * This function scans the MDIO bus, looking for devices which can be
486  * identified using a vendor/product ID in registers 2 and 3. Not all
487  * MDIO devices have such registers, but PHY devices typically
488  * do. Hence this function assumes anything found is a PHY, or can be
489  * treated as a PHY. Other MDIO devices, such as switches, will
490  * probably not be found during the scan.
491  */
492 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
493 {
494 	struct phy_device *phydev;
495 	int err;
496 
497 	phydev = get_phy_device(bus, addr, false);
498 	if (IS_ERR(phydev))
499 		return phydev;
500 
501 	/*
502 	 * For DT, see if the auto-probed phy has a correspoding child
503 	 * in the bus node, and set the of_node pointer in this case.
504 	 */
505 	of_mdiobus_link_mdiodev(bus, &phydev->mdio);
506 
507 	err = phy_device_register(phydev);
508 	if (err) {
509 		phy_device_free(phydev);
510 		return ERR_PTR(-ENODEV);
511 	}
512 
513 	return phydev;
514 }
515 EXPORT_SYMBOL(mdiobus_scan);
516 
517 /**
518  * mdiobus_read_nested - Nested version of the mdiobus_read function
519  * @bus: the mii_bus struct
520  * @addr: the phy address
521  * @regnum: register number to read
522  *
523  * In case of nested MDIO bus access avoid lockdep false positives by
524  * using mutex_lock_nested().
525  *
526  * NOTE: MUST NOT be called from interrupt context,
527  * because the bus read/write functions may wait for an interrupt
528  * to conclude the operation.
529  */
530 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
531 {
532 	int retval;
533 
534 	BUG_ON(in_interrupt());
535 
536 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
537 	retval = bus->read(bus, addr, regnum);
538 	mutex_unlock(&bus->mdio_lock);
539 
540 	trace_mdio_access(bus, 1, addr, regnum, retval, retval);
541 
542 	return retval;
543 }
544 EXPORT_SYMBOL(mdiobus_read_nested);
545 
546 /**
547  * mdiobus_read - Convenience function for reading a given MII mgmt register
548  * @bus: the mii_bus struct
549  * @addr: the phy address
550  * @regnum: register number to read
551  *
552  * NOTE: MUST NOT be called from interrupt context,
553  * because the bus read/write functions may wait for an interrupt
554  * to conclude the operation.
555  */
556 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
557 {
558 	int retval;
559 
560 	BUG_ON(in_interrupt());
561 
562 	mutex_lock(&bus->mdio_lock);
563 	retval = bus->read(bus, addr, regnum);
564 	mutex_unlock(&bus->mdio_lock);
565 
566 	trace_mdio_access(bus, 1, addr, regnum, retval, retval);
567 
568 	return retval;
569 }
570 EXPORT_SYMBOL(mdiobus_read);
571 
572 /**
573  * mdiobus_write_nested - Nested version of the mdiobus_write function
574  * @bus: the mii_bus struct
575  * @addr: the phy address
576  * @regnum: register number to write
577  * @val: value to write to @regnum
578  *
579  * In case of nested MDIO bus access avoid lockdep false positives by
580  * using mutex_lock_nested().
581  *
582  * NOTE: MUST NOT be called from interrupt context,
583  * because the bus read/write functions may wait for an interrupt
584  * to conclude the operation.
585  */
586 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
587 {
588 	int err;
589 
590 	BUG_ON(in_interrupt());
591 
592 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
593 	err = bus->write(bus, addr, regnum, val);
594 	mutex_unlock(&bus->mdio_lock);
595 
596 	trace_mdio_access(bus, 0, addr, regnum, val, err);
597 
598 	return err;
599 }
600 EXPORT_SYMBOL(mdiobus_write_nested);
601 
602 /**
603  * mdiobus_write - Convenience function for writing a given MII mgmt register
604  * @bus: the mii_bus struct
605  * @addr: the phy address
606  * @regnum: register number to write
607  * @val: value to write to @regnum
608  *
609  * NOTE: MUST NOT be called from interrupt context,
610  * because the bus read/write functions may wait for an interrupt
611  * to conclude the operation.
612  */
613 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
614 {
615 	int err;
616 
617 	BUG_ON(in_interrupt());
618 
619 	mutex_lock(&bus->mdio_lock);
620 	err = bus->write(bus, addr, regnum, val);
621 	mutex_unlock(&bus->mdio_lock);
622 
623 	trace_mdio_access(bus, 0, addr, regnum, val, err);
624 
625 	return err;
626 }
627 EXPORT_SYMBOL(mdiobus_write);
628 
629 /**
630  * mdio_bus_match - determine if given MDIO driver supports the given
631  *		    MDIO device
632  * @dev: target MDIO device
633  * @drv: given MDIO driver
634  *
635  * Description: Given a MDIO device, and a MDIO driver, return 1 if
636  *   the driver supports the device.  Otherwise, return 0. This may
637  *   require calling the devices own match function, since different classes
638  *   of MDIO devices have different match criteria.
639  */
640 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
641 {
642 	struct mdio_device *mdio = to_mdio_device(dev);
643 
644 	if (of_driver_match_device(dev, drv))
645 		return 1;
646 
647 	if (mdio->bus_match)
648 		return mdio->bus_match(dev, drv);
649 
650 	return 0;
651 }
652 
653 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
654 {
655 	int rc;
656 
657 	/* Some devices have extra OF data and an OF-style MODALIAS */
658 	rc = of_device_uevent_modalias(dev, env);
659 	if (rc != -ENODEV)
660 		return rc;
661 
662 	return 0;
663 }
664 
665 #ifdef CONFIG_PM
666 static int mdio_bus_suspend(struct device *dev)
667 {
668 	struct mdio_device *mdio = to_mdio_device(dev);
669 
670 	if (mdio->pm_ops && mdio->pm_ops->suspend)
671 		return mdio->pm_ops->suspend(dev);
672 
673 	return 0;
674 }
675 
676 static int mdio_bus_resume(struct device *dev)
677 {
678 	struct mdio_device *mdio = to_mdio_device(dev);
679 
680 	if (mdio->pm_ops && mdio->pm_ops->resume)
681 		return mdio->pm_ops->resume(dev);
682 
683 	return 0;
684 }
685 
686 static int mdio_bus_restore(struct device *dev)
687 {
688 	struct mdio_device *mdio = to_mdio_device(dev);
689 
690 	if (mdio->pm_ops && mdio->pm_ops->restore)
691 		return mdio->pm_ops->restore(dev);
692 
693 	return 0;
694 }
695 
696 static const struct dev_pm_ops mdio_bus_pm_ops = {
697 	.suspend = mdio_bus_suspend,
698 	.resume = mdio_bus_resume,
699 	.freeze = mdio_bus_suspend,
700 	.thaw = mdio_bus_resume,
701 	.restore = mdio_bus_restore,
702 };
703 
704 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
705 
706 #else
707 
708 #define MDIO_BUS_PM_OPS NULL
709 
710 #endif /* CONFIG_PM */
711 
712 struct bus_type mdio_bus_type = {
713 	.name		= "mdio_bus",
714 	.match		= mdio_bus_match,
715 	.uevent		= mdio_uevent,
716 	.pm		= MDIO_BUS_PM_OPS,
717 };
718 EXPORT_SYMBOL(mdio_bus_type);
719 
720 int __init mdio_bus_init(void)
721 {
722 	int ret;
723 
724 	ret = class_register(&mdio_bus_class);
725 	if (!ret) {
726 		ret = bus_register(&mdio_bus_type);
727 		if (ret)
728 			class_unregister(&mdio_bus_class);
729 	}
730 
731 	return ret;
732 }
733 EXPORT_SYMBOL_GPL(mdio_bus_init);
734 
735 #if IS_ENABLED(CONFIG_PHYLIB)
736 void mdio_bus_exit(void)
737 {
738 	class_unregister(&mdio_bus_class);
739 	bus_unregister(&mdio_bus_type);
740 }
741 EXPORT_SYMBOL_GPL(mdio_bus_exit);
742 #else
743 module_init(mdio_bus_init);
744 /* no module_exit, intentional */
745 MODULE_LICENSE("GPL");
746 MODULE_DESCRIPTION("MDIO bus/device layer");
747 #endif
748