Skip to contentSkip to conversation

wifi: mt76: mt7921: Support temp sensor.

PATCH Superseded [email protected] 3 participants 5 messages May 18, 2023 – May 19, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: mt76: mt7921: Support temp sensor.

From: Ben Greear <[email protected]>

Allow sensors tool to read radio's temperature, example:

mt7921_phy17-pci-1800
Adapter: PCI adapter
temp1: +72.0°C

---
 .../net/wireless/mediatek/mt76/mt7921/init.c  | 53 +++++++++++++++++++
 .../net/wireless/mediatek/mt76/mt7921/mcu.c   | 17 ++++++
 .../wireless/mediatek/mt76/mt7921/mt7921.h    |  1 +
 3 files changed, 71 insertions(+)
drivers/net/wireless/mediatek/mt76/mt7921/init.c +53 -0
index c15ce1a19000..18f0f2dfbbcf 100644
@@ -2,6 +2,9 @@
 /* Copyright (C) 2020 MediaTek Inc. */
 
 #include <linux/etherdevice.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/thermal.h>
 #include <linux/firmware.h>
 #include "mt7921.h"
 #include "../mt76_connac2_mac.h"
@@ -58,6 +61,50 @@ static const struct ieee80211_iface_combination if_comb_chanctx[] = {
 	}
 };
 
+static ssize_t mt7921_thermal_temp_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct mt7921_phy *phy = dev_get_drvdata(dev);
+	int i = to_sensor_dev_attr(attr)->index;
+	int temperature;
+
+	switch (i) {
+	case 0:
+		temperature = mt7921_mcu_get_temperature(phy);
+		if (temperature < 0)
+			return temperature;
+		/* display in millidegree celcius */
+		return sprintf(buf, "%u\n", temperature * 1000);
+	default:
+		return -EINVAL;
+	}
+}
+static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0);
+
+static struct attribute *mt7921_hwmon_attrs[] = {
+	&sensor_dev_attr_temp1_input.dev_attr.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(mt7921_hwmon);
+
+static int mt7921_thermal_init(struct mt7921_phy *phy)
+{
+	struct wiphy *wiphy = phy->mt76->hw->wiphy;
+	struct device *hwmon;
+	const char *name;
+
+	name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7921_%s",
+			      wiphy_name(wiphy));
+
+	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
+						       mt7921_hwmon_groups);
+	if (IS_ERR(hwmon))
+		return PTR_ERR(hwmon);
+
+	return 0;
+}
+
 static void
 mt7921_regd_notifier(struct wiphy *wiphy,
 		     struct regulatory_request *request)
@@ -384,6 +431,12 @@ static void mt7921_init_work(struct work_struct *work)
 		return;
 	}
 
+	ret = mt7921_thermal_init(&dev->phy);
+	if (ret) {
+		dev_err(dev->mt76.dev, "thermal_init failed\n");
+		return;
+	}
+
 	/* we support chip reset now */
 	dev->hw_init_done = true;
 
drivers/net/wireless/mediatek/mt76/mt7921/mcu.c +17 -0
index 9c4dcc0e5a7c..abeedacc28f2 100644
@@ -1346,6 +1346,23 @@ int mt7921_mcu_set_clc(struct mt7921_dev *dev, u8 *alpha2,
 	return 0;
 }
 
