phylib: Add phy_set_max_speed helper
authorSimon Horman <horms+renesas@verge.net.au>
Wed, 30 Sep 2015 06:15:52 +0000 (15:15 +0900)
committerDavid S. Miller <davem@davemloft.net>
Sat, 3 Oct 2015 12:05:18 +0000 (05:05 -0700)
Add a helper to allow ethernet drivers to limit the speed of a phy
(that they are attached to).

This mainly involves factoring out the business-end of
of_set_phy_supported() and exporting a new symbol.

This code seems to be open coded in several places, in several different
variants.

It is is envisaged that this will be used in situations where setting the
"max-speed" property in DT is not appropriate, e.g. because the maximum
speed is not a property of the phy hardware.

Signed-off-by: Simon Horman <horms+renesas@verge.net.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/phy/phy_device.c
include/linux/phy.h

index f761288abe660b7d49b0a9a7dbd92b11efdbbbba..383389146099e3c1f5e8fbb2995570eab0fa037f 100644 (file)
@@ -1239,6 +1239,44 @@ static int gen10g_resume(struct phy_device *phydev)
        return 0;
 }
 
+static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
+{
+       /* The default values for phydev->supported are provided by the PHY
+        * driver "features" member, we want to reset to sane defaults first
+        * before supporting higher speeds.
+        */
+       phydev->supported &= PHY_DEFAULT_FEATURES;
+
+       switch (max_speed) {
+       default:
+               return -ENOTSUPP;
+       case SPEED_1000:
+               phydev->supported |= PHY_1000BT_FEATURES;
+               /* fall through */
+       case SPEED_100:
+               phydev->supported |= PHY_100BT_FEATURES;
+               /* fall through */
+       case SPEED_10:
+               phydev->supported |= PHY_10BT_FEATURES;
+       }
+
+       return 0;
+}
+
+int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
+{
+       int err;
+
+       err = __set_phy_supported(phydev, max_speed);
+       if (err)
+               return err;
+
+       phydev->advertising = phydev->supported;
+
+       return 0;
+}
+EXPORT_SYMBOL(phy_set_max_speed);
+
 static void of_set_phy_supported(struct phy_device *phydev)
 {
        struct device_node *node = phydev->dev.of_node;
@@ -1250,25 +1288,8 @@ static void of_set_phy_supported(struct phy_device *phydev)
        if (!node)
                return;
 
-       if (!of_property_read_u32(node, "max-speed", &max_speed)) {
-               /* The default values for phydev->supported are provided by the PHY
-                * driver "features" member, we want to reset to sane defaults fist
-                * before supporting higher speeds.
-                */
-               phydev->supported &= PHY_DEFAULT_FEATURES;
-
-               switch (max_speed) {
-               default:
-                       return;
-
-               case SPEED_1000:
-                       phydev->supported |= PHY_1000BT_FEATURES;
-               case SPEED_100:
-                       phydev->supported |= PHY_100BT_FEATURES;
-               case SPEED_10:
-                       phydev->supported |= PHY_10BT_FEATURES;
-               }
-       }
+       if (!of_property_read_u32(node, "max-speed", &max_speed))
+               __set_phy_supported(phydev, max_speed);
 }
 
 /**
index 4a4e3a09233762ba0e2d3065a6fa951e352a2869..4c477e6ece33356530da3d38210df2820d2be04d 100644 (file)
@@ -798,6 +798,7 @@ int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
 int phy_start_interrupts(struct phy_device *phydev);
 void phy_print_status(struct phy_device *phydev);
 void phy_device_free(struct phy_device *phydev);
+int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
 
 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
                       int (*run)(struct phy_device *));