Skip to content

Commit

Permalink
hwmon: drivetemp: support to be a platform driver for thermal_of
Browse files Browse the repository at this point in the history
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@realtek.com>
  • Loading branch information
phinexhung authored and intel-lab-lkp committed Mar 15, 2023
1 parent 5c88c44 commit 1c53b68
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
35 changes: 35 additions & 0 deletions 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@realtek.com>

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>;
};
102 changes: 98 additions & 4 deletions drivers/hwmon/drivetemp.c
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -536,6 +545,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,
Expand All @@ -555,11 +604,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);
Expand All @@ -569,9 +623,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;
Expand Down Expand Up @@ -604,14 +658,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);
Expand Down

0 comments on commit 1c53b68

Please sign in to comment.