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/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> 23 24 void mdio_device_free(struct mdio_device *mdiodev) 25 { 26 put_device(&mdiodev->dev); 27 } 28 EXPORT_SYMBOL(mdio_device_free); 29 30 static void mdio_device_release(struct device *dev) 31 { 32 kfree(to_mdio_device(dev)); 33 } 34 35 int mdio_device_bus_match(struct device *dev, struct device_driver *drv) 36 { 37 struct mdio_device *mdiodev = to_mdio_device(dev); 38 struct mdio_driver *mdiodrv = to_mdio_driver(drv); 39 40 if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) 41 return 0; 42 43 return strcmp(mdiodev->modalias, drv->name) == 0; 44 } 45 46 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr) 47 { 48 struct mdio_device *mdiodev; 49 50 /* We allocate the device, and initialize the default values */ 51 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL); 52 if (!mdiodev) 53 return ERR_PTR(-ENOMEM); 54 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; 60 mdiodev->bus = bus; 61 mdiodev->addr = addr; 62 63 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); 64 65 device_initialize(&mdiodev->dev); 66 67 return mdiodev; 68 } 69 EXPORT_SYMBOL(mdio_device_create); 70 71 /** 72 * mdio_device_register - Register the mdio device on the MDIO bus 73 * @mdiodev: mdio_device structure to be added to the MDIO bus 74 */ 75 int mdio_device_register(struct mdio_device *mdiodev) 76 { 77 int err; 78 79 dev_dbg(&mdiodev->dev, "mdio_device_register\n"); 80 81 err = mdiobus_register_device(mdiodev); 82 if (err) 83 return err; 84 85 err = device_add(&mdiodev->dev); 86 if (err) { 87 pr_err("MDIO %d failed to add\n", mdiodev->addr); 88 goto out; 89 } 90 91 return 0; 92 93 out: 94 mdiobus_unregister_device(mdiodev); 95 return err; 96 } 97 EXPORT_SYMBOL(mdio_device_register); 98 99 /** 100 * mdio_device_remove - Remove a previously registered mdio device from the 101 * MDIO bus 102 * @mdiodev: mdio_device structure to remove 103 * 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. 107 */ 108 void mdio_device_remove(struct mdio_device *mdiodev) 109 { 110 device_del(&mdiodev->dev); 111 mdiobus_unregister_device(mdiodev); 112 } 113 EXPORT_SYMBOL(mdio_device_remove); 114 115 void mdio_device_reset(struct mdio_device *mdiodev, int value) 116 { 117 unsigned int d; 118 119 if (!mdiodev->reset) 120 return; 121 122 gpiod_set_value(mdiodev->reset, value); 123 124 d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay; 125 if (d) 126 usleep_range(d, d + max_t(unsigned int, d / 10, 100)); 127 } 128 EXPORT_SYMBOL(mdio_device_reset); 129 130 /** 131 * mdio_probe - probe an MDIO device 132 * @dev: device to probe 133 * 134 * Description: Take care of setting up the mdio_device structure 135 * and calling the driver to probe the device. 136 */ 137 static int mdio_probe(struct device *dev) 138 { 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); 142 int err = 0; 143 144 if (mdiodrv->probe) { 145 /* Deassert the reset signal */ 146 mdio_device_reset(mdiodev, 0); 147 148 err = mdiodrv->probe(mdiodev); 149 if (err) { 150 /* Assert the reset signal */ 151 mdio_device_reset(mdiodev, 1); 152 } 153 } 154 155 return err; 156 } 157 158 static int mdio_remove(struct device *dev) 159 { 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); 163 164 if (mdiodrv->remove) { 165 mdiodrv->remove(mdiodev); 166 167 /* Assert the reset signal */ 168 mdio_device_reset(mdiodev, 1); 169 } 170 171 return 0; 172 } 173 174 /** 175 * mdio_driver_register - register an mdio_driver with the MDIO layer 176 * @new_driver: new mdio_driver to register 177 */ 178 int mdio_driver_register(struct mdio_driver *drv) 179 { 180 struct mdio_driver_common *mdiodrv = &drv->mdiodrv; 181 int retval; 182 183 pr_debug("mdio_driver_register: %s\n", mdiodrv->driver.name); 184 185 mdiodrv->driver.bus = &mdio_bus_type; 186 mdiodrv->driver.probe = mdio_probe; 187 mdiodrv->driver.remove = mdio_remove; 188 189 retval = driver_register(&mdiodrv->driver); 190 if (retval) { 191 pr_err("%s: Error %d in registering driver\n", 192 mdiodrv->driver.name, retval); 193 194 return retval; 195 } 196 197 return 0; 198 } 199 EXPORT_SYMBOL(mdio_driver_register); 200 201 void mdio_driver_unregister(struct mdio_driver *drv) 202 { 203 struct mdio_driver_common *mdiodrv = &drv->mdiodrv; 204 205 driver_unregister(&mdiodrv->driver); 206 } 207 EXPORT_SYMBOL(mdio_driver_unregister); 208