From patchwork Thu Aug 19 12:32:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Bailon X-Patchwork-Id: 12447111 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DAB2BC4338F for ; Thu, 19 Aug 2021 12:30:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C17856113B for ; Thu, 19 Aug 2021 12:30:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239632AbhHSMbX (ORCPT ); Thu, 19 Aug 2021 08:31:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239646AbhHSMbV (ORCPT ); Thu, 19 Aug 2021 08:31:21 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68633C061756 for ; Thu, 19 Aug 2021 05:30:45 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id u15so3751387wmj.1 for ; Thu, 19 Aug 2021 05:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RQnOV6HhqP7xV2Furr8pq5/wUZ2SXaR/nLHhOafGXkg=; b=2M3dXhUfU0qKuKCF87Zz/LV0W9JQ4kD1+H3/waNq+P2Yf+7Rxl8X8TFZGsAZpel+Nn UCTQouixJTql6pETsgzdq8mtVxzWEhonECdt4/Xf5v1fUkW0RrD0zrFTZm00qnaz0o26 lZpjbcc2W0OR4sb1fI7KVj4jUPu+nHdBJ5TN1filcNaxa1ye9u9gnKPjuhGIHBJl/a/L GhZP3mHe787LX8EZIe3/vHcSLLHUaMXsV+46+ibrE2V6CU7Y2GC2PfV6WdMK8uhn6ltp Kezc7+/+sYCMM2G5ksZC0vXx3G2CEIy42ebH3nmIvdNWSWIho1bcFdzBl5QuwhxGliQK twXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RQnOV6HhqP7xV2Furr8pq5/wUZ2SXaR/nLHhOafGXkg=; b=byE/fpQwjgdXL51HwAtaI59SACDFLC8lAmCaQ5/wi5ThBEuK6et9lLd3juKbaH9eoG 4Z9wWOhWz7KAgR9gbziHkP246U/+/Oq/OSw0lIBOXDeI98UZZI6dJkTp/ZAD3PDL8nBk x6VB2rK9nYTv6kXQMfrsAu3/fjCdhrqIsXL0E54r2nyfeX7dgaum4dD5yFK+0T5tJUED gKK5lf/Cx2Ge5ahVTQrOoKIR8YrQPNyh9iDW1P7m0zZ2WAYwdJ4srqlF+t/sd+laU8+9 wBulf1xRq8EWXWws+qrHtyVfkF2wUza4CHSytqI+ev/ErM991C4gPsG8XQ2vVytbcCju X+WA== X-Gm-Message-State: AOAM530/0r3SONVEewudVPYtk47sB33nHrwvcv2NS15V56+6XizjbKGf 90H+S2bpwXOsjON/QyYFmar90MZ+a8YDwQ== X-Google-Smtp-Source: ABdhPJxLoydTz8/ZAOb+nllolXs0SdEtbPQ1+DoQvowXCF1XS0hwyU2vhLvpz1Jrs2HKBUr1Ms9LAQ== X-Received: by 2002:a7b:c0c6:: with SMTP id s6mr13535984wmh.21.1629376243984; Thu, 19 Aug 2021 05:30:43 -0700 (PDT) Received: from alex-xps13.baylibre.local (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id y10sm8146814wmo.18.2021.08.19.05.30.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Aug 2021 05:30:43 -0700 (PDT) From: Alexandre Bailon 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 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 Message-Id: <20210819123215.591593-2-abailon@baylibre.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210819123215.591593-1-abailon@baylibre.com> References: <20210819123215.591593-1-abailon@baylibre.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org 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 --- 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, From patchwork Thu Aug 19 12:32:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Bailon X-Patchwork-Id: 12447113 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D331EC4338F for ; Thu, 19 Aug 2021 12:30:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C20E260231 for ; Thu, 19 Aug 2021 12:30:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239625AbhHSMb3 (ORCPT ); Thu, 19 Aug 2021 08:31:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239503AbhHSMbW (ORCPT ); Thu, 19 Aug 2021 08:31:22 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E735C061756 for ; Thu, 19 Aug 2021 05:30:46 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id r7so8951453wrs.0 for ; Thu, 19 Aug 2021 05:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=X/WKvB1VI4vM6pe2e95RFfcySWVrkDhonAyBFFWmQa8=; b=gSd69I3EpqoMcenCzLmcnoLLEKBqbR1RtEj30JxZ35+9Rl7w4qdENpciAdwDekKXmo wWRrm9C2yMx0VBm8eI1Oyw8+pPaamw0Q8mnT6SbJpiQQqORcoBRxV4yYX6h3nd2TeKrb TUtMLmKHoq5kgt2xJZz+pwge6x+LCh8HLwNsVwfGTweKwUdPMblq6kTRmHSJ+oHTBfZ9 zfWJapXWAD64QzJgXkE/UxnAXmFUtMAnVIQQqEcf81ozxiMLtB7TB52ir55C5rxDT9lz inU36xsvKsmdPf2333nBsT7vkaLe/DJtwFnIQUCrGIqlgW+FWihTc8wf7wzEchEjDv6x BRsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=X/WKvB1VI4vM6pe2e95RFfcySWVrkDhonAyBFFWmQa8=; b=AHOBfOVldVZTJWYjq5wWhPUE4fFnfp2tNS2mET5G8ynRPYjvFFabCBOlLnx3K5L6Wr Qu+CFqoF9WWaob1C87u1NiM7uXKfVf5bPo4MBzWCYxIDcUvoPPwsKc00Dau3Kp9K+KJi 5fy9eX5Z4CtXNomYMFu9uoDfC8f1F+CG+OSw6lWtvYU3QwQiN55TM/cXhzWhOe9jVWcA +EhgqD81OSHdxmcmavp8TPH//2zP61NlvjJJKc1kifkchbZmCavDyAAWVXIJ8zT6r1bG 8oY/pgqxOYkxByFjwdt5Jj/YsDhTca6qBK0OkugoBljfmcgynCc78hsgwmWp7aWYNfJd 7DZQ== X-Gm-Message-State: AOAM533vCcavUhEShHfDtsfN/spGrg2RjxJhUFYtiIJYE+84JHCOjLYe 1q8NWQA28AlnLnwN2xcp3bCnNg== X-Google-Smtp-Source: ABdhPJylz9TOPxhpZf4AyuRNTDd2HXaYZYAo1L9bHp5Lo3Y0JKfwF+ZIQ0ddYiLaz/VVYI0dkjAfew== X-Received: by 2002:a5d:6301:: with SMTP id i1mr3466077wru.423.1629376245008; Thu, 19 Aug 2021 05:30:45 -0700 (PDT) Received: from alex-xps13.baylibre.local (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id y10sm8146814wmo.18.2021.08.19.05.30.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Aug 2021 05:30:44 -0700 (PDT) From: Alexandre Bailon 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 Subject: [RFC PATCH 2/2] thermal: add a virtual sensor to aggregate temperatures Date: Thu, 19 Aug 2021 14:32:15 +0200 Message-Id: <20210819123215.591593-3-abailon@baylibre.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210819123215.591593-1-abailon@baylibre.com> References: <20210819123215.591593-1-abailon@baylibre.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org This adds a virtual thermal sensor that reads temperature from hardware sensor and return an aggregated temperature. Currently, this only return the max temperature. Signed-off-by: Alexandre Bailon --- drivers/thermal/Kconfig | 8 ++ drivers/thermal/Makefile | 1 + drivers/thermal/thermal_aggregator.c | 134 +++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 drivers/thermal/thermal_aggregator.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index 7a4ba50ba97d0..f9c152cfb95bc 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -228,6 +228,14 @@ config THERMAL_MMIO register or shared memory, is a potential candidate to work with this driver. +config THERMAL_AGGREGATOR + tristate "Generic thermal aggregator driver" + depends on TERMAL_OF || COMPILE_TEST + help + This option enables the generic thermal sensor aggregator. + This driver creates a thermal sensor that reads the hardware sensors + and aggregate the temperature. + config HISI_THERMAL tristate "Hisilicon thermal driver" depends on ARCH_HISI || COMPILE_TEST diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 9729a2b089919..5b20ef15aebbe 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -60,3 +60,4 @@ obj-$(CONFIG_UNIPHIER_THERMAL) += uniphier_thermal.o obj-$(CONFIG_AMLOGIC_THERMAL) += amlogic_thermal.o obj-$(CONFIG_SPRD_THERMAL) += sprd_thermal.o obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL) += khadas_mcu_fan.o +obj-$(CONFIG_THERMAL_AGGREGATOR) += thermal_aggregator.o diff --git a/drivers/thermal/thermal_aggregator.c b/drivers/thermal/thermal_aggregator.c new file mode 100644 index 0000000000000..76f871dbfee9e --- /dev/null +++ b/drivers/thermal/thermal_aggregator.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const char *aggr_types[] = { + "min", + "max", + "avg", +}; + +struct thermal_aggr; + +typedef int (*aggregate_fn)(struct thermal_aggr *aggr); + +struct thermal_aggr_sensor { + struct thermal_sensor *sensor; + + struct list_head node; +}; + +struct thermal_aggr { + struct list_head sensors; + aggregate_fn *aggregate; + //struct thermal_zone_device *tz; +}; + +static int thermal_aggr_read_temp(void *data, int *temperature) +{ + struct thermal_aggr *aggr = data; + struct thermal_aggr_sensor *sensor; + int max_temp = 0; + int temp; + + list_for_each_entry(sensor, &aggr->sensors, node) { + if (!sensor->sensor) { + return -ENODEV; + } + + sensor->sensor->ops->get_temp(sensor->sensor->sensor_data, &temp); + max_temp = max(max_temp, temp); + } + + *temperature = max_temp; + + return 0; +} + +static const struct thermal_zone_of_device_ops thermal_aggr_max_ops = { + .get_temp = thermal_aggr_read_temp, +}; + +static int thermal_aggr_probe(struct platform_device *pdev) +{ + struct thermal_aggr *aggr; + struct device *dev = &pdev->dev; + struct of_phandle_args args; + int count; + int ret; + int i; + + count = of_count_phandle_with_args(dev->of_node, + "thermal-sensors", + "#thermal-sensor-cells"); + if (count <= 0) + return -EINVAL; + + aggr = kzalloc(sizeof(*aggr), GFP_KERNEL); + INIT_LIST_HEAD(&aggr->sensors); + + for (i = 0; i < count; i++) { + struct thermal_sensor *sensor; + struct thermal_aggr_sensor *aggr_sensor; + int id; + + ret = of_parse_phandle_with_args(dev->of_node, + "thermal-sensors", + "#thermal-sensor-cells", + i, + &args); + if (ret) { + return ret; + } + + id = args.args_count ? args.args[0] : 0; + sensor = thermal_of_get_sensor(args.np, id); + if (sensor == NULL) { + return -EPROBE_DEFER; + } + + aggr_sensor = kzalloc(sizeof(*aggr_sensor), GFP_KERNEL); + aggr_sensor->np = args.np; + aggr_sensor->id = id; + aggr_sensor->sensor = sensor; + list_add(&aggr_sensor->node, &aggr->sensors); + } + + /*tzdev = */devm_thermal_zone_of_sensor_register(dev, 0, aggr, &thermal_aggr_max_ops); + + return 0; +} + +static int thermal_aggr_remove(struct platform_device *pdev) +{ + return 0; +} + +static const struct of_device_id thermal_aggr_of_match[] = { + { + .compatible = "generic,thermal-aggregator", + }, + { + }, +}; +MODULE_DEVICE_TABLE(of, thermal_aggr_of_match); + +static struct platform_driver thermal_aggr = { + .probe = thermal_aggr_probe, + .remove = thermal_aggr_remove, + .driver = { + .name = "thermal-aggregator", + .of_match_table = thermal_aggr_of_match, + }, +}; + +module_platform_driver(thermal_aggr); +MODULE_AUTHOR("Alexandre Bailon "); +MODULE_DESCRIPTION("Thermal sensor aggregator"); +MODULE_LICENSE("GPL v2");