Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp194823pxb; Fri, 29 Oct 2021 08:11:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxg04qj+t8q9BlMQtGZ5X9i9XK3Wmh7T/rq7MkxKsKGGgyIae/qR/Yhm4WTBv54SHyL5rWt X-Received: by 2002:a92:ce02:: with SMTP id b2mr8509680ilo.210.1635520299245; Fri, 29 Oct 2021 08:11:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635520299; cv=none; d=google.com; s=arc-20160816; b=lg080rIqJYJisXr27MWsQcmd7vdzAJgLWt40CPSnmW5hYRGSeAYcbvjYWwLHwXlsoN O7WeQktufsjkJUJRLRr3rxh+q8/UA4yzfPmD3RSUXw83a/oznwTwm3pGKiKpF+gOGT0p 8oBVp50RIohml8QMKfcBJp1f4ZyVpg4kZX+E+AQJTcTgWqdZc1jJDIlOxyJCKYD30K9r oqJF3DUTODGKOZeQ8UdgL1rScN+xWVxvUzQq3WubP06JhfL1aWr7Kxt7FsyiB5dY/9My roXO8NkyITVMQCTUhwlb5NLULs9wS+OjFo9DpyPFry6lPBH2hizgVGzPR3xjVn5HQ+CE xGbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WCUD7wa/UfrImVYU2GRX/0pQIO55YFU0ILpNbfg7yjE=; b=1Ekh7bN8rYbCKdKHUarFwO2L9VpIOJlE26/gkNUDPwKAmix2xZKxCwtn8tab+34lyT E8my5d5NuhpHeYn7SUp7f9vq9+QvZfq0ElksiS28pIrkRxNF1Kk7vqTiUieKTVFqRjTn hPR3HnZUg+khy8pbRdUMa9za1EXHubHz8w3oZTfVJ6MoLg4RyIW2t53Ar8CjEZoyOcgb XHmS1G/Cku9qnPlZN0pO+RoDxNpF572igID0FOCMvWa5MFc9wyNErIe650OP3p2U6qv3 B68AUl3wl2nYtlujtbsedLr0LXd6ph+bOIBNW7fhCdWBXV3FxU5cADXa6KX3XbwD5N1c JuOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=I0z6ropa; 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 x19si7793105ioa.9.2021.10.29.08.11.26; Fri, 29 Oct 2021 08:11:39 -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.20210112.gappssmtp.com header.s=20210112 header.b=I0z6ropa; 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 S230044AbhJ2PMT (ORCPT + 99 others); Fri, 29 Oct 2021 11:12:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230010AbhJ2PMP (ORCPT ); Fri, 29 Oct 2021 11:12:15 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 289B6C061714 for ; Fri, 29 Oct 2021 08:09:47 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id d13so16585532wrf.11 for ; Fri, 29 Oct 2021 08:09:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WCUD7wa/UfrImVYU2GRX/0pQIO55YFU0ILpNbfg7yjE=; b=I0z6ropag1QO0QDAkHx4XE8A8b+S+vIEOc4vVyXs+GlMZT/hpKP2qAY8FeEfgW+mXu QkDAUYHQPO8TRAa8ldvRJna+ure7x53hGhrZhCfmn3tPWmmWjBW4nCePhtdg4853hJ6I VHQ4RctOcF7G0un97tw+ZqMFmSl3vc04yLQ/3w2qGuLffzwCNXlgbaWfBLTkaBTs+kgq Xy/UPTnyEEA4pPGI3rctAE1sh3OuqZri4+KehAhfqHSzqGBZ2UUMm8pT9IgV7vUPCfGX ePtmfZs8HS1x2yXOWp4rc9UooI8Ig2cjp0Ipr/+TSUGxl37+80oSao6Gkf4VOqmEBgrU bD4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WCUD7wa/UfrImVYU2GRX/0pQIO55YFU0ILpNbfg7yjE=; b=p8eElvI0gozXyQVI8fMrGTYCAONvQPBSAF6mUIFfj/6AEIHQxc/HiCfLLYvmDxq0Cs Ps1neWp3PabjcOtpbfwySOrgtfFw9TA89zksar5hdaL1GbMm9bEqzEH28RGd6MulPGY6 xHTMAp7tPN/VGbffPn4beEtySEF11dKeTG+B3G+tHUEI1yT+/PEI1HFf3C9RkAmZP5oU G2zLRf8tACj5L19Ak380qX1Sjt6NOCTXPEE1BbhTD8oX0TYT+o0vGLUGdpLIpZrgGqoX SoZtoDYc2DDed+0Gy4VFlhiacdy8KW9P/aj9uFGcfmEuqS3D+WG+roYiJvodAE5vRq6j ctcg== X-Gm-Message-State: AOAM531qBnMIr8DcQ1rbhjdkeTriX/9AgK82ocwfyOn0LeL8Q70NZ3vj 7YfBw8tfWMeNM5ksQibaL/X8Gw== X-Received: by 2002:adf:a1d4:: with SMTP id v20mr15236363wrv.168.1635520185673; Fri, 29 Oct 2021 08:09:45 -0700 (PDT) Received: from xps-9300.baylibre.local (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id b7sm7091906wrv.72.2021.10.29.08.09.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Oct 2021 08:09:45 -0700 (PDT) From: Alexandre Bailon To: rafael@kernel.org, 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, mka@chromium.org, Alexandre Bailon Subject: [PATCH v3 2/2] thermal: add a virtual sensor to aggregate temperatures Date: Fri, 29 Oct 2021 17:08:57 +0200 Message-Id: <20211029150857.504891-3-abailon@baylibre.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211029150857.504891-1-abailon@baylibre.com> References: <20211029150857.504891-1-abailon@baylibre.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds a virtual thermal sensor driver that reads temperature from multiple hardware sensors and returns an aggregated temperature. Currently, this supports three aggregations: the minimum, maximum and average temperature. Signed-off-by: Alexandre Bailon --- drivers/thermal/Kconfig | 8 + drivers/thermal/Makefile | 1 + drivers/thermal/virtual_thermal_sensor.c | 218 +++++++++++++++++++++++ 3 files changed, 227 insertions(+) create mode 100644 drivers/thermal/virtual_thermal_sensor.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index d7f44deab5b1..b326fae5ad1d 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 VIRTUAL_THERMAL + tristate "DT-based virtual thermal sensor driver" + depends on THERMAL_OF || COMPILE_TEST + help + This option enables the generic DT-based thermal sensor aggregator. + This driver creates a thermal sensor that reads multiple hardware + sensors and aggregates their output. + config HISI_THERMAL tristate "Hisilicon thermal driver" depends on ARCH_HISI || COMPILE_TEST diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 82fc3e616e54..8bf55973059c 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_VIRTUAL_THERMAL) += virtual_thermal_sensor.o diff --git a/drivers/thermal/virtual_thermal_sensor.c b/drivers/thermal/virtual_thermal_sensor.c new file mode 100644 index 000000000000..b1c5ea308abb --- /dev/null +++ b/drivers/thermal/virtual_thermal_sensor.c @@ -0,0 +1,218 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * virtual_thermal_sensor.c - DT-based virtual thermal sensor driver. + * + * Copyright (c) 2021 BayLibre + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +struct virtual_thermal_zone_device { + struct thermal_zone_device *zone; + struct module *owner; +}; + +struct virtual_thermal_sensor { + int count; + struct virtual_thermal_zone_device *zones; + struct thermal_zone_device *tzd; + int (*aggr_temp)(int temp1, int temp2); + + struct list_head node; +}; + +static int max_temp(int temp1, int temp2) +{ + return max(temp1, temp2); +} + +static int min_temp(int temp1, int temp2) +{ + return min(temp1, temp2); +} + +static int avg_temp(int temp1, int temp2) +{ + return (temp1 + temp2) / 2; +} + +static int virtual_thermal_sensor_get_temp(void *data, int *temperature) +{ + struct virtual_thermal_sensor *sensor = data; + int max_temp = INT_MIN; + int temp; + int i; + + for (i = 0; i < sensor->count; i++) { + struct thermal_zone_device *zone; + + zone = sensor->zones[i].zone; + zone->ops->get_temp(zone, &temp); + max_temp = sensor->aggr_temp(max_temp, temp); + } + + *temperature = max_temp; + + return 0; +} + +static const struct thermal_zone_of_device_ops virtual_thermal_sensor_ops = { + .get_temp = virtual_thermal_sensor_get_temp, +}; + +int virtual_thermal_sensor_get_module(struct virtual_thermal_zone_device *zone, + const char *name) +{ + struct platform_device *sensor_pdev; + struct device_node *node; + + node = of_find_node_by_name(NULL, name); + if (!node) + return -ENODEV; + + node = of_parse_phandle(node, "thermal-sensors", 0); + if (!node) + return -ENODEV; + + sensor_pdev = of_find_device_by_node(node); + if (!sensor_pdev) + return -ENODEV; + + if (!sensor_pdev->dev.driver) + return -EPROBE_DEFER; + + if (!try_module_get(sensor_pdev->dev.driver->owner)) + return -ENODEV; + + zone->owner = sensor_pdev->dev.driver->owner; + + return 0; +} + +void virtual_thermal_sensor_put_modules(struct virtual_thermal_sensor *sensor) +{ + int i; + + for (i = 0; i < sensor->count; i++) { + if (sensor->zones[i].zone) + module_put(sensor->zones[i].owner); + } +} + +static int virtual_thermal_sensor_probe(struct platform_device *pdev) +{ + struct virtual_thermal_sensor *sensor; + struct device *dev = &pdev->dev; + struct property *prop; + const char *name; + u32 type; + int ret; + int i = 0; + + sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); + if (!sensor) + return -ENOMEM; + sensor->count = of_property_count_strings(dev->of_node, "thermal-sensors"); + if (sensor->count <= 0) + return -EINVAL; + + sensor->zones = devm_kmalloc_array(dev, sensor->count, + sizeof(*sensor->zones), + GFP_KERNEL); + if (!sensor->zones) + return -ENOMEM; + + of_property_for_each_string(dev->of_node, "thermal-sensors", prop, name) { + struct virtual_thermal_zone_device *virtual_zone; + struct thermal_zone_device *zone; + + virtual_zone = &sensor->zones[i++]; + + zone = thermal_zone_get_zone_by_name(name); + if (IS_ERR(zone)) + return PTR_ERR(zone); + + ret = virtual_thermal_sensor_get_module(virtual_zone, name); + if (ret) + goto err; + + virtual_zone->zone = zone; + } + + ret = of_property_read_u32(dev->of_node, "aggregation-function", &type); + if (ret) + return ret; + + switch (type) { + case VIRTUAL_THERMAL_SENSOR_MAX_VAL: + sensor->aggr_temp = max_temp; + break; + case VIRTUAL_THERMAL_SENSOR_MIN_VAL: + sensor->aggr_temp = min_temp; + break; + case VIRTUAL_THERMAL_SENSOR_AVG_VAL: + sensor->aggr_temp = avg_temp; + break; + default: + return -EINVAL; + } + + sensor->tzd = devm_thermal_zone_of_sensor_register(dev, 0, sensor, + &virtual_thermal_sensor_ops); + if (IS_ERR(sensor->tzd)) + return PTR_ERR(sensor->tzd); + + platform_set_drvdata(pdev, sensor); + + return 0; + +err: + virtual_thermal_sensor_put_modules(sensor); + + return ret; +} + +static int virtual_thermal_sensor_remove(struct platform_device *pdev) +{ + struct virtual_thermal_sensor *sensor; + + sensor = platform_get_drvdata(pdev); + list_del(&sensor->node); + + virtual_thermal_sensor_put_modules(sensor); + + return 0; +} + +static const struct of_device_id virtual_thermal_sensor_of_match[] = { + { + .compatible = "virtual,thermal-sensor", + }, + { + }, +}; +MODULE_DEVICE_TABLE(of, virtual_thermal_sensor_of_match); + +static struct platform_driver virtual_thermal_sensor = { + .probe = virtual_thermal_sensor_probe, + .remove = virtual_thermal_sensor_remove, + .driver = { + .name = "virtual-thermal-sensor", + .of_match_table = virtual_thermal_sensor_of_match, + }, +}; + +module_platform_driver(virtual_thermal_sensor); +MODULE_AUTHOR("Alexandre Bailon "); +MODULE_DESCRIPTION("Virtual thermal sensor"); +MODULE_LICENSE("GPL v2"); -- 2.32.0