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/delay.h>
10 #include <linux/errno.h>
11 #include <linux/gpio.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/mdio.h>
17 #include <linux/mii.h>
18 #include <linux/module.h>
19 #include <linux/phy.h>
20 #include <linux/reset.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/unistd.h>
24 #include <linux/property.h>
26 void mdio_device_free(struct mdio_device
*mdiodev
)
28 put_device(&mdiodev
->dev
);
30 EXPORT_SYMBOL(mdio_device_free
);
32 static void mdio_device_release(struct device
*dev
)
34 fwnode_handle_put(dev
->fwnode
);
35 kfree(to_mdio_device(dev
));
38 int mdio_device_bus_match(struct device
*dev
, const struct device_driver
*drv
)
40 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
41 const struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
43 if (mdiodrv
->mdiodrv
.flags
& MDIO_DEVICE_IS_PHY
)
46 return strcmp(mdiodev
->modalias
, drv
->name
) == 0;
49 struct mdio_device
*mdio_device_create(struct mii_bus
*bus
, int addr
)
51 struct mdio_device
*mdiodev
;
53 /* We allocate the device, and initialize the default values */
54 mdiodev
= kzalloc(sizeof(*mdiodev
), GFP_KERNEL
);
56 return ERR_PTR(-ENOMEM
);
58 mdiodev
->dev
.release
= mdio_device_release
;
59 mdiodev
->dev
.parent
= &bus
->dev
;
60 mdiodev
->dev
.bus
= &mdio_bus_type
;
61 mdiodev
->device_free
= mdio_device_free
;
62 mdiodev
->device_remove
= mdio_device_remove
;
65 mdiodev
->reset_state
= -1;
67 dev_set_name(&mdiodev
->dev
, PHY_ID_FMT
, bus
->id
, addr
);
69 device_initialize(&mdiodev
->dev
);
73 EXPORT_SYMBOL(mdio_device_create
);
76 * mdio_device_register - Register the mdio device on the MDIO bus
77 * @mdiodev: mdio_device structure to be added to the MDIO bus
79 int mdio_device_register(struct mdio_device
*mdiodev
)
83 dev_dbg(&mdiodev
->dev
, "%s\n", __func__
);
85 err
= mdiobus_register_device(mdiodev
);
89 err
= device_add(&mdiodev
->dev
);
91 pr_err("MDIO %d failed to add\n", mdiodev
->addr
);
98 mdiobus_unregister_device(mdiodev
);
101 EXPORT_SYMBOL(mdio_device_register
);
104 * mdio_device_remove - Remove a previously registered mdio device from the
106 * @mdiodev: mdio_device structure to remove
108 * This doesn't free the mdio_device itself, it merely reverses the effects
109 * of mdio_device_register(). Use mdio_device_free() to free the device
110 * after calling this function.
112 void mdio_device_remove(struct mdio_device
*mdiodev
)
114 device_del(&mdiodev
->dev
);
115 mdiobus_unregister_device(mdiodev
);
117 EXPORT_SYMBOL(mdio_device_remove
);
119 void mdio_device_reset(struct mdio_device
*mdiodev
, int value
)
123 if (!mdiodev
->reset_gpio
&& !mdiodev
->reset_ctrl
)
126 if (mdiodev
->reset_state
== value
)
129 if (mdiodev
->reset_gpio
)
130 gpiod_set_value_cansleep(mdiodev
->reset_gpio
, value
);
132 if (mdiodev
->reset_ctrl
) {
134 reset_control_assert(mdiodev
->reset_ctrl
);
136 reset_control_deassert(mdiodev
->reset_ctrl
);
139 d
= value
? mdiodev
->reset_assert_delay
: mdiodev
->reset_deassert_delay
;
143 mdiodev
->reset_state
= value
;
145 EXPORT_SYMBOL(mdio_device_reset
);
148 * mdio_probe - probe an MDIO device
149 * @dev: device to probe
151 * Description: Take care of setting up the mdio_device structure
152 * and calling the driver to probe the device.
154 static int mdio_probe(struct device
*dev
)
156 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
157 struct device_driver
*drv
= mdiodev
->dev
.driver
;
158 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
161 /* Deassert the reset signal */
162 mdio_device_reset(mdiodev
, 0);
164 if (mdiodrv
->probe
) {
165 err
= mdiodrv
->probe(mdiodev
);
167 /* Assert the reset signal */
168 mdio_device_reset(mdiodev
, 1);
175 static int mdio_remove(struct device
*dev
)
177 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
178 struct device_driver
*drv
= mdiodev
->dev
.driver
;
179 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
182 mdiodrv
->remove(mdiodev
);
184 /* Assert the reset signal */
185 mdio_device_reset(mdiodev
, 1);
190 static void mdio_shutdown(struct device
*dev
)
192 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
193 struct device_driver
*drv
= mdiodev
->dev
.driver
;
194 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
196 if (mdiodrv
->shutdown
)
197 mdiodrv
->shutdown(mdiodev
);
201 * mdio_driver_register - register an mdio_driver with the MDIO layer
202 * @drv: new mdio_driver to register
204 int mdio_driver_register(struct mdio_driver
*drv
)
206 struct mdio_driver_common
*mdiodrv
= &drv
->mdiodrv
;
209 pr_debug("%s: %s\n", __func__
, mdiodrv
->driver
.name
);
211 mdiodrv
->driver
.bus
= &mdio_bus_type
;
212 mdiodrv
->driver
.probe
= mdio_probe
;
213 mdiodrv
->driver
.remove
= mdio_remove
;
214 mdiodrv
->driver
.shutdown
= mdio_shutdown
;
216 retval
= driver_register(&mdiodrv
->driver
);
218 pr_err("%s: Error %d in registering driver\n",
219 mdiodrv
->driver
.name
, retval
);
226 EXPORT_SYMBOL(mdio_driver_register
);
228 void mdio_driver_unregister(struct mdio_driver
*drv
)
230 struct mdio_driver_common
*mdiodrv
= &drv
->mdiodrv
;
232 driver_unregister(&mdiodrv
->driver
);
234 EXPORT_SYMBOL(mdio_driver_unregister
);