Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 1 | /* MDIO Bus interface |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2 | * |
| 3 | * Author: Andy Fleming |
| 4 | * |
| 5 | * Copyright (c) 2004 Freescale Semiconductor, Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | */ |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 17 | #include <linux/string.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/unistd.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/delay.h> |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 24 | #include <linux/device.h> |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 25 | #include <linux/gpio.h> |
| 26 | #include <linux/gpio/consumer.h> |
David Daney | a30e2c1 | 2012-06-27 07:33:37 +0000 | [diff] [blame] | 27 | #include <linux/of_device.h> |
Mark Brown | 4085a7f | 2012-10-08 22:55:43 +0000 | [diff] [blame] | 28 | #include <linux/of_mdio.h> |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 29 | #include <linux/of_gpio.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 30 | #include <linux/netdevice.h> |
| 31 | #include <linux/etherdevice.h> |
| 32 | #include <linux/skbuff.h> |
| 33 | #include <linux/spinlock.h> |
| 34 | #include <linux/mm.h> |
| 35 | #include <linux/module.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 36 | #include <linux/mii.h> |
| 37 | #include <linux/ethtool.h> |
| 38 | #include <linux/phy.h> |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 39 | #include <linux/io.h> |
| 40 | #include <linux/uaccess.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 41 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 42 | #include <asm/irq.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 43 | |
Uwe Kleine-König | e22e996 | 2016-11-22 16:47:11 +0100 | [diff] [blame] | 44 | #define CREATE_TRACE_POINTS |
| 45 | #include <trace/events/mdio.h> |
| 46 | |
Florian Fainelli | 648ea01 | 2017-02-04 13:02:44 -0800 | [diff] [blame] | 47 | #include "mdio-boardinfo.h" |
| 48 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 49 | int mdiobus_register_device(struct mdio_device *mdiodev) |
| 50 | { |
| 51 | if (mdiodev->bus->mdio_map[mdiodev->addr]) |
| 52 | return -EBUSY; |
| 53 | |
| 54 | mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | EXPORT_SYMBOL(mdiobus_register_device); |
| 59 | |
| 60 | int mdiobus_unregister_device(struct mdio_device *mdiodev) |
| 61 | { |
| 62 | if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | mdiodev->bus->mdio_map[mdiodev->addr] = NULL; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | EXPORT_SYMBOL(mdiobus_unregister_device); |
| 70 | |
| 71 | struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr) |
| 72 | { |
| 73 | struct mdio_device *mdiodev = bus->mdio_map[addr]; |
| 74 | |
| 75 | if (!mdiodev) |
| 76 | return NULL; |
| 77 | |
| 78 | if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY)) |
| 79 | return NULL; |
| 80 | |
| 81 | return container_of(mdiodev, struct phy_device, mdio); |
| 82 | } |
| 83 | EXPORT_SYMBOL(mdiobus_get_phy); |
| 84 | |
| 85 | bool mdiobus_is_registered_device(struct mii_bus *bus, int addr) |
| 86 | { |
| 87 | return bus->mdio_map[addr]; |
| 88 | } |
| 89 | EXPORT_SYMBOL(mdiobus_is_registered_device); |
| 90 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 91 | /** |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 92 | * mdiobus_alloc_size - allocate a mii_bus structure |
Randy Dunlap | af58f1d | 2012-01-21 09:03:04 +0000 | [diff] [blame] | 93 | * @size: extra amount of memory to allocate for private storage. |
| 94 | * If non-zero, then bus->priv is points to that memory. |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 95 | * |
| 96 | * Description: called by a bus driver to allocate an mii_bus |
| 97 | * structure to fill in. |
| 98 | */ |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 99 | struct mii_bus *mdiobus_alloc_size(size_t size) |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 100 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 101 | struct mii_bus *bus; |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 102 | size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); |
| 103 | size_t alloc_size; |
Andrew Lunn | e7f4dc3 | 2016-01-06 20:11:15 +0100 | [diff] [blame] | 104 | int i; |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 105 | |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 106 | /* If we alloc extra space, it should be aligned */ |
| 107 | if (size) |
| 108 | alloc_size = aligned_size + size; |
| 109 | else |
| 110 | alloc_size = sizeof(*bus); |
| 111 | |
| 112 | bus = kzalloc(alloc_size, GFP_KERNEL); |
Dan Carpenter | db9107b | 2016-01-12 12:34:36 +0300 | [diff] [blame] | 113 | if (!bus) |
| 114 | return NULL; |
| 115 | |
| 116 | bus->state = MDIOBUS_ALLOCATED; |
| 117 | if (size) |
| 118 | bus->priv = (void *)bus + aligned_size; |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 119 | |
Andrew Lunn | e7f4dc3 | 2016-01-06 20:11:15 +0100 | [diff] [blame] | 120 | /* Initialise the interrupts to polling */ |
| 121 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 122 | bus->irq[i] = PHY_POLL; |
| 123 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 124 | return bus; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 125 | } |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 126 | EXPORT_SYMBOL(mdiobus_alloc_size); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 127 | |
Grygorii Strashko | 6d48f44 | 2014-04-30 15:23:33 +0300 | [diff] [blame] | 128 | static void _devm_mdiobus_free(struct device *dev, void *res) |
| 129 | { |
| 130 | mdiobus_free(*(struct mii_bus **)res); |
| 131 | } |
| 132 | |
| 133 | static int devm_mdiobus_match(struct device *dev, void *res, void *data) |
| 134 | { |
| 135 | struct mii_bus **r = res; |
| 136 | |
| 137 | if (WARN_ON(!r || !*r)) |
| 138 | return 0; |
| 139 | |
| 140 | return *r == data; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size() |
| 145 | * @dev: Device to allocate mii_bus for |
| 146 | * @sizeof_priv: Space to allocate for private structure. |
| 147 | * |
| 148 | * Managed mdiobus_alloc_size. mii_bus allocated with this function is |
| 149 | * automatically freed on driver detach. |
| 150 | * |
| 151 | * If an mii_bus allocated with this function needs to be freed separately, |
| 152 | * devm_mdiobus_free() must be used. |
| 153 | * |
| 154 | * RETURNS: |
| 155 | * Pointer to allocated mii_bus on success, NULL on failure. |
| 156 | */ |
| 157 | struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv) |
| 158 | { |
| 159 | struct mii_bus **ptr, *bus; |
| 160 | |
| 161 | ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL); |
| 162 | if (!ptr) |
| 163 | return NULL; |
| 164 | |
| 165 | /* use raw alloc_dr for kmalloc caller tracing */ |
| 166 | bus = mdiobus_alloc_size(sizeof_priv); |
| 167 | if (bus) { |
| 168 | *ptr = bus; |
| 169 | devres_add(dev, ptr); |
| 170 | } else { |
| 171 | devres_free(ptr); |
| 172 | } |
| 173 | |
| 174 | return bus; |
| 175 | } |
Arnd Bergmann | 93dccc5 | 2014-05-08 16:46:52 +0200 | [diff] [blame] | 176 | EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size); |
Grygorii Strashko | 6d48f44 | 2014-04-30 15:23:33 +0300 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * devm_mdiobus_free - Resource-managed mdiobus_free() |
| 180 | * @dev: Device this mii_bus belongs to |
| 181 | * @bus: the mii_bus associated with the device |
| 182 | * |
| 183 | * Free mii_bus allocated with devm_mdiobus_alloc_size(). |
| 184 | */ |
| 185 | void devm_mdiobus_free(struct device *dev, struct mii_bus *bus) |
| 186 | { |
| 187 | int rc; |
| 188 | |
| 189 | rc = devres_release(dev, _devm_mdiobus_free, |
| 190 | devm_mdiobus_match, bus); |
| 191 | WARN_ON(rc); |
| 192 | } |
| 193 | EXPORT_SYMBOL_GPL(devm_mdiobus_free); |
| 194 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 195 | /** |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 196 | * mdiobus_release - mii_bus device release callback |
Randy Dunlap | 78c36b1 | 2008-10-13 18:46:22 -0700 | [diff] [blame] | 197 | * @d: the target struct device that contains the mii_bus |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 198 | * |
| 199 | * Description: called when the last reference to an mii_bus is |
| 200 | * dropped, to free the underlying memory. |
| 201 | */ |
| 202 | static void mdiobus_release(struct device *d) |
| 203 | { |
| 204 | struct mii_bus *bus = to_mii_bus(d); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 205 | BUG_ON(bus->state != MDIOBUS_RELEASED && |
| 206 | /* for compatibility with error handling in drivers */ |
| 207 | bus->state != MDIOBUS_ALLOCATED); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 208 | kfree(bus); |
| 209 | } |
| 210 | |
| 211 | static struct class mdio_bus_class = { |
| 212 | .name = "mdio_bus", |
| 213 | .dev_release = mdiobus_release, |
| 214 | }; |
| 215 | |
Bjørn Mork | b943fbb | 2012-05-11 05:47:01 +0000 | [diff] [blame] | 216 | #if IS_ENABLED(CONFIG_OF_MDIO) |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 217 | /* Helper function for of_mdio_find_bus */ |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 218 | static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np) |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 219 | { |
| 220 | return dev->of_node == mdio_bus_np; |
| 221 | } |
| 222 | /** |
| 223 | * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. |
Randy Dunlap | f41ef2e | 2012-06-08 14:07:19 +0000 | [diff] [blame] | 224 | * @mdio_bus_np: Pointer to the mii_bus. |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 225 | * |
Russell King | a136442 | 2015-09-24 20:35:52 +0100 | [diff] [blame] | 226 | * Returns a reference to the mii_bus, or NULL if none found. The |
| 227 | * embedded struct device will have its reference count incremented, |
| 228 | * and this must be put once the bus is finished with. |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 229 | * |
| 230 | * Because the association of a device_node and mii_bus is made via |
| 231 | * of_mdiobus_register(), the mii_bus cannot be found before it is |
| 232 | * registered with of_mdiobus_register(). |
| 233 | * |
| 234 | */ |
| 235 | struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) |
| 236 | { |
| 237 | struct device *d; |
| 238 | |
| 239 | if (!mdio_bus_np) |
| 240 | return NULL; |
| 241 | |
| 242 | d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np, |
| 243 | of_mdio_bus_match); |
| 244 | |
| 245 | return d ? to_mii_bus(d) : NULL; |
| 246 | } |
| 247 | EXPORT_SYMBOL(of_mdio_find_bus); |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 248 | |
Andrew Lunn | f03bc4a | 2016-01-06 20:11:24 +0100 | [diff] [blame] | 249 | /* Walk the list of subnodes of a mdio bus and look for a node that |
| 250 | * matches the mdio device's address with its 'reg' property. If |
| 251 | * found, set the of_node pointer for the mdio device. This allows |
| 252 | * auto-probed phy devices to be supplied with information passed in |
| 253 | * via DT. |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 254 | */ |
Andrew Lunn | f03bc4a | 2016-01-06 20:11:24 +0100 | [diff] [blame] | 255 | static void of_mdiobus_link_mdiodev(struct mii_bus *bus, |
| 256 | struct mdio_device *mdiodev) |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 257 | { |
Andrew Lunn | f03bc4a | 2016-01-06 20:11:24 +0100 | [diff] [blame] | 258 | struct device *dev = &mdiodev->dev; |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 259 | struct device_node *child; |
| 260 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 261 | if (dev->of_node || !bus->dev.of_node) |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 262 | return; |
| 263 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 264 | for_each_available_child_of_node(bus->dev.of_node, child) { |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 265 | int addr; |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 266 | |
Jon Mason | d0a65400 | 2017-05-31 15:43:30 -0400 | [diff] [blame] | 267 | addr = of_mdio_parse_addr(dev, child); |
| 268 | if (addr < 0) |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 269 | continue; |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 270 | |
Andrew Lunn | f03bc4a | 2016-01-06 20:11:24 +0100 | [diff] [blame] | 271 | if (addr == mdiodev->addr) { |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 272 | dev->of_node = child; |
| 273 | return; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ |
Andrew Lunn | f03bc4a | 2016-01-06 20:11:24 +0100 | [diff] [blame] | 278 | static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio, |
| 279 | struct mdio_device *mdiodev) |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 280 | { |
| 281 | } |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 282 | #endif |
| 283 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 284 | /** |
Florian Fainelli | d0281a5 | 2017-03-28 12:57:09 -0700 | [diff] [blame] | 285 | * mdiobus_create_device_from_board_info - create a full MDIO device given |
| 286 | * a mdio_board_info structure |
| 287 | * @bus: MDIO bus to create the devices on |
| 288 | * @bi: mdio_board_info structure describing the devices |
| 289 | * |
| 290 | * Returns 0 on success or < 0 on error. |
| 291 | */ |
| 292 | static int mdiobus_create_device(struct mii_bus *bus, |
| 293 | struct mdio_board_info *bi) |
| 294 | { |
| 295 | struct mdio_device *mdiodev; |
| 296 | int ret = 0; |
| 297 | |
| 298 | mdiodev = mdio_device_create(bus, bi->mdio_addr); |
| 299 | if (IS_ERR(mdiodev)) |
| 300 | return -ENODEV; |
| 301 | |
| 302 | strncpy(mdiodev->modalias, bi->modalias, |
| 303 | sizeof(mdiodev->modalias)); |
| 304 | mdiodev->bus_match = mdio_device_bus_match; |
| 305 | mdiodev->dev.platform_data = (void *)bi->platform_data; |
| 306 | |
| 307 | ret = mdio_device_register(mdiodev); |
| 308 | if (ret) |
| 309 | mdio_device_free(mdiodev); |
| 310 | |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | /** |
Russell King | 59f0697 | 2015-09-25 11:56:56 +0100 | [diff] [blame] | 315 | * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 316 | * @bus: target mii_bus |
Russell King | 59f0697 | 2015-09-25 11:56:56 +0100 | [diff] [blame] | 317 | * @owner: module containing bus accessor functions |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 318 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 319 | * Description: Called by a bus driver to bring up all the PHYs |
Russell King | 59f0697 | 2015-09-25 11:56:56 +0100 | [diff] [blame] | 320 | * on a given bus, and attach them to the bus. Drivers should use |
| 321 | * mdiobus_register() rather than __mdiobus_register() unless they |
Andrew Lunn | f89df3f | 2016-01-06 20:11:25 +0100 | [diff] [blame] | 322 | * need to pass a specific owner module. MDIO devices which are not |
| 323 | * PHYs will not be brought up by this function. They are expected to |
| 324 | * to be explicitly listed in DT and instantiated by of_mdiobus_register(). |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 325 | * |
| 326 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 327 | */ |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 328 | int __mdiobus_register(struct mii_bus *bus, struct module *owner) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 329 | { |
Andrew Lunn | 711fdba | 2016-01-06 20:11:27 +0100 | [diff] [blame] | 330 | struct mdio_device *mdiodev; |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 331 | int i, err; |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 332 | struct gpio_desc *gpiod; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 333 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 334 | if (NULL == bus || NULL == bus->name || |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 335 | NULL == bus->read || NULL == bus->write) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 336 | return -EINVAL; |
| 337 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 338 | BUG_ON(bus->state != MDIOBUS_ALLOCATED && |
| 339 | bus->state != MDIOBUS_UNREGISTERED); |
| 340 | |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 341 | bus->owner = owner; |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 342 | bus->dev.parent = bus->parent; |
| 343 | bus->dev.class = &mdio_bus_class; |
| 344 | bus->dev.groups = NULL; |
Stephen Hemminger | 036b668 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 345 | dev_set_name(&bus->dev, "%s", bus->id); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 346 | |
| 347 | err = device_register(&bus->dev); |
| 348 | if (err) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 349 | pr_err("mii_bus %s failed to register\n", bus->id); |
Levente Kurusa | 0c692d0 | 2014-01-30 15:45:46 -0800 | [diff] [blame] | 350 | put_device(&bus->dev); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 351 | return -EINVAL; |
| 352 | } |
| 353 | |
Adrian Bunk | d1e7fe4 | 2008-02-20 02:13:53 +0200 | [diff] [blame] | 354 | mutex_init(&bus->mdio_lock); |
| 355 | |
Sergei Shtylyov | d396e84 | 2017-06-12 23:55:38 +0300 | [diff] [blame^] | 356 | /* de-assert bus level PHY GPIO reset */ |
| 357 | gpiod = devm_gpiod_get(&bus->dev, "reset", GPIOD_OUT_LOW); |
| 358 | if (IS_ERR(gpiod)) { |
| 359 | err = PTR_ERR(gpiod); |
| 360 | if (err != -ENOENT) { |
| 361 | dev_err(&bus->dev, |
| 362 | "mii_bus %s couldn't get reset GPIO\n", |
| 363 | bus->id); |
| 364 | return err; |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 365 | } |
Sergei Shtylyov | d396e84 | 2017-06-12 23:55:38 +0300 | [diff] [blame^] | 366 | } else { |
| 367 | bus->reset_gpiod = gpiod; |
| 368 | |
| 369 | gpiod_set_value_cansleep(gpiod, 1); |
| 370 | udelay(bus->reset_delay_us); |
| 371 | gpiod_set_value_cansleep(gpiod, 0); |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 372 | } |
| 373 | |
Florian Fainelli | df0c8d9 | 2017-05-11 11:24:16 -0700 | [diff] [blame] | 374 | if (bus->reset) |
| 375 | bus->reset(bus); |
| 376 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 377 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 378 | if ((bus->phy_mask & (1 << i)) == 0) { |
| 379 | struct phy_device *phydev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 380 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 381 | phydev = mdiobus_scan(bus, i); |
Marek Vasut | 70e927b | 2016-05-02 02:47:31 +0200 | [diff] [blame] | 382 | if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 383 | err = PTR_ERR(phydev); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 384 | goto error; |
| 385 | } |
Herbert Valerio Riedel | 64b1c2b | 2006-05-10 12:12:57 -0400 | [diff] [blame] | 386 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Florian Fainelli | d0281a5 | 2017-03-28 12:57:09 -0700 | [diff] [blame] | 389 | mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device); |
Florian Fainelli | 648ea01 | 2017-02-04 13:02:44 -0800 | [diff] [blame] | 390 | |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 391 | bus->state = MDIOBUS_REGISTERED; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 392 | pr_info("%s: probed\n", bus->name); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 393 | return 0; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 394 | |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 395 | error: |
| 396 | while (--i >= 0) { |
Andrew Lunn | 711fdba | 2016-01-06 20:11:27 +0100 | [diff] [blame] | 397 | mdiodev = bus->mdio_map[i]; |
| 398 | if (!mdiodev) |
| 399 | continue; |
| 400 | |
| 401 | mdiodev->device_remove(mdiodev); |
| 402 | mdiodev->device_free(mdiodev); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 403 | } |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 404 | |
| 405 | /* Put PHYs in RESET to save power */ |
Sergei Shtylyov | d396e84 | 2017-06-12 23:55:38 +0300 | [diff] [blame^] | 406 | if (bus->reset_gpiod) |
| 407 | gpiod_set_value_cansleep(bus->reset_gpiod, 1); |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 408 | |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 409 | device_del(&bus->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 410 | return err; |
| 411 | } |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 412 | EXPORT_SYMBOL(__mdiobus_register); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 413 | |
| 414 | void mdiobus_unregister(struct mii_bus *bus) |
| 415 | { |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 416 | struct mdio_device *mdiodev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 417 | int i; |
| 418 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 419 | BUG_ON(bus->state != MDIOBUS_REGISTERED); |
| 420 | bus->state = MDIOBUS_UNREGISTERED; |
| 421 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 422 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 423 | mdiodev = bus->mdio_map[i]; |
| 424 | if (!mdiodev) |
| 425 | continue; |
| 426 | |
Andrew Lunn | 711fdba | 2016-01-06 20:11:27 +0100 | [diff] [blame] | 427 | mdiodev->device_remove(mdiodev); |
| 428 | mdiodev->device_free(mdiodev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 429 | } |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 430 | |
| 431 | /* Put PHYs in RESET to save power */ |
Sergei Shtylyov | d396e84 | 2017-06-12 23:55:38 +0300 | [diff] [blame^] | 432 | if (bus->reset_gpiod) |
| 433 | gpiod_set_value_cansleep(bus->reset_gpiod, 1); |
Roger Quadros | 6922689 | 2017-04-21 16:15:38 +0300 | [diff] [blame] | 434 | |
Mark Salter | b6c6aed | 2015-09-01 09:36:05 -0400 | [diff] [blame] | 435 | device_del(&bus->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 436 | } |
| 437 | EXPORT_SYMBOL(mdiobus_unregister); |
| 438 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 439 | /** |
| 440 | * mdiobus_free - free a struct mii_bus |
| 441 | * @bus: mii_bus to free |
| 442 | * |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 443 | * This function releases the reference to the underlying device |
| 444 | * object in the mii_bus. If this is the last reference, the mii_bus |
| 445 | * will be freed. |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 446 | */ |
| 447 | void mdiobus_free(struct mii_bus *bus) |
| 448 | { |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 449 | /* For compatibility with error handling in drivers. */ |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 450 | if (bus->state == MDIOBUS_ALLOCATED) { |
| 451 | kfree(bus); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | BUG_ON(bus->state != MDIOBUS_UNREGISTERED); |
| 456 | bus->state = MDIOBUS_RELEASED; |
| 457 | |
| 458 | put_device(&bus->dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 459 | } |
| 460 | EXPORT_SYMBOL(mdiobus_free); |
| 461 | |
Andrew Lunn | f89df3f | 2016-01-06 20:11:25 +0100 | [diff] [blame] | 462 | /** |
| 463 | * mdiobus_scan - scan a bus for MDIO devices. |
| 464 | * @bus: mii_bus to scan |
| 465 | * @addr: address on bus to scan |
| 466 | * |
| 467 | * This function scans the MDIO bus, looking for devices which can be |
| 468 | * identified using a vendor/product ID in registers 2 and 3. Not all |
| 469 | * MDIO devices have such registers, but PHY devices typically |
| 470 | * do. Hence this function assumes anything found is a PHY, or can be |
| 471 | * treated as a PHY. Other MDIO devices, such as switches, will |
| 472 | * probably not be found during the scan. |
| 473 | */ |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 474 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
| 475 | { |
| 476 | struct phy_device *phydev; |
| 477 | int err; |
| 478 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 479 | phydev = get_phy_device(bus, addr, false); |
Sergei Shtylyov | 66c239e | 2016-04-24 20:30:53 +0300 | [diff] [blame] | 480 | if (IS_ERR(phydev)) |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 481 | return phydev; |
| 482 | |
Daniel Mack | 86f6cf4 | 2014-05-24 09:34:26 +0200 | [diff] [blame] | 483 | /* |
| 484 | * For DT, see if the auto-probed phy has a correspoding child |
| 485 | * in the bus node, and set the of_node pointer in this case. |
| 486 | */ |
Andrew Lunn | f03bc4a | 2016-01-06 20:11:24 +0100 | [diff] [blame] | 487 | of_mdiobus_link_mdiodev(bus, &phydev->mdio); |
Daniel Mack | 86f6cf4 | 2014-05-24 09:34:26 +0200 | [diff] [blame] | 488 | |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 489 | err = phy_device_register(phydev); |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 490 | if (err) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 491 | phy_device_free(phydev); |
Sergei Shtylyov | e98a3aa | 2016-05-03 23:14:41 +0300 | [diff] [blame] | 492 | return ERR_PTR(-ENODEV); |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 495 | return phydev; |
| 496 | } |
| 497 | EXPORT_SYMBOL(mdiobus_scan); |
| 498 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 499 | /** |
Neil Armstrong | 21dd19f | 2015-10-22 10:37:49 +0200 | [diff] [blame] | 500 | * mdiobus_read_nested - Nested version of the mdiobus_read function |
| 501 | * @bus: the mii_bus struct |
| 502 | * @addr: the phy address |
| 503 | * @regnum: register number to read |
| 504 | * |
| 505 | * In case of nested MDIO bus access avoid lockdep false positives by |
| 506 | * using mutex_lock_nested(). |
| 507 | * |
| 508 | * NOTE: MUST NOT be called from interrupt context, |
| 509 | * because the bus read/write functions may wait for an interrupt |
| 510 | * to conclude the operation. |
| 511 | */ |
| 512 | int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum) |
| 513 | { |
| 514 | int retval; |
| 515 | |
| 516 | BUG_ON(in_interrupt()); |
| 517 | |
Andrew Lunn | 9a6f2b0 | 2016-04-11 21:40:05 +0200 | [diff] [blame] | 518 | mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); |
Neil Armstrong | 21dd19f | 2015-10-22 10:37:49 +0200 | [diff] [blame] | 519 | retval = bus->read(bus, addr, regnum); |
| 520 | mutex_unlock(&bus->mdio_lock); |
| 521 | |
Uwe Kleine-König | e22e996 | 2016-11-22 16:47:11 +0100 | [diff] [blame] | 522 | trace_mdio_access(bus, 1, addr, regnum, retval, retval); |
| 523 | |
Neil Armstrong | 21dd19f | 2015-10-22 10:37:49 +0200 | [diff] [blame] | 524 | return retval; |
| 525 | } |
| 526 | EXPORT_SYMBOL(mdiobus_read_nested); |
| 527 | |
| 528 | /** |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 529 | * mdiobus_read - Convenience function for reading a given MII mgmt register |
| 530 | * @bus: the mii_bus struct |
| 531 | * @addr: the phy address |
| 532 | * @regnum: register number to read |
| 533 | * |
| 534 | * NOTE: MUST NOT be called from interrupt context, |
| 535 | * because the bus read/write functions may wait for an interrupt |
| 536 | * to conclude the operation. |
| 537 | */ |
Jason Gunthorpe | abf35df | 2010-03-09 09:17:42 +0000 | [diff] [blame] | 538 | int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 539 | { |
| 540 | int retval; |
| 541 | |
| 542 | BUG_ON(in_interrupt()); |
| 543 | |
| 544 | mutex_lock(&bus->mdio_lock); |
| 545 | retval = bus->read(bus, addr, regnum); |
| 546 | mutex_unlock(&bus->mdio_lock); |
| 547 | |
Uwe Kleine-König | e22e996 | 2016-11-22 16:47:11 +0100 | [diff] [blame] | 548 | trace_mdio_access(bus, 1, addr, regnum, retval, retval); |
| 549 | |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 550 | return retval; |
| 551 | } |
| 552 | EXPORT_SYMBOL(mdiobus_read); |
| 553 | |
| 554 | /** |
Neil Armstrong | 21dd19f | 2015-10-22 10:37:49 +0200 | [diff] [blame] | 555 | * mdiobus_write_nested - Nested version of the mdiobus_write function |
| 556 | * @bus: the mii_bus struct |
| 557 | * @addr: the phy address |
| 558 | * @regnum: register number to write |
| 559 | * @val: value to write to @regnum |
| 560 | * |
| 561 | * In case of nested MDIO bus access avoid lockdep false positives by |
| 562 | * using mutex_lock_nested(). |
| 563 | * |
| 564 | * NOTE: MUST NOT be called from interrupt context, |
| 565 | * because the bus read/write functions may wait for an interrupt |
| 566 | * to conclude the operation. |
| 567 | */ |
| 568 | int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val) |
| 569 | { |
| 570 | int err; |
| 571 | |
| 572 | BUG_ON(in_interrupt()); |
| 573 | |
Andrew Lunn | 9a6f2b0 | 2016-04-11 21:40:05 +0200 | [diff] [blame] | 574 | mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); |
Neil Armstrong | 21dd19f | 2015-10-22 10:37:49 +0200 | [diff] [blame] | 575 | err = bus->write(bus, addr, regnum, val); |
| 576 | mutex_unlock(&bus->mdio_lock); |
| 577 | |
Uwe Kleine-König | e22e996 | 2016-11-22 16:47:11 +0100 | [diff] [blame] | 578 | trace_mdio_access(bus, 0, addr, regnum, val, err); |
| 579 | |
Neil Armstrong | 21dd19f | 2015-10-22 10:37:49 +0200 | [diff] [blame] | 580 | return err; |
| 581 | } |
| 582 | EXPORT_SYMBOL(mdiobus_write_nested); |
| 583 | |
| 584 | /** |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 585 | * mdiobus_write - Convenience function for writing a given MII mgmt register |
| 586 | * @bus: the mii_bus struct |
| 587 | * @addr: the phy address |
| 588 | * @regnum: register number to write |
| 589 | * @val: value to write to @regnum |
| 590 | * |
| 591 | * NOTE: MUST NOT be called from interrupt context, |
| 592 | * because the bus read/write functions may wait for an interrupt |
| 593 | * to conclude the operation. |
| 594 | */ |
Jason Gunthorpe | abf35df | 2010-03-09 09:17:42 +0000 | [diff] [blame] | 595 | int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 596 | { |
| 597 | int err; |
| 598 | |
| 599 | BUG_ON(in_interrupt()); |
| 600 | |
| 601 | mutex_lock(&bus->mdio_lock); |
| 602 | err = bus->write(bus, addr, regnum, val); |
| 603 | mutex_unlock(&bus->mdio_lock); |
| 604 | |
Uwe Kleine-König | e22e996 | 2016-11-22 16:47:11 +0100 | [diff] [blame] | 605 | trace_mdio_access(bus, 0, addr, regnum, val, err); |
| 606 | |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 607 | return err; |
| 608 | } |
| 609 | EXPORT_SYMBOL(mdiobus_write); |
| 610 | |
| 611 | /** |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 612 | * mdio_bus_match - determine if given MDIO driver supports the given |
| 613 | * MDIO device |
| 614 | * @dev: target MDIO device |
| 615 | * @drv: given MDIO driver |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 616 | * |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 617 | * Description: Given a MDIO device, and a MDIO driver, return 1 if |
| 618 | * the driver supports the device. Otherwise, return 0. This may |
| 619 | * require calling the devices own match function, since different classes |
| 620 | * of MDIO devices have different match criteria. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 621 | */ |
| 622 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) |
| 623 | { |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 624 | struct mdio_device *mdio = to_mdio_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 625 | |
David Daney | a30e2c1 | 2012-06-27 07:33:37 +0000 | [diff] [blame] | 626 | if (of_driver_match_device(dev, drv)) |
| 627 | return 1; |
| 628 | |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 629 | if (mdio->bus_match) |
| 630 | return mdio->bus_match(dev, drv); |
David Daney | a30e2c1 | 2012-06-27 07:33:37 +0000 | [diff] [blame] | 631 | |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 632 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 633 | } |
| 634 | |
Russell King | 1b8f869 | 2017-05-30 21:38:18 +0100 | [diff] [blame] | 635 | static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 636 | { |
| 637 | int rc; |
| 638 | |
| 639 | /* Some devices have extra OF data and an OF-style MODALIAS */ |
| 640 | rc = of_device_uevent_modalias(dev, env); |
| 641 | if (rc != -ENODEV) |
| 642 | return rc; |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 647 | #ifdef CONFIG_PM |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 648 | static int mdio_bus_suspend(struct device *dev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 649 | { |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 650 | struct mdio_device *mdio = to_mdio_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 651 | |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 652 | if (mdio->pm_ops && mdio->pm_ops->suspend) |
| 653 | return mdio->pm_ops->suspend(dev); |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 654 | |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 655 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 658 | static int mdio_bus_resume(struct device *dev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 659 | { |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 660 | struct mdio_device *mdio = to_mdio_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 661 | |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 662 | if (mdio->pm_ops && mdio->pm_ops->resume) |
| 663 | return mdio->pm_ops->resume(dev); |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 664 | |
| 665 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 666 | } |
| 667 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 668 | static int mdio_bus_restore(struct device *dev) |
| 669 | { |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 670 | struct mdio_device *mdio = to_mdio_device(dev); |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 671 | |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 672 | if (mdio->pm_ops && mdio->pm_ops->restore) |
| 673 | return mdio->pm_ops->restore(dev); |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 678 | static const struct dev_pm_ops mdio_bus_pm_ops = { |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 679 | .suspend = mdio_bus_suspend, |
| 680 | .resume = mdio_bus_resume, |
| 681 | .freeze = mdio_bus_suspend, |
| 682 | .thaw = mdio_bus_resume, |
| 683 | .restore = mdio_bus_restore, |
| 684 | }; |
| 685 | |
| 686 | #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops) |
| 687 | |
| 688 | #else |
| 689 | |
| 690 | #define MDIO_BUS_PM_OPS NULL |
| 691 | |
| 692 | #endif /* CONFIG_PM */ |
| 693 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 694 | struct bus_type mdio_bus_type = { |
| 695 | .name = "mdio_bus", |
| 696 | .match = mdio_bus_match, |
Russell King | 1b8f869 | 2017-05-30 21:38:18 +0100 | [diff] [blame] | 697 | .uevent = mdio_uevent, |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 698 | .pm = MDIO_BUS_PM_OPS, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 699 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 700 | EXPORT_SYMBOL(mdio_bus_type); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 701 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 702 | int __init mdio_bus_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 703 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 704 | int ret; |
| 705 | |
| 706 | ret = class_register(&mdio_bus_class); |
| 707 | if (!ret) { |
| 708 | ret = bus_register(&mdio_bus_type); |
| 709 | if (ret) |
| 710 | class_unregister(&mdio_bus_class); |
| 711 | } |
| 712 | |
| 713 | return ret; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 714 | } |
Florian Fainelli | 90eff90 | 2017-03-23 10:01:19 -0700 | [diff] [blame] | 715 | EXPORT_SYMBOL_GPL(mdio_bus_init); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 716 | |
Florian Fainelli | 90eff90 | 2017-03-23 10:01:19 -0700 | [diff] [blame] | 717 | #if IS_ENABLED(CONFIG_PHYLIB) |
Peter Chubb | dc85dec | 2005-09-03 14:05:06 -0700 | [diff] [blame] | 718 | void mdio_bus_exit(void) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 719 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 720 | class_unregister(&mdio_bus_class); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 721 | bus_unregister(&mdio_bus_type); |
| 722 | } |
Florian Fainelli | 90eff90 | 2017-03-23 10:01:19 -0700 | [diff] [blame] | 723 | EXPORT_SYMBOL_GPL(mdio_bus_exit); |
| 724 | #else |
| 725 | module_init(mdio_bus_init); |
| 726 | /* no module_exit, intentional */ |
| 727 | MODULE_LICENSE("GPL"); |
| 728 | MODULE_DESCRIPTION("MDIO bus/device layer"); |
| 729 | #endif |