+int mt7921_mcu_get_temperature(struct mt7921_phy *phy)
+{
+	struct mt7921_dev *dev = phy->dev;
+	struct {
+		u8 ctrl_id;
+		u8 action;
+		u8 band_idx;
+		u8 rsv[5];
+	} req = {
+		.ctrl_id = THERMAL_SENSOR_TEMP_QUERY,
+		.band_idx = phy->mt76->band_idx,
+	};
+
+	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_CTRL), &req,
+				 sizeof(req), true);
+}
+
 int mt7921_mcu_set_rxfilter(struct mt7921_dev *dev, u32 fif,
 			    u8 bit_op, u32 bit_map)
 {
drivers/net/wireless/mediatek/mt76/mt7921/mt7921.h +1 -0
index 706f00df6836..85fddf99d497 100644
@@ -568,6 +568,7 @@ int mt7921_mcu_set_sniffer(struct mt7921_dev *dev, struct ieee80211_vif *vif,
 			   bool enable);
 int mt7921_mcu_config_sniffer(struct mt7921_vif *vif,
 			      struct ieee80211_chanctx_conf *ctx);
+int mt7921_mcu_get_temperature(struct mt7921_phy *phy);
 
 int mt7921_usb_sdio_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
 				   enum mt76_txq_id qid, struct mt76_wcid *wcid,
Signature · 1 line
2.40.0
maintainer
#1 lore raw
Quoted 32 lines

From: Ben Greear <[email protected]>

Allow sensors tool to read radio's temperature, example:

mt7921_phy17-pci-1800
Adapter: PCI adapter
temp1: +72.0°C

---
 .../net/wireless/mediatek/mt76/mt7921/init.c  | 53 +++++++++++++++++++
 .../net/wireless/mediatek/mt76/mt7921/mcu.c   | 17 ++++++
 .../wireless/mediatek/mt76/mt7921/mt7921.h    |  1 +
 3 files changed, 71 insertions(+)
drivers/net/wireless/mediatek/mt76/mt7921/init.c +3 -0
index c15ce1a19000..18f0f2dfbbcf 100644
@@ -2,6 +2,9 @@
 /* Copyright (C) 2020 MediaTek Inc. */
 
 #include <linux/etherdevice.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/thermal.h>
 #include <linux/firmware.h>
 #include "mt7921.h"
 #include "../mt76_connac2_mac.h"
@@ -58,6 +61,50 @@ static const struct ieee80211_iface_combination if_comb_chanctx[] = {
 	}
 };

drivers/net/wireless/mediatek/mt76/mt7921/init.c static const struct ieee80211_iface_combination if_comb_chanctx[] = { · line 72

Quoted 10 lines
drivers/net/wireless/mediatek/mt76/mt7921/init.c +9 -0
 
+static ssize_t mt7921_thermal_temp_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct mt7921_phy *phy = dev_get_drvdata(dev);
+	int i = to_sensor_dev_attr(attr)->index;
+	int temperature;
+
+	switch (i) {

nit: you can drop i and just use to_sensor_dev_attr(attr)->index

drivers/net/wireless/mediatek/mt76/mt7921/init.c static const struct ieee80211_iface_combination if_comb_chanctx[] = { · line 73

Quoted 1 line
drivers/net/wireless/mediatek/mt76/mt7921/init.c +1 -0
+	case 0:

I think you need to wake the device up here running mt7921_mutex_acquire before sending the mcu command.

nit: you can move temperature variable definition here.

Regards,
Lorenzo

Quoted 93 lines
drivers/net/wireless/mediatek/mt76/mt7921/init.c +40 -0
+		temperature = mt7921_mcu_get_temperature(phy);
+		if (temperature < 0)
+			return temperature;
+		/* display in millidegree celcius */
+		return sprintf(buf, "%u\n", temperature * 1000);
+	default:
+		return -EINVAL;
+	}
+}
+static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0);
+
+static struct attribute *mt7921_hwmon_attrs[] = {
+	&sensor_dev_attr_temp1_input.dev_attr.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(mt7921_hwmon);
+
+static int mt7921_thermal_init(struct mt7921_phy *phy)
+{
+	struct wiphy *wiphy = phy->mt76->hw->wiphy;
+	struct device *hwmon;
+	const char *name;
+
+	name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7921_%s",
+			      wiphy_name(wiphy));
+
+	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
+						       mt7921_hwmon_groups);
+	if (IS_ERR(hwmon))
+		return PTR_ERR(hwmon);
+
+	return 0;
+}
+
 static void
 mt7921_regd_notifier(struct wiphy *wiphy,
 		     struct regulatory_request *request)
@@ -384,6 +431,12 @@ static void mt7921_init_work(struct work_struct *work)
 		return;
 	}
 
+	ret = mt7921_thermal_init(&dev->phy);
+	if (ret) {
+		dev_err(dev->mt76.dev, "thermal_init failed\n");
+		return;
+	}
+
 	/* we support chip reset now */
 	dev->hw_init_done = true;
 
drivers/net/wireless/mediatek/mt76/mt7921/mcu.c +17 -0
index 9c4dcc0e5a7c..abeedacc28f2 100644
@@ -1346,6 +1346,23 @@ int mt7921_mcu_set_clc(struct mt7921_dev *dev, u8 *alpha2,
 	return 0;
 }
 
