net: phy: add sysfs node for reading PHY's registers
authorroger <roger.chen@rock-chips.com>
Wed, 6 Apr 2016 10:07:56 +0000 (18:07 +0800)
committerroger <roger.chen@rock-chips.com>
Wed, 6 Apr 2016 10:20:01 +0000 (18:20 +0800)
Change-Id: I76468dd235a39b6f79699b1cc931c2c7bb7bdbc5
Signed-off-by: roger <roger.chen@rock-chips.com>
drivers/net/phy/mdio_bus.c

index 88cb4592b6fbbc6cc1ed6812b0fad2d17885a17e..c9a62692c1ea9525e4f25a385725e31a5705607f 100644 (file)
@@ -656,10 +656,57 @@ phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf
 }
 static DEVICE_ATTR_RO(phy_has_fixups);
 
+static ssize_t
+phy_registers_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct phy_device *phydev = to_phy_device(dev);
+       int index;
+
+       for (index = 0; index < 32; index++)
+               sprintf(buf, "%s%2d: 0x%x\n", buf, index,
+                       phy_read(phydev, index));
+
+       return strlen(buf);
+}
+
+static ssize_t
+phy_registers_store(struct device *dev,
+                   struct device_attribute *attr,
+                   const char *buf, size_t count)
+{
+       struct phy_device *phydev = to_phy_device(dev);
+       long long index, val;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               if (*(buf + i) == ' ')
+                       break;
+       }
+
+       if (!kstrtoll(buf, 0, &index)) {
+               pr_err("Please input like1: <reg index> <value>\n");
+               return count;
+       }
+
+       if (!kstrtoll(buf + i + 1, 0, &val)) {
+               pr_err("Please input like2: <reg index> <value>\n");
+               return count;
+       }
+
+       pr_info("Set Ethernet PHY register %d to 0x%x\n", (int)index, (int)val);
+
+       phy_write(phydev, index, val);
+
+       return count;
+}
+
+static DEVICE_ATTR_RW(phy_registers);
+
 static struct attribute *mdio_dev_attrs[] = {
        &dev_attr_phy_id.attr,
        &dev_attr_phy_interface.attr,
        &dev_attr_phy_has_fixups.attr,
+       &dev_attr_phy_registers.attr,
        NULL,
 };
 ATTRIBUTE_GROUPS(mdio_dev);