1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Framework for MDIO devices, other than PHYs. 3 * 4 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 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> 25 #include "mdio-private.h" 26 27 void mdio_device_free(struct mdio_device *mdiodev) 28 { 29 put_device(&mdiodev->dev); 30 } 31 EXPORT_SYMBOL(mdio_device_free); 32 33 static void mdio_device_release(struct device *dev) 34 { 35 fwnode_handle_put(dev->fwnode); 36 kfree(to_mdio_device(dev)); 37 } 38 39 static int mdio_device_bus_match(struct device *dev, 40 const struct device_driver *drv) 41 { 42 struct mdio_device *mdiodev = to_mdio_device(dev); 43 const struct mdio_driver *mdiodrv = to_mdio_driver(drv); 44 45 if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) 46 return 0; 47 48 return strcmp(mdiodev->modalias, drv->name) == 0; 49 } 50 51 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr) 52 { 53 struct mdio_device *mdiodev; 54 55 /* We allocate the device, and initialize the default values */ 56 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL); 57 if (!mdiodev) 58 return ERR_PTR(-ENOMEM); 59 60 mdiodev->dev.release = mdio_device_release; 61 mdiodev->dev.parent = &bus->dev; 62 mdiodev->dev.bus = &mdio_bus_type; 63 mdiodev->bus_match = mdio_device_bus_match; 64 mdiodev->device_free = mdio_device_free; 65 mdiodev->device_remove = mdio_device_remove; 66 mdiodev->bus = bus; 67 mdiodev->addr = addr; 68 mdiodev->reset_state = -1; 69 70 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); 71 72 device_initialize(&mdiodev->dev); 73 74 return mdiodev; 75 } 76 EXPORT_SYMBOL(mdio_device_create); 77 78 /** 79 * mdio_device_register - Register the mdio device on the MDIO bus 80 * @mdiodev: mdio_device structure to be added to the MDIO bus 81 */ 82 int mdio_device_register(struct mdio_device *mdiodev) 83 { 84 int err; 85 86 dev_dbg(&mdiodev->dev, "%s\n", __func__); 87 88 err = mdiobus_register_device(mdiodev); 89 if (err) 90 return err; 91 92 err = device_add(&mdiodev->dev); 93 if (err) { 94 pr_err("MDIO %d failed to add\n", mdiodev->addr); 95 goto out; 96 } 97 98 return 0; 99 100 out: 101 mdiobus_unregister_device(mdiodev); 102 return err; 103 } 104 EXPORT_SYMBOL(mdio_device_register); 105 106 /** 107 * mdio_device_remove - Remove a previously registered mdio device from the 108 * MDIO bus 109 * @mdiodev: mdio_device structure to remove 110 * 111 * This doesn't free the mdio_device itself, it merely reverses the effects 112 * of mdio_device_register(). Use mdio_device_free() to free the device 113 * after calling this function. 114 */ 115 void mdio_device_remove(struct mdio_device *mdiodev) 116 { 117 device_del(&mdiodev->dev); 118 mdiobus_unregister_device(mdiodev); 119 } 120 EXPORT_SYMBOL(mdio_device_remove); 121 122 /** 123 * mdio_device_register_reset - Read and initialize the reset properties of 124 * an mdio device 125 * @mdiodev: mdio_device structure 126 * 127 * Return: Zero if successful, negative error code on failure 128 */ 129 int mdio_device_register_reset(struct mdio_device *mdiodev) 130 { 131 struct reset_control *reset; 132 133 /* Deassert the optional reset signal */ 134 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev, 135 "reset", GPIOD_OUT_LOW); 136 if (IS_ERR(mdiodev->reset_gpio)) 137 return PTR_ERR(mdiodev->reset_gpio); 138 139 if (mdiodev->reset_gpio) 140 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset"); 141 142 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy"); 143 if (IS_ERR(reset)) { 144 gpiod_put(mdiodev->reset_gpio); 145 mdiodev->reset_gpio = NULL; 146 return PTR_ERR(reset); 147 } 148 149 mdiodev->reset_ctrl = reset; 150 151 /* Read optional firmware properties */ 152 device_property_read_u32(&mdiodev->dev, "reset-assert-us", 153 &mdiodev->reset_assert_delay); 154 device_property_read_u32(&mdiodev->dev, "reset-deassert-us", 155 &mdiodev->reset_deassert_delay); 156 157 return 0; 158 } 159 160 /** 161 * mdio_device_unregister_reset - uninitialize the reset properties of 162 * an mdio device 163 * @mdiodev: mdio_device structure 164 */ 165 void mdio_device_unregister_reset(struct mdio_device *mdiodev) 166 { 167 gpiod_put(mdiodev->reset_gpio); 168 mdiodev->reset_gpio = NULL; 169 reset_control_put(mdiodev->reset_ctrl); 170 mdiodev->reset_ctrl = NULL; 171 mdiodev->reset_assert_delay = 0; 172 mdiodev->reset_deassert_delay = 0; 173 } 174 175 void mdio_device_reset(struct mdio_device *mdiodev, int value) 176 { 177 unsigned int d; 178 179 if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl) 180 return; 181 182 if (mdiodev->reset_state == value) 183 return; 184 185 if (mdiodev->reset_gpio) 186 gpiod_set_value_cansleep(mdiodev->reset_gpio, value); 187 188 if (mdiodev->reset_ctrl) { 189 if (value) 190 reset_control_assert(mdiodev->reset_ctrl); 191 else 192 reset_control_deassert(mdiodev->reset_ctrl); 193 } 194 195 d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay; 196 if (d) 197 fsleep(d); 198 199 mdiodev->reset_state = value; 200 } 201 EXPORT_SYMBOL(mdio_device_reset); 202 203 /** 204 * mdio_probe - probe an MDIO device 205 * @dev: device to probe 206 * 207 * Description: Take care of setting up the mdio_device structure 208 * and calling the driver to probe the device. 209 */ 210 static int mdio_probe(struct device *dev) 211 { 212 struct mdio_device *mdiodev = to_mdio_device(dev); 213 struct device_driver *drv = mdiodev->dev.driver; 214 struct mdio_driver *mdiodrv = to_mdio_driver(drv); 215 int err = 0; 216 217 /* Deassert the reset signal */ 218 mdio_device_reset(mdiodev, 0); 219 220 if (mdiodrv->probe) { 221 err = mdiodrv->probe(mdiodev); 222 if (err) { 223 /* Assert the reset signal */ 224 mdio_device_reset(mdiodev, 1); 225 } 226 } 227 228 return err; 229 } 230 231 static int mdio_remove(struct device *dev) 232 { 233 struct mdio_device *mdiodev = to_mdio_device(dev); 234 struct device_driver *drv = mdiodev->dev.driver; 235 struct mdio_driver *mdiodrv = to_mdio_driver(drv); 236 237 if (mdiodrv->remove) 238 mdiodrv->remove(mdiodev); 239 240 /* Assert the reset signal */ 241 mdio_device_reset(mdiodev, 1); 242 243 return 0; 244 } 245 246 static void mdio_shutdown(struct device *dev) 247 { 248 struct mdio_device *mdiodev = to_mdio_device(dev); 249 struct device_driver *drv = mdiodev->dev.driver; 250 struct mdio_driver *mdiodrv = to_mdio_driver(drv); 251 252 if (mdiodrv->shutdown) 253 mdiodrv->shutdown(mdiodev); 254 } 255 256 /** 257 * mdio_driver_register - register an mdio_driver with the MDIO layer 258 * @drv: new mdio_driver to register 259 */ 260 int mdio_driver_register(struct mdio_driver *drv) 261 { 262 struct mdio_driver_common *mdiodrv = &drv->mdiodrv; 263 int retval; 264 265 pr_debug("%s: %s\n", __func__, mdiodrv->driver.name); 266 267 mdiodrv->driver.bus = &mdio_bus_type; 268 mdiodrv->driver.probe = mdio_probe; 269 mdiodrv->driver.remove = mdio_remove; 270 mdiodrv->driver.shutdown = mdio_shutdown; 271 272 retval = driver_register(&mdiodrv->driver); 273 if (retval) { 274 pr_err("%s: Error %d in registering driver\n", 275 mdiodrv->driver.name, retval); 276 277 return retval; 278 } 279 280 return 0; 281 } 282 EXPORT_SYMBOL(mdio_driver_register); 283 284 void mdio_driver_unregister(struct mdio_driver *drv) 285 { 286 struct mdio_driver_common *mdiodrv = &drv->mdiodrv; 287 288 driver_unregister(&mdiodrv->driver); 289 } 290 EXPORT_SYMBOL(mdio_driver_unregister); 291