[PATCH 1/1] drivers: hwmon: pmbus: register with thermal for PSC_TEMPERATURE

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

 



Some pmbus device drivers have device tree support and
may want to use of-thermal to register a thermal zone
OF sensor for those device drivers.

This way we allow describing device tree thermal zones
for pmbus device drivers with device tree support.

This patch achieves this by registering pmbus sensors
with thermal subsystem if they are PSC_TEMPERATURE
and are providing _input hwmon interface.

Cc: Guenter Roeck <linux@xxxxxxxxxxxx> (maintainer:PMBUS HARDWARE MONITORING DRIVERS)
Cc: Jean Delvare <jdelvare@xxxxxxxx> (maintainer:HARDWARE MONITORING)
Cc: linux-hwmon@xxxxxxxxxxxxxxx (open list:PMBUS HARDWARE MONITORING DRIVERS)
Cc: linux-kernel@xxxxxxxxxxxxxxx (open list)
Signed-off-by: Eduardo Valentin <eduval@xxxxxxxxxx>
Signed-off-by: Eduardo Valentin <evalenti@xxxxxxxxxx>
---
 drivers/hwmon/pmbus/pmbus_core.c | 88 +++++++++++++++++++++++++++++---
 1 file changed, 80 insertions(+), 8 deletions(-)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index 776ee2237be2..a51cdfab1c3e 100644
--- a/drivers/hwmon/pmbus/pmbus_core.c
+++ b/drivers/hwmon/pmbus/pmbus_core.c
@@ -19,6 +19,8 @@
 #include <linux/pmbus.h>
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
+#include <linux/of.h>
+#include <linux/thermal.h>
 #include "pmbus.h"
 
 /*
@@ -1078,7 +1080,71 @@ static int pmbus_add_boolean(struct pmbus_data *data,
 	return pmbus_add_attribute(data, &a->dev_attr.attr);
 }
 
-static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
+/* of thermal for pmbus temperature sensors */
+struct pmbus_thermal_data {
+	struct i2c_client *client;
+	struct pmbus_sensor *sensor;
+};
+
+static int pmbus_thermal_get_temp(void *data, int *temp)
+{
+	struct pmbus_thermal_data *tdata = data;
+	struct i2c_client *client = tdata->client;
+	struct pmbus_sensor *sensor = tdata->sensor;
+	struct pmbus_data *pmbus_data = i2c_get_clientdata(client);
+	struct device *dev = pmbus_data->hwmon_dev;
+	int ret = 0;
+
+	if (!dev) {
+		/* May not even get to hwmon yet */
+		*temp = 0;
+		return 0;
+	}
+
+	mutex_lock(&pmbus_data->update_lock);
+	pmbus_update_sensor_data(client, sensor);
+	if (sensor->data < 0)
+		ret = sensor->data;
+	else
+		*temp = (int)pmbus_reg2data(pmbus_data, sensor);
+	mutex_unlock(&pmbus_data->update_lock);
+
+	return ret;
+}
+
+static const struct thermal_zone_of_device_ops pmbus_thermal_ops = {
+	.get_temp = pmbus_thermal_get_temp,
+};
+
+static int pmbus_thermal_add_sensor(struct i2c_client *client,
+				    struct pmbus_data *pmbus_data,
+				    struct pmbus_sensor *sensor, int index)
+{
+	struct device *dev = pmbus_data->dev;
+	struct pmbus_thermal_data *tdata;
+	struct thermal_zone_device *tzd;
+
+	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
+	if (!tdata)
+		return -ENOMEM;
+
+	tdata->sensor = sensor;
+	tdata->client = client;
+
+	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
+						   &pmbus_thermal_ops);
+	/*
+	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
+	 * so ignore that error but forward any other error.
+	 */
+	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
+		return PTR_ERR(tzd);
+
+	return 0;
+}
+
+static struct pmbus_sensor *pmbus_add_sensor(struct i2c_client *client,
+					     struct pmbus_data *data,
 					     const char *name, const char *type,
 					     int seq, int page, int phase,
 					     int reg,
@@ -1121,6 +1187,10 @@ static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
 	sensor->next = data->sensors;
 	data->sensors = sensor;
 
+	/* temperature sensors with _input values are registered with thermal */
+	if (class == PSC_TEMPERATURE && strcmp(type, "input") == 0)
+		pmbus_thermal_add_sensor(client, data, sensor, seq);
+
 	return sensor;
 }
 
@@ -1216,8 +1286,9 @@ static int pmbus_add_limit_attrs(struct i2c_client *client,
 
 	for (i = 0; i < nlimit; i++) {
 		if (pmbus_check_word_register(client, page, l->reg)) {
-			curr = pmbus_add_sensor(data, name, l->attr, index,
-						page, 0xff, l->reg, attr->class,
+			curr = pmbus_add_sensor(client, data, name, l->attr,
+						index, page, 0xff, l->reg,
+						attr->class,
 						attr->update || l->update,
 						false, true);
 			if (!curr)
@@ -1258,7 +1329,7 @@ static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
 		if (ret)
 			return ret;
 	}
-	base = pmbus_add_sensor(data, name, "input", index, page, phase,
+	base = pmbus_add_sensor(client, data, name, "input", index, page, phase,
 				attr->reg, attr->class, true, true, true);
 	if (!base)
 		return -ENOMEM;
@@ -1887,7 +1958,7 @@ static int pmbus_add_fan_ctrl(struct i2c_client *client,
 {
 	struct pmbus_sensor *sensor;
 
-	sensor = pmbus_add_sensor(data, "fan", "target", index, page,
+	sensor = pmbus_add_sensor(client, data, "fan", "target", index, page,
 				  0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN,
 				  false, false, true);
 
@@ -1898,14 +1969,14 @@ static int pmbus_add_fan_ctrl(struct i2c_client *client,
 			(data->info->func[page] & PMBUS_HAVE_PWM34)))
 		return 0;
 
-	sensor = pmbus_add_sensor(data, "pwm", NULL, index, page,
+	sensor = pmbus_add_sensor(client, data, "pwm", NULL, index, page,
 				  0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM,
 				  false, false, true);
 
 	if (!sensor)
 		return -ENOMEM;
 
-	sensor = pmbus_add_sensor(data, "pwm", "enable", index, page,
+	sensor = pmbus_add_sensor(client, data, "pwm", "enable", index, page,
 				  0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM,
 				  true, false, false);
 
@@ -1947,7 +2018,8 @@ static int pmbus_add_fan_attributes(struct i2c_client *client,
 			    (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
 				continue;
 
-			if (pmbus_add_sensor(data, "fan", "input", index,
+			if (pmbus_add_sensor(client, data, "fan",
+					     "input", index,
 					     page, 0xff, pmbus_fan_registers[f],
 					     PSC_FAN, true, true, true) == NULL)
 				return -ENOMEM;
-- 
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