1 /* Framework for MDIO devices, other than PHYs.
3 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/errno.h>
15 #include <linux/gpio.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/mdio.h>
21 #include <linux/mii.h>
22 #include <linux/module.h>
23 #include <linux/phy.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/unistd.h>
27 #include <linux/delay.h>
29 void mdio_device_free(struct mdio_device
*mdiodev
)
31 put_device(&mdiodev
->dev
);
33 EXPORT_SYMBOL(mdio_device_free
);
35 static void mdio_device_release(struct device
*dev
)
37 kfree(to_mdio_device(dev
));
40 int mdio_device_bus_match(struct device
*dev
, struct device_driver
*drv
)
42 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
43 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
45 if (mdiodrv
->mdiodrv
.flags
& MDIO_DEVICE_IS_PHY
)
48 return strcmp(mdiodev
->modalias
, drv
->name
) == 0;
51 struct mdio_device
*mdio_device_create(struct mii_bus
*bus
, int addr
)
53 struct mdio_device
*mdiodev
;
55 /* We allocate the device, and initialize the default values */
56 mdiodev
= kzalloc(sizeof(*mdiodev
), GFP_KERNEL
);
58 return ERR_PTR(-ENOMEM
);
60 mdiodev
->dev
.release
= mdio_device_release
;
61 mdiodev
->dev
.parent
= &bus
->dev
;
62 mdiodev
->dev
.bus
= &mdio_bus_type
;
63 mdiodev
->device_free
= mdio_device_free
;
64 mdiodev
->device_remove
= mdio_device_remove
;
68 dev_set_name(&mdiodev
->dev
, PHY_ID_FMT
, bus
->id
, addr
);
70 device_initialize(&mdiodev
->dev
);
74 EXPORT_SYMBOL(mdio_device_create
);
77 * mdio_device_register - Register the mdio device on the MDIO bus
78 * @mdiodev: mdio_device structure to be added to the MDIO bus
80 int mdio_device_register(struct mdio_device
*mdiodev
)
84 dev_dbg(&mdiodev
->dev
, "mdio_device_register\n");
86 err
= mdiobus_register_device(mdiodev
);
90 err
= device_add(&mdiodev
->dev
);
92 pr_err("MDIO %d failed to add\n", mdiodev
->addr
);
99 mdiobus_unregister_device(mdiodev
);
102 EXPORT_SYMBOL(mdio_device_register
);
105 * mdio_device_remove - Remove a previously registered mdio device from the
107 * @mdiodev: mdio_device structure to remove
109 * This doesn't free the mdio_device itself, it merely reverses the effects
110 * of mdio_device_register(). Use mdio_device_free() to free the device
111 * after calling this function.
113 void mdio_device_remove(struct mdio_device
*mdiodev
)
115 device_del(&mdiodev
->dev
);
116 mdiobus_unregister_device(mdiodev
);
118 EXPORT_SYMBOL(mdio_device_remove
);
120 void mdio_device_reset(struct mdio_device
*mdiodev
, int value
)
127 gpiod_set_value(mdiodev
->reset
, value
);
129 d
= value
? mdiodev
->reset_assert_delay
: mdiodev
->reset_deassert_delay
;
131 usleep_range(d
, d
+ max_t(unsigned int, d
/ 10, 100));
133 EXPORT_SYMBOL(mdio_device_reset
);
136 * mdio_probe - probe an MDIO device
137 * @dev: device to probe
139 * Description: Take care of setting up the mdio_device structure
140 * and calling the driver to probe the device.
142 static int mdio_probe(struct device
*dev
)
144 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
145 struct device_driver
*drv
= mdiodev
->dev
.driver
;
146 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
149 if (mdiodrv
->probe
) {
150 /* Deassert the reset signal */
151 mdio_device_reset(mdiodev
, 0);
153 err
= mdiodrv
->probe(mdiodev
);
155 /* Assert the reset signal */
156 mdio_device_reset(mdiodev
, 1);
163 static int mdio_remove(struct device
*dev
)
165 struct mdio_device
*mdiodev
= to_mdio_device(dev
);
166 struct device_driver
*drv
= mdiodev
->dev
.driver
;
167 struct mdio_driver
*mdiodrv
= to_mdio_driver(drv
);
169 if (mdiodrv
->remove
) {
170 mdiodrv
->remove(mdiodev
);
172 /* Assert the reset signal */
173 mdio_device_reset(mdiodev
, 1);
180 * mdio_driver_register - register an mdio_driver with the MDIO layer
181 * @new_driver: new mdio_driver to register
183 int mdio_driver_register(struct mdio_driver
*drv
)
185 struct mdio_driver_common
*mdiodrv
= &drv
->mdiodrv
;
188 pr_debug("mdio_driver_register: %s\n", mdiodrv
->driver
.name
);
190 mdiodrv
->driver
.bus
= &mdio_bus_type
;
191 mdiodrv
->driver
.probe
= mdio_probe
;
192 mdiodrv
->driver
.remove
= mdio_remove
;
194 retval
= driver_register(&mdiodrv
->driver
);
196 pr_err("%s: Error %d in registering driver\n",
197 mdiodrv
->driver
.name
, retval
);
204 EXPORT_SYMBOL(mdio_driver_register
);
206 void mdio_driver_unregister(struct mdio_driver
*drv
)
208 struct mdio_driver_common
*mdiodrv
= &drv
->mdiodrv
;
210 driver_unregister(&mdiodrv
->driver
);
212 EXPORT_SYMBOL(mdio_driver_unregister
);