+int mt7921_mcu_get_temperature(struct mt7921_phy *phy)
+{
+	struct mt7921_dev *dev = phy->dev;
+	struct {
+		u8 ctrl_id;
+		u8 action;
+		u8 band_idx;
+		u8 rsv[5];
+	} req = {
+		.ctrl_id = THERMAL_SENSOR_TEMP_QUERY,
+		.band_idx = phy->mt76->band_idx,
+	};
+
+	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_CTRL), &req,
+				 sizeof(req), true);
+}
+
 int mt7921_mcu_set_rxfilter(struct mt7921_dev *dev, u32 fif,
 			    u8 bit_op, u32 bit_map)
 {
drivers/net/wireless/mediatek/mt76/mt7921/mt7921.h +1 -0
index 706f00df6836..85fddf99d497 100644
@@ -568,6 +568,7 @@ int mt7921_mcu_set_sniffer(struct mt7921_dev *dev, struct ieee80211_vif *vif,
 			   bool enable);
 int mt7921_mcu_config_sniffer(struct mt7921_vif *vif,
 			      struct ieee80211_chanctx_conf *ctx);
+int mt7921_mcu_get_temperature(struct mt7921_phy *phy);
 
 int mt7921_usb_sdio_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
 				   enum mt76_txq_id qid, struct mt76_wcid *wcid,
Signature · 1 line
2.40.0
author
#2 lore raw
Quoted 45 lines · Lorenzo Bianconi
Quoted 32 lines

From: Ben Greear <[email protected]>

Allow sensors tool to read radio's temperature, example:

mt7921_phy17-pci-1800
Adapter: PCI adapter
temp1: +72.0°C

---
  .../net/wireless/mediatek/mt76/mt7921/init.c  | 53 +++++++++++++++++++
  .../net/wireless/mediatek/mt76/mt7921/mcu.c   | 17 ++++++
  .../wireless/mediatek/mt76/mt7921/mt7921.h    |  1 +
  3 files changed, 71 insertions(+)
drivers/net/wireless/mediatek/mt76/mt7921/init.c +3 -0
index c15ce1a19000..18f0f2dfbbcf 100644
@@ -2,6 +2,9 @@
  /* Copyright (C) 2020 MediaTek Inc. */
 
  #include <linux/etherdevice.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/thermal.h>
  #include <linux/firmware.h>
  #include "mt7921.h"
  #include "../mt76_connac2_mac.h"
@@ -58,6 +61,50 @@ static const struct ieee80211_iface_combination if_comb_chanctx[] = {
  	}
  };

