1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for MDIO devices, other than PHYs.
4 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/errno.h>
10 #include <linux/gpio.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mdio.h>
16 #include <linux/mii.h>
17 #include <linux/module.h>
18 #include <linux/phy.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/unistd.h>
22 #include <linux/delay.h>
24 void mdio_device_free(struct mdio_device
*mdiodev
)
26 put_device(&mdiodev
->dev
);
28 EXPORT_SYMBOL(mdio_device_free
);
30 static void mdio_device_release(struct device
*dev
)
32 kfree(to_mdio_device(dev
));
35 int mdio_device_bus_match(struct device
*dev
, struct device_driver
*drv
)
37 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
38 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
40 if (mdiodrv
->mdiodrv
.flags
& MDIO_DEVICE_IS_PHY
)
43 return strcmp(mdiodev
->modalias
, drv
->name
) == 0;
46 struct mdio_device
*mdio_device_create(struct mii_bus
*bus
, int addr
)
48 struct mdio_device
*mdiodev
;
50 /* We allocate the device, and initialize the default values */
51 mdiodev
= kzalloc(sizeof(*mdiodev
), GFP_KERNEL
);
53 return ERR_PTR(-ENOMEM
);
55 mdiodev
->dev
.release
= mdio_device_release
;
56 mdiodev
->dev
.parent
= &bus
->dev
;
57 mdiodev
->dev
.bus
= &mdio_bus_type
;
58 mdiodev
->device_free
= mdio_device_free
;
59 mdiodev
->device_remove
= mdio_device_remove
;
63 dev_set_name(&mdiodev
->dev
, PHY_ID_FMT
, bus
->id
, addr
);
65 device_initialize(&mdiodev
->dev
);
69 EXPORT_SYMBOL(mdio_device_create
);
72 * mdio_device_register - Register the mdio device on the MDIO bus
73 * @mdiodev: mdio_device structure to be added to the MDIO bus
75 int mdio_device_register(struct mdio_device
*mdiodev
)
79 dev_dbg(&mdiodev
->dev
, "mdio_device_register\n");
81 err
= mdiobus_register_device(mdiodev
);
85 err
= device_add(&mdiodev
->dev
);
87 pr_err("MDIO %d failed to add\n", mdiodev
->addr
);
94 mdiobus_unregister_device(mdiodev
);
97 EXPORT_SYMBOL(mdio_device_register
);
100 * mdio_device_remove - Remove a previously registered mdio device from the
102 * @mdiodev: mdio_device structure to remove
104 * This doesn't free the mdio_device itself, it merely reverses the effects
105 * of mdio_device_register(). Use mdio_device_free() to free the device
106 * after calling this function.
108 void mdio_device_remove(struct mdio_device
*mdiodev
)
110 device_del(&mdiodev
->dev
);
111 mdiobus_unregister_device(mdiodev
);
113 EXPORT_SYMBOL(mdio_device_remove
);
115 void mdio_device_reset(struct mdio_device
*mdiodev
, int value
)
122 gpiod_set_value(mdiodev
->reset
, value
);
124 d
= value
? mdiodev
->reset_assert_delay
: mdiodev
->reset_deassert_delay
;
126 usleep_range(d
, d
+ max_t(unsigned int, d
/ 10, 100));
128 EXPORT_SYMBOL(mdio_device_reset
);
131 * mdio_probe - probe an MDIO device
132 * @dev: device to probe
134 * Description: Take care of setting up the mdio_device structure
135 * and calling the driver to probe the device.
137 static int mdio_probe(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 if (mdiodrv
->probe
) {
145 /* Deassert the reset signal */
146 mdio_device_reset(mdiodev
, 0);
148 err
= mdiodrv
->probe(mdiodev
);
150 /* Assert the reset signal */
151 mdio_device_reset(mdiodev
, 1);
158 static int mdio_remove(struct device
*dev
)
160 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
161 struct device_driver
*drv
= mdiodev
->dev
.driver
;
162 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
164 if (mdiodrv
->remove
) {
165 mdiodrv
->remove(mdiodev
);
167 /* Assert the reset signal */
168 mdio_device_reset(mdiodev
, 1);
175 * mdio_driver_register - register an mdio_driver with the MDIO layer
176 * @new_driver: new mdio_driver to register
178 int mdio_driver_register(struct mdio_driver
*drv
)
180 struct mdio_driver_common
*mdiodrv
= &drv
->mdiodrv
;
183 pr_debug("mdio_driver_register: %s\n", mdiodrv
->driver
.name
);
185 mdiodrv
->driver
.bus
= &mdio_bus_type
;
186 mdiodrv
->driver
.probe
= mdio_probe
;
187 mdiodrv
->driver
.remove
= mdio_remove
;
189 retval
= driver_register(&mdiodrv
->driver
);
191 pr_err("%s: Error %d in registering driver\n",
192 mdiodrv
->driver
.name
, retval
);
199 EXPORT_SYMBOL(mdio_driver_register
);
201 void mdio_driver_unregister(struct mdio_driver
*drv
)
203 struct mdio_driver_common
*mdiodrv
= &drv
->mdiodrv
;
205 driver_unregister(&mdiodrv
->driver
);
207 EXPORT_SYMBOL(mdio_driver_unregister
);