2022-04-30 19:12:19

by Eduardo Valentin

[permalink] [raw]
Subject: [PATCHv2 1/1] drivers: hwmon: pmbus: register with thermal for PSC_TEMPERATURE

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 <[email protected]> (maintainer:PMBUS HARDWARE MONITORING DRIVERS)
Cc: Jean Delvare <[email protected]> (maintainer:HARDWARE MONITORING)
Cc: [email protected] (open list:PMBUS HARDWARE MONITORING DRIVERS)
Cc: [email protected] (open list)
Signed-off-by: Eduardo Valentin <[email protected]>
Signed-off-by: Eduardo Valentin <[email protected]>
---
V2: Incorporated Guenter's suggestion to use pmbus_data as field of pmbus_thermal_data.

---
drivers/hwmon/pmbus/pmbus_core.c | 68 ++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index 776ee2237be2..371d16e3bac6 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,6 +1080,68 @@ static int pmbus_add_boolean(struct pmbus_data *data,
return pmbus_add_attribute(data, &a->dev_attr.attr);
}

+/* of thermal for pmbus temperature sensors */
+struct pmbus_thermal_data {
+ struct pmbus_data *pmbus_data;
+ struct pmbus_sensor *sensor;
+};
+
+static int pmbus_thermal_get_temp(void *data, int *temp)
+{
+ struct pmbus_thermal_data *tdata = data;
+ struct pmbus_sensor *sensor = tdata->sensor;
+ struct pmbus_data *pmbus_data = tdata->pmbus_data;
+ struct i2c_client *client = to_i2c_client(pmbus_data->dev);
+ 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 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->pmbus_data = pmbus_data;
+
+ 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 pmbus_data *data,
const char *name, const char *type,
int seq, int page, int phase,
@@ -1121,6 +1185,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(data, sensor, seq);
+
return sensor;
}

--
2.17.1


2022-05-03 00:11:31

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCHv2 1/1] drivers: hwmon: pmbus: register with thermal for PSC_TEMPERATURE

On Thu, Apr 28, 2022 at 10:49:26AM -0700, Eduardo Valentin wrote:
> 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 <[email protected]> (maintainer:PMBUS HARDWARE MONITORING DRIVERS)
> Cc: Jean Delvare <[email protected]> (maintainer:HARDWARE MONITORING)
> Cc: [email protected] (open list:PMBUS HARDWARE MONITORING DRIVERS)
> Cc: [email protected] (open list)
> Signed-off-by: Eduardo Valentin <[email protected]>
> Signed-off-by: Eduardo Valentin <[email protected]>

Applied to hwmon-next.

Thanks,
Guenter

> ---
> V2: Incorporated Guenter's suggestion to use pmbus_data as field of pmbus_thermal_data.
>
> ---
> drivers/hwmon/pmbus/pmbus_core.c | 68 ++++++++++++++++++++++++++++++++
> 1 file changed, 68 insertions(+)
>
> diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
> index 776ee2237be2..371d16e3bac6 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,6 +1080,68 @@ static int pmbus_add_boolean(struct pmbus_data *data,
> return pmbus_add_attribute(data, &a->dev_attr.attr);
> }
>
> +/* of thermal for pmbus temperature sensors */
> +struct pmbus_thermal_data {
> + struct pmbus_data *pmbus_data;
> + struct pmbus_sensor *sensor;
> +};
> +
> +static int pmbus_thermal_get_temp(void *data, int *temp)
> +{
> + struct pmbus_thermal_data *tdata = data;
> + struct pmbus_sensor *sensor = tdata->sensor;
> + struct pmbus_data *pmbus_data = tdata->pmbus_data;
> + struct i2c_client *client = to_i2c_client(pmbus_data->dev);
> + 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 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->pmbus_data = pmbus_data;
> +
> + 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 pmbus_data *data,
> const char *name, const char *type,
> int seq, int page, int phase,
> @@ -1121,6 +1185,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(data, sensor, seq);
> +
> return sensor;
> }
>