drivers/net/wireless/mediatek/mt76/mt7921/init.c static const struct ieee80211_iface_combination if_comb_chanctx[] = { · line 72

Quoted 10 lines
drivers/net/wireless/mediatek/mt76/mt7921/init.c +9 -0
 
+static ssize_t mt7921_thermal_temp_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct mt7921_phy *phy = dev_get_drvdata(dev);
+	int i = to_sensor_dev_attr(attr)->index;
+	int temperature;
+
+	switch (i) {

nit: you can drop i and just use to_sensor_dev_attr(attr)->index

Quoted 4 lines

drivers/net/wireless/mediatek/mt76/mt7921/init.c static const struct ieee80211_iface_combination if_comb_chanctx[] = { · line 73

Quoted 1 line
drivers/net/wireless/mediatek/mt76/mt7921/init.c +1 -0
+	case 0:

I think you need to wake the device up here running mt7921_mutex_acquire before sending the mcu command.

I need to wake it up and acquire a mutex, or does acquiring the mutex wake it automatically?

And, mt7915 has some other logic in this area. I left the switch logic in to match that, thinking maybe later we can add the other options to 7921?

Do you know if 7921 supports the other options like 7915?

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
maintainer
#3 lore raw
Quoted 44 lines · Lorenzo Bianconi
Quoted 43 lines
Quoted 30 lines

From: Ben Greear <[email protected]>

Allow sensors tool to read radio's temperature, example:

mt7921_phy17-pci-1800
Adapter: PCI adapter
temp1: +72.0°C

---
  .../net/wireless/mediatek/mt76/mt7921/init.c  | 53 +++++++++++++++++++
  .../net/wireless/mediatek/mt76/mt7921/mcu.c   | 17 ++++++
  .../wireless/mediatek/mt76/mt7921/mt7921.h    |  1 +
  3 files changed, 71 insertions(+)
drivers/net/wireless/mediatek/mt76/mt7921/init.c +3 -0
index c15ce1a19000..18f0f2dfbbcf 100644
@@ -2,6 +2,9 @@
  /* Copyright (C) 2020 MediaTek Inc. */
  #include <linux/etherdevice.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/thermal.h>
  #include <linux/firmware.h>
  #include "mt7921.h"
  #include "../mt76_connac2_mac.h"
@@ -58,6 +61,50 @@ static const struct ieee80211_iface_combination if_comb_chanctx[] = {
  	}

drivers/net/wireless/mediatek/mt76/mt7921/init.c static const struct ieee80211_iface_combination if_comb_chanctx[] = { · line 71

Quoted 10 lines
drivers/net/wireless/mediatek/mt76/mt7921/init.c +9 -0
  };
+static ssize_t mt7921_thermal_temp_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct mt7921_phy *phy = dev_get_drvdata(dev);
+	int i = to_sensor_dev_attr(attr)->index;
+	int temperature;
+
+	switch (i) {

nit: you can drop i and just use to_sensor_dev_attr(attr)->index

Quoted 7 lines
Quoted 4 lines

drivers/net/wireless/mediatek/mt76/mt7921/init.c static const struct ieee80211_iface_combination if_comb_chanctx[] = { · line 73

Quoted 1 line
drivers/net/wireless/mediatek/mt76/mt7921/init.c +1 -0
+	case 0:

I think you need to wake the device up here running mt7921_mutex_acquire before sending the mcu command.

I need to wake it up and acquire a mutex, or does acquiring the mutex wake it automatically?

acquiring the lock with mt7921_mutex_acquire() will wake the device up.

Quoted 5 lines

And, mt7915 has some other logic in this area. I left the switch logic in to match that, thinking maybe later we can add the other options to 7921?

Do you know if 7921 supports the other options like 7915?

nope.

Regards,
Lorenzo

Quoted 10 lines

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
#4 lore raw
In reply to [email protected]
Quoted 37 lines · On Thu, May 18, 2023 at 01:07:18PM -0700, [email protected] wrote:

From: Ben Greear <[email protected]>

Allow sensors tool to read radio's temperature, example:

mt7921_phy17-pci-1800
Adapter: PCI adapter
temp1: +72.0°C

---
 .../net/wireless/mediatek/mt76/mt7921/init.c  | 53 +++++++++++++++++++
 .../net/wireless/mediatek/mt76/mt7921/mcu.c   | 17 ++++++
 .../wireless/mediatek/mt76/mt7921/mt7921.h    |  1 +
 3 files changed, 71 insertions(+)
drivers/net/wireless/mediatek/mt76/mt7921/init.c +7 -0
index c15ce1a19000..18f0f2dfbbcf 100644
@@ -2,6 +2,9 @@
 /* Copyright (C) 2020 MediaTek Inc. */
 
 #include <linux/etherdevice.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/thermal.h>
 #include <linux/firmware.h>
 #include "mt7921.h"
 #include "../mt76_connac2_mac.h"
@@ -58,6 +61,50 @@ static const struct ieee80211_iface_combination if_comb_chanctx[] = {
 	}
 };
 
+static ssize_t mt7921_thermal_temp_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{

drivers/net/wireless/mediatek/mt76/mt7921/init.c static const struct ieee80211_iface_combination if_comb_chanctx[] = { · line 77

Quoted 10 lines
drivers/net/wireless/mediatek/mt76/mt7921/init.c +10 -0
+	struct mt7921_phy *phy = dev_get_drvdata(dev);
+	int i = to_sensor_dev_attr(attr)->index;
+	int temperature;
+
+	switch (i) {
+	case 0:
+		temperature = mt7921_mcu_get_temperature(phy);
+		if (temperature < 0)
+			return temperature;
+		/* display in millidegree celcius */

Hi Ben,

a minor nit from my side: s/celcius/Celsius/

Quoted 5 lines
drivers/net/wireless/mediatek/mt76/mt7921/init.c +5 -0
+		return sprintf(buf, "%u\n", temperature * 1000);
+	default:
+		return -EINVAL;
+	}
+}

...