[PATCH v1] hwmon: drivetemp: support to be a platform driver for thermal_of

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Support thermal zone so that we can just rely on dts to describe a
thermal zone and do the cooling operations.

You can define a comptible string "drivetemp,hdd-sensors" to enable
this, such as

	sata_port0: sata-port@0 {
		compatible = "drivetemp,hdd-sensors";
		#thermal-sensor-cells = <0>;
	}

Then define a thermal with this sensor to get it work.

               hdd_thermal: hdd-thermal {
                       thermal-sensors = <&sata_port0>;
		}

In most of the SoC systems, using dts to handle cooling is common.
This can eliminate the usage of user space application to check
the value exported in hwmon and then through sysfs to cooling.

Signed-off-by: phinex <phinex@xxxxxxxxxxx>

---
 .../bindings/hwmon/drivetemp,hdd-sensors.yaml |  35 ++++++
 drivers/hwmon/drivetemp.c                     | 102 +++++++++++++++++-
 2 files changed, 133 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/hwmon/drivetemp,hdd-sensors.yaml

diff --git a/Documentation/devicetree/bindings/hwmon/drivetemp,hdd-sensors.yaml b/Documentation/devicetree/bindings/hwmon/drivetemp,hdd-sensors.yaml
new file mode 100644
index 000000000000..939d7a923e94
--- /dev/null
+++ b/Documentation/devicetree/bindings/hwmon/drivetemp,hdd-sensors.yaml
@@ -0,0 +1,35 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/drivetemp,hdd-sensors.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Drivetemp Temperature Monitor
+
+maintainers:
+  - Phinex <phinex@xxxxxxxxxxx>
+
+description: |
+  Drivetemp Temperature Monitor that support a single thermal zone
+  This single thermal zone can support multiple hard drives,
+  it uses maximal temperature of these hard drivers as its temp value.
+
+properties:
+  compatible:
+    enum:
+      - drivetemp,hdd-sensors
+
+  '#thermal-sensor-cells':
+    const: 0
+
+required:
+  - compatible
+
+additionalProperties: false
+
+examples:
+  - |
+    sata_port0: sata-port@0 {
+        ompatible = "drivetemp,hdd-sensors";
+        #thermal-sensor-cells = <0>;
+    };
diff --git a/drivers/hwmon/drivetemp.c b/drivers/hwmon/drivetemp.c
index 1eb37106a220..9a60315d732c 100644
--- a/drivers/hwmon/drivetemp.c
+++ b/drivers/hwmon/drivetemp.c
@@ -107,6 +107,14 @@
 #include <scsi/scsi_device.h>
 #include <scsi/scsi_driver.h>
 #include <scsi/scsi_proto.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/thermal.h>
+#include <linux/libata.h>
+
+/*A single thermal_zone for all HDD sensors */
+static struct thermal_zone_device *tz;
+#define MAX_NAME_LEN 255
 
 struct drivetemp_data {
 	struct list_head list;		/* list of instantiated devices */
@@ -126,6 +134,7 @@ struct drivetemp_data {
 	int temp_max;			/* max temp */
 	int temp_lcrit;			/* lower critical limit */
 	int temp_crit;			/* critical limit */
+	u8 drivename[MAX_NAME_LEN];     /* Keep the drive name for each hdd*/
 };
 
 static LIST_HEAD(drivetemp_devlist);
@@ -537,6 +546,46 @@ static const struct hwmon_channel_info *drivetemp_info[] = {
 	NULL
 };
 
+#if IS_ENABLED(CONFIG_THERMAL_OF)
+static int hdd_read_temp(void *data, int *temp)
+{
+	int err;
+	struct drivetemp_data *st = data;
+	long value, max = 0;
+
+	list_for_each_entry(st, &drivetemp_devlist, list) {
+		mutex_lock(&st->lock);
+		err = st->get_temp(st, hwmon_temp_input, &value);
+		mutex_unlock(&st->lock);
+
+		if (value > max)
+			max = value;
+	}
+
+	/*non-existent sensor or not ready*/
+	if (max == 0)
+		return -EAGAIN;
+
+	*temp = (int)max;
+
+	dev_dbg(st->dev, "%s, sensor read %d\n", __func__, *temp);
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops hdd_sensor_ops = {
+	.get_temp = hdd_read_temp,
+};
+
+static const struct of_device_id hdd_of_match[] = {
+	{
+		.compatible = "drivetemp,hdd-sensors",
+	},
+	{},
+};
+MODULE_DEVICE_TABLE(of, hdd_of_match);
+#endif
+
 static const struct hwmon_ops drivetemp_ops = {
 	.is_visible = drivetemp_is_visible,
 	.read = drivetemp_read,
@@ -556,11 +605,16 @@ static int drivetemp_add(struct device *dev, struct class_interface *intf)
 	struct scsi_device *sdev = to_scsi_device(dev->parent);
 	struct drivetemp_data *st;
 	int err;
+	struct ata_port *ap;
 
 	st = kzalloc(sizeof(*st), GFP_KERNEL);
 	if (!st)
 		return -ENOMEM;
 
+	ap = ata_shost_to_port(sdev->host);
+
+	snprintf(st->drivename, MAX_NAME_LEN, "drivetemp_port%d", ap->port_no);
+
 	st->sdev = sdev;
 	st->dev = dev;
 	mutex_init(&st->lock);
@@ -570,9 +624,9 @@ static int drivetemp_add(struct device *dev, struct class_interface *intf)
 		goto abort;
 	}
 
-	st->hwdev = hwmon_device_register_with_info(dev->parent, "drivetemp",
-						    st, &drivetemp_chip_info,
-						    NULL);
+	st->hwdev = hwmon_device_register_with_info(
+		dev->parent, st->drivename, st, &drivetemp_chip_info, NULL);
+
 	if (IS_ERR(st->hwdev)) {
 		err = PTR_ERR(st->hwdev);
 		goto abort;
@@ -605,14 +659,54 @@ static struct class_interface drivetemp_interface = {
 	.remove_dev = drivetemp_remove,
 };
 
+#if IS_ENABLED(CONFIG_THERMAL_OF)
+static int hdd_hwmon_probe(struct platform_device *pdev)
+{
+	if (list_empty(&drivetemp_devlist))
+		return -EPROBE_DEFER;
+
+	if (!tz)
+		devm_thermal_zone_of_sensor_register(
+			&pdev->dev, 0, &drivetemp_devlist, &hdd_sensor_ops);
+
+	return 0;
+}
+
+static int hdd_hwmon_remove(struct platform_device *dev)
+{
+	thermal_zone_device_unregister(tz);
+	tz = NULL;
+	return 0;
+}
+
+static struct platform_driver hdd_hwmon_platdrv = {
+	.driver = {
+		.name   = "hdd-hwmon",
+		.of_match_table = hdd_of_match,
+	},
+	.probe          = hdd_hwmon_probe,
+	.remove		= hdd_hwmon_remove,
+};
+#endif
+
 static int __init drivetemp_init(void)
 {
-	return scsi_register_interface(&drivetemp_interface);
+	int ret;
+
+	ret = scsi_register_interface(&drivetemp_interface);
+#if IS_ENABLED(CONFIG_THERMAL_OF)
+	if (ret == 0)
+		ret = platform_driver_register(&hdd_hwmon_platdrv);
+#endif
+	return ret;
 }
 
 static void __exit drivetemp_exit(void)
 {
 	scsi_unregister_interface(&drivetemp_interface);
+#if IS_ENABLED(CONFIG_THERMAL_OF)
+	platform_driver_unregister(&hdd_hwmon_platdrv);
+#endif
 }
 
 module_init(drivetemp_init);
-- 
2.17.1




[Index of Archives]     [LM Sensors]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux