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