net: dsa/mv88e6352: Report chip temperature
authorGuenter Roeck <linux@roeck-us.net>
Wed, 29 Oct 2014 17:44:59 +0000 (10:44 -0700)
committerDavid S. Miller <davem@davemloft.net>
Thu, 30 Oct 2014 18:54:11 +0000 (14:54 -0400)
MV88E6352 supports reading the chip temperature from two PHY registers,
6:26 and 6:27. Report it using the more accurate register 6:27.
Also report temperature limit and alarm.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/dsa/mv88e6352.c

index f17364ff8f3fa04bf3ef67d05105b6355e5d80e5..744e6fac8023f6d97e216178d79709c2d966a014 100644 (file)
@@ -325,6 +325,101 @@ static int mv88e6352_setup_port(struct dsa_switch *ds, int p)
        return 0;
 }
 
+#ifdef CONFIG_NET_DSA_HWMON
+
+static int mv88e6352_phy_page_read(struct dsa_switch *ds,
+                                  int port, int page, int reg)
+{
+       struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
+       int ret;
+
+       mutex_lock(&ps->phy_mutex);
+       ret = __mv88e6352_phy_write(ds, port, 0x16, page);
+       if (ret < 0)
+               goto error;
+       ret = __mv88e6352_phy_read(ds, port, reg);
+error:
+       __mv88e6352_phy_write(ds, port, 0x16, 0x0);
+       mutex_unlock(&ps->phy_mutex);
+       return ret;
+}
+
+static int mv88e6352_phy_page_write(struct dsa_switch *ds,
+                                   int port, int page, int reg, int val)
+{
+       struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
+       int ret;
+
+       mutex_lock(&ps->phy_mutex);
+       ret = __mv88e6352_phy_write(ds, port, 0x16, page);
+       if (ret < 0)
+               goto error;
+
+       ret = __mv88e6352_phy_write(ds, port, reg, val);
+error:
+       __mv88e6352_phy_write(ds, port, 0x16, 0x0);
+       mutex_unlock(&ps->phy_mutex);
+       return ret;
+}
+
+static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
+{
+       int ret;
+
+       *temp = 0;
+
+       ret = mv88e6352_phy_page_read(ds, 0, 6, 27);
+       if (ret < 0)
+               return ret;
+
+       *temp = (ret & 0xff) - 25;
+
+       return 0;
+}
+
+static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
+{
+       int ret;
+
+       *temp = 0;
+
+       ret = mv88e6352_phy_page_read(ds, 0, 6, 26);
+       if (ret < 0)
+               return ret;
+
+       *temp = (((ret >> 8) & 0x1f) * 5) - 25;
+
+       return 0;
+}
+
+static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
+{
+       int ret;
+
+       ret = mv88e6352_phy_page_read(ds, 0, 6, 26);
+       if (ret < 0)
+               return ret;
+       temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
+       return mv88e6352_phy_page_write(ds, 0, 6, 26,
+                                       (ret & 0xe0ff) | (temp << 8));
+}
+
+static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
+{
+       int ret;
+
+       *alarm = false;
+
+       ret = mv88e6352_phy_page_read(ds, 0, 6, 26);
+       if (ret < 0)
+               return ret;
+
+       *alarm = !!(ret & 0x40);
+
+       return 0;
+}
+#endif /* CONFIG_NET_DSA_HWMON */
+
 static int mv88e6352_setup(struct dsa_switch *ds)
 {
        struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
@@ -461,6 +556,12 @@ struct dsa_switch_driver mv88e6352_switch_driver = {
        .get_strings            = mv88e6352_get_strings,
        .get_ethtool_stats      = mv88e6352_get_ethtool_stats,
        .get_sset_count         = mv88e6352_get_sset_count,
+#ifdef CONFIG_NET_DSA_HWMON
+       .get_temp               = mv88e6352_get_temp,
+       .get_temp_limit         = mv88e6352_get_temp_limit,
+       .set_temp_limit         = mv88e6352_set_temp_limit,
+       .get_temp_alarm         = mv88e6352_get_temp_alarm,
+#endif
 };
 
 MODULE_ALIAS("platform:mv88e6352");