[RFC PATCH 1/2] thermal: provide a way to get thermal sensor from a device tree node

From: Alexandre Bailon
Date: Thu Aug 19 2021 - 08:30:57 EST


In order to add support of a virtual thermal sensor,
add a way to get a sensor using the device tree node and sensor id.

Signed-off-by: Alexandre Bailon <abailon@xxxxxxxxxxxx>
---
drivers/thermal/thermal_of.c | 43 ++++++++++++++++++++++++++++++++++++
include/linux/thermal.h | 12 ++++++++++
2 files changed, 55 insertions(+)

diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 6379f26a335f6..4d3b07ceef41a 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -82,6 +82,46 @@ struct __thermal_zone {
const struct thermal_zone_of_device_ops *ops;
};

+
+static LIST_HEAD(thermal_sensors);
+
+struct thermal_sensor *thermal_of_get_sensor(struct device_node *np, int id)
+{
+ struct thermal_sensor *sensor;
+ list_for_each_entry(sensor, &thermal_sensors, node) {
+ if (sensor->dev->of_node == np && sensor->id == id) {
+ return sensor;
+ }
+ }
+
+ return NULL;
+}
+
+static int thermal_of_register_sensor(struct device *dev, int id,
+ void *sensor_data,
+ const struct thermal_zone_of_device_ops *ops)
+{
+
+ struct thermal_sensor *sensor;
+
+ sensor = thermal_of_get_sensor(dev->of_node, id);
+ if (sensor)
+ return 0;
+
+ sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
+ sensor->dev = dev;
+ sensor->id = id;
+ sensor->sensor_data = sensor_data;
+ sensor->ops = ops;
+
+ list_add(&sensor->node, &thermal_sensors);
+
+ return 0;
+}
+
+
+
+
/*** DT thermal zone device callbacks ***/

static int of_thermal_get_temp(struct thermal_zone_device *tz,
@@ -518,6 +558,9 @@ thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
if (!IS_ERR(tzd))
thermal_zone_device_enable(tzd);

+ /* TODO handle errors */
+ thermal_of_register_sensor(dev, id, data, ops);
+
of_node_put(child);
goto exit;
}
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index 8da5b61070472..3c46b142ebfef 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -104,6 +104,16 @@ struct thermal_cooling_device {
struct list_head node;
};

+struct thermal_sensor {
+ struct device *dev;
+ int id;
+
+ void *sensor_data;
+ const struct thermal_zone_of_device_ops *ops;
+
+ struct list_head node;
+};
+
/**
* struct thermal_zone_device - structure for a thermal zone
* @id: unique id number for each thermal zone
@@ -394,6 +404,8 @@ int thermal_zone_get_offset(struct thermal_zone_device *tz);
int thermal_zone_device_enable(struct thermal_zone_device *tz);
int thermal_zone_device_disable(struct thermal_zone_device *tz);
void thermal_zone_device_critical(struct thermal_zone_device *tz);
+
+struct thermal_sensor *thermal_of_get_sensor(struct device_node *np, int id);
#else
static inline struct thermal_zone_device *thermal_zone_device_register(
const char *type, int trips, int mask, void *devdata,
--
2.31.1