linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandre Bailon <abailon@baylibre.com>
To: rui.zhang@intel.com, daniel.lezcano@linaro.org, amitk@kernel.org
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	ben.tseng@mediatek.com, khilman@baylibre.com,
	Alexandre Bailon <abailon@baylibre.com>
Subject: [RFC PATCH 1/2] thermal: provide a way to get thermal sensor from a device tree node
Date: Thu, 19 Aug 2021 14:32:14 +0200	[thread overview]
Message-ID: <20210819123215.591593-2-abailon@baylibre.com> (raw)
In-Reply-To: <20210819123215.591593-1-abailon@baylibre.com>

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@baylibre.com>
---
 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


  reply	other threads:[~2021-08-19 12:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19 12:32 [RFC PATCH 0/2] Add a generic virtual thermal sensor Alexandre Bailon
2021-08-19 12:32 ` Alexandre Bailon [this message]
2021-08-19 12:32 ` [RFC PATCH 2/2] thermal: add a virtual sensor to aggregate temperatures Alexandre Bailon
2021-08-20 12:52   ` Daniel Lezcano
2021-08-23  7:54     ` Alexandre Bailon
2021-08-20 11:30 ` [RFC PATCH 0/2] Add a generic virtual thermal sensor Daniel Lezcano
2021-08-23  7:35   ` Alexandre Bailon
2021-08-23  8:40     ` Daniel Lezcano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210819123215.591593-2-abailon@baylibre.com \
    --to=abailon@baylibre.com \
    --cc=amitk@kernel.org \
    --cc=ben.tseng@mediatek.com \
    --cc=daniel.lezcano@linaro.org \
    --cc=khilman@baylibre.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rui.zhang@intel.com \
    --subject='Re: [RFC PATCH 1/2] thermal: provide a way to get thermal sensor from a device tree node' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).