Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1649575pxb; Mon, 23 Aug 2021 00:54:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVsNe0xjYXfOoj4sDTUU8YyAKq5lRmgUb2SrgkSQA5iH4a1Mbgw6cQhbLpE7r9pTfeGsSc X-Received: by 2002:a05:6402:2049:: with SMTP id bc9mr35599817edb.130.1629705290365; Mon, 23 Aug 2021 00:54:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629705290; cv=none; d=google.com; s=arc-20160816; b=fmnD3amypquYFM2zqABfVtTTlIq03XC0PLcmjpdyJkNY977Yrenk3xw4DSvrqQXQnT 1Eatns9BczfEZIYkBZ8J7VZKYpQDnLWWE3ZDUpTC+2jtMlQyTguG881RzRmDWcbV0ZVv o4CdAeBuTzFkWvxL/MX6Tqw5AE0fyte5XuHItOVH4zfFk8rWmY9rrRIIEht0KJgAmPKt IXxybFgjsxODR99bhoekFNaSwcoHQAQsvgD145ZMpsIVNnQ8XLqCdgWMSbP/S1HU0mSy mjnjcA1EZ4UmFyHWXZmbnmdLRnCQuMJPW7tBnMaMWe0YBlxkiGCc6rFzA9Hws/s1UtNT cbnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=V+vPGHI9SVeq2sopTpon+vV9taO7FHpPMPDfk1nYm0I=; b=r2SK7Gy6LX4JnwVUBbEtP4mviGztWyaTMN0B6RfZjJhCR8OPFmC7pwd09J7PxXBEr4 h4Pca1u4mjiQOHPDi0UZ8twmOT9Fq7v3xJ+/CxmGH89XpXZz68UXhTFICtkOHyFpv362 kC4eMnW+OcLULwODKr28Gvda4hPBJ3AqWoZVdGu9cg75rL4KN9YP39k7XWLjq+yQ++Jz tjjMqchuxl5IqkfcSlml/Lm1pzOdxcB7BrzuYcuCBmRtkw5cM0MQWKI6i0lmiq62gzOS M5a2LnvavgvtxVZHd/MF59psAbc/rW9Du9ODo02W7gANcgvcvVwtQW+uEulvrqiJddxU a8MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b="jtpopl/c"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k23si6669893edj.270.2021.08.23.00.54.28; Mon, 23 Aug 2021 00:54:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b="jtpopl/c"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235115AbhHWHxu (ORCPT + 99 others); Mon, 23 Aug 2021 03:53:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235204AbhHWHxt (ORCPT ); Mon, 23 Aug 2021 03:53:49 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9717C061756 for ; Mon, 23 Aug 2021 00:53:06 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id v10so13645160wrd.4 for ; Mon, 23 Aug 2021 00:53:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=V+vPGHI9SVeq2sopTpon+vV9taO7FHpPMPDfk1nYm0I=; b=jtpopl/cB84EcZ+xmqeCzJ0hOgYb3IzvrOHuzSw1kbR0nker/+mZpSJ1JRfnkucPUM MquYTkl/ZpuXUR/MtHOxru78Jt9yb2MjxCQZJaGWTcXL69cJXY/+22yMdd9vzJLFX2Kf PXpfw/LtifHIrhpDEtlvtOgj5RfTH+6N68Z02xX72A/WBjUt9vFTLad/XQcizO4qSs6u M9W29HW4vDnIlCp/Zwj/3X1wQ4pqdQdNRtkW+8pkbF1cmbo30rZYz7dT6jD40S6r3wRv w/NOpUSA9EW3oW8Deq00mkec7px+Xf61ohdewFC22vIfDaWoDq8j8mMmWagX7m0NFIBm Qj1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=V+vPGHI9SVeq2sopTpon+vV9taO7FHpPMPDfk1nYm0I=; b=hq68Qmendh3ewFgXPNWD+Ku+PbVljgQxnxovqiKHBTD40mBOopQzn3F4pfnnre0LNo cxpZ3LijhQK/CsY0900+hyVKj8Oq3pbfZeZEBbAeC3gv2D5oWSoHfVWUkhSSSV79kPwO d+Ar36cptwapRp2eEx7Cxfuu/S2KbtODi6U9iVz3vvHDGiJt7Jd3Js/xgCdcsq4ggAH4 tR7uR4fZ80ZEG2Yd5SEnkqjtckMbihrvvj4VkASV1jm3BqRiUfjzc5LkB9S9EW/d7FwL 3xgMZDOQ5kBXJHopcCrIMACwkK4i03bTTx5OpPQ/X5eAoPgllooZTArRIne8I9E8KT0S gYjQ== X-Gm-Message-State: AOAM533qZNKXt5L8/iiW4F0j03UxRhgM1eUFM/Jqvnd1cu/oAFZiFIWk mnbLNaEuD2t4xeKB4dpO+ion/Q== X-Received: by 2002:a5d:64ce:: with SMTP id f14mr11980656wri.17.1629705184690; Mon, 23 Aug 2021 00:53:04 -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 q22sm17447869wmj.32.2021.08.23.00.53.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 Aug 2021 00:53:04 -0700 (PDT) Subject: Re: [RFC PATCH 2/2] thermal: add a virtual sensor to aggregate temperatures To: Daniel Lezcano , rui.zhang@intel.com, amitk@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, ben.tseng@mediatek.com, khilman@baylibre.com References: <20210819123215.591593-1-abailon@baylibre.com> <20210819123215.591593-3-abailon@baylibre.com> <068ad0f9-ca73-6f62-f04a-6916c055279a@linaro.org> From: Alexandre Bailon Message-ID: <55b94272-7364-72cf-f95d-5de0b0f99495@baylibre.com> Date: Mon, 23 Aug 2021 09:54:44 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <068ad0f9-ca73-6f62-f04a-6916c055279a@linaro.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Daniel, On 20/08/2021 14:52, Daniel wrote: > On 19/08/2021 14:32, Alexandre Bailon wrote: >> 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 > We discussed the virtual sensor doing aggregation but may be we can give > it another purpose in the future like returning a constant temp or > low/high pass filter. > > It may make sense to use a more generic name like virtual sensor for > example. Indeed, this would make more sense. > >> + tristate "Generic thermal aggregator driver" >> + depends on TERMAL_OF || COMPILE_TEST > s/TERMAL_OF/THERMAL_OF/ > >> + 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; >> + > What happens if a hardware sensor module is unloaded ? Hum, I don't know how to deal with it. Maybe adding refcounting to sensors to prevent module unloading ? > >> + 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, > .get_temp = thermal_virtual_sensor_get_temp ? Actually, I though I could create a thermol_zone_of_device_ops for each type of operation (min, max, etc) we would support and would just to register the right ops at probe time. >> +}; >> + >> +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); > devm_kzalloc > >> + 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); > devm_kzalloc > >> + aggr_sensor->np = args.np; > Why the 'np' and id are stored, they won't be needed anymore, no ? Right. At some point, I had issues with the sensors that was not available. I though it was an probe orderings issue and I tried to get the sensor later from the callback. It was an issue with the sensor module itself, and not with the ordering but I forgot to remove np and id that not useful anymore. > >> + 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", "^virtual,.*": > As stated in the documentation > Documentation/devicetree/bindings/vendor-prefixes.yaml > > "^virtual,.*": > description: Used for virtual device without specific vendor. > > I suggest something like: > > .compatible = "virtual,thermal-sensor", OK, makes sense to me. Thanks you for the review. Alexandre > > >> + }, >> + { >> + }, >> +}; >> +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"); >> >