From patchwork Fri Sep 17 07:27:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Bailon X-Patchwork-Id: 12501313 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.7 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,URIBL_BLOCKED,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 10770C433F5 for ; Fri, 17 Sep 2021 07:25:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DE4B8610C8 for ; Fri, 17 Sep 2021 07:25:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239423AbhIQH1I (ORCPT ); Fri, 17 Sep 2021 03:27:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239272AbhIQH1H (ORCPT ); Fri, 17 Sep 2021 03:27:07 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF682C061764 for ; Fri, 17 Sep 2021 00:25:45 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id s24so6536181wmh.4 for ; Fri, 17 Sep 2021 00:25:45 -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=iAiAjfHULvSkEXET8f5wVsDWre5TWnookf/FICWyQDE=; b=ixOao/HZulX8ebkoTGjNSpcFgJG1iRiIvduv1Y7eHivIuEo3c2ap3UpWC3Az1Lg4NB wNFg1nG4mr0dBQLMW9MmBx3ueLUb1QvhQsjMG7+VNQ3d32TQ9qoW/87VpOPToT64Vt8O SjZfHh9gF1Ls6dhJRxS7JTJInCuSgmR+vd8Sj449yXJJNbk1EowhOiJbVi0Dr0pWrj2N /J+658YxnTuREpMnpZWC7tHMuZgyqyHNuNdEuz0VoOi+mR9mukLJwnzuAan30TOVsvgV b/+bEV1gHrGiJiK18b6/EC6HZpgcmpSa+dOUr6axiuP3WI4hzgIWZw4/8n4mBkLO24nP siYQ== 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=iAiAjfHULvSkEXET8f5wVsDWre5TWnookf/FICWyQDE=; b=bZ0SrV1Gpny6/OV3mfw3QeSJ7mXOGU4tk591ofzOCysc/+y0WcQEp98NGRRvDPEw9V JDbZiX97xeQJYPHPlvOc+BBRvOW+6zRtQRThDflf7fQj7XFxtGvkM/UydibGZV9iCcvy CGC8Bf8PMDYGjuxNpPE0Yy1rE2rNwv3NOn1zrlobpWvfO3zlqx5EZMnVEP26Sn0hyDhp 2xJdXlLUKYNxWvz0G6wh0zDcg2DmEHH2uXSt/eHFIIe5+9IdKDHlsskCx5Q7iV2GaLIu 2Ao19A1DEYqFGieQqcdAscqJQU/QXBSjeHS0kT2Zs2dJL+hyWpDxnnw9RVKIl5we49OX 65xA== X-Gm-Message-State: AOAM532xZaa7TeO5V0wr/7bO8kHdllvl2RJuaDgpPERBMB9H95ROSqbf bXfA/hmpHA7GLqV/SPpcwTM5Rw== X-Google-Smtp-Source: ABdhPJy8TEPcqC/LfpgvUg2g+F4guhtaGSbnKEk6mfPFIJuLJuNTSqsJWxCvZ8/1Qs15kf3OZxRmWA== X-Received: by 2002:a7b:c107:: with SMTP id w7mr8895517wmi.91.1631863544393; Fri, 17 Sep 2021 00:25:44 -0700 (PDT) Received: from localhost.baylibre.local (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id z19sm11777565wma.0.2021.09.17.00.25.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Sep 2021 00:25: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, mka@chromium.org, Alexandre Bailon Subject: [PATCH v2 1/2] dt-bindings: Add bindings for the virtual thermal sensor Date: Fri, 17 Sep 2021 09:27:31 +0200 Message-Id: <20210917072732.611140-2-abailon@baylibre.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210917072732.611140-1-abailon@baylibre.com> References: <20210917072732.611140-1-abailon@baylibre.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org This adds the device tree bidings for the virtual thermal sensor. The virtual sensor could be used to a temperature computed from many thermal sensors. Signed-off-by: Alexandre Bailon --- .../thermal/virtual,thermal-sensor.yaml | 67 +++++++++++++++++++ include/dt-bindings/thermal/virtual-sensor.h | 15 +++++ 2 files changed, 82 insertions(+) create mode 100644 Documentation/devicetree/bindings/thermal/virtual,thermal-sensor.yaml create mode 100644 include/dt-bindings/thermal/virtual-sensor.h diff --git a/Documentation/devicetree/bindings/thermal/virtual,thermal-sensor.yaml b/Documentation/devicetree/bindings/thermal/virtual,thermal-sensor.yaml new file mode 100644 index 0000000000000..049620ed88a5b --- /dev/null +++ b/Documentation/devicetree/bindings/thermal/virtual,thermal-sensor.yaml @@ -0,0 +1,67 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +# Copyright 2021 BayLibre +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/thermal/thermal-sensor.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Virtual thermal sensor binding + +description: | + The virtual thermal sensor devices provide temperature sensing capabilities + based on hardware thermal sensors. Basically, this could be used to get the + maximum, minimum or average temperature of the hardware thermal sensors. +properties: + "#thermal-sensor-cells": + description: + Used to uniquely identify a thermal sensor instance within an IC. Will be + 0 on sensor nodes with only a single sensor and at least 1 on nodes + containing several internal sensors. + enum: [0, 1] + + aggregation-function: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + Used to select the operations to perform on the sensors to get the virtual + sensor temperature. + enum: + - VIRTUAL_THERMAL_SENSOR_MIN + - VIRTUAL_THERMAL_SENSOR_MAX + - VIRTUAL_THERMAL_SENSOR_AVG + + thermal-sensors: + $ref: /schemas/types.yaml#/definitions/phandle-array + description: + The thermal sensor phandle and sensor specifier used to monitor this + thermal zone. + +required: + - "#thermal-sensor-cells" + - aggregation-function + - thermal-sensors + +additionalProperties: true + +examples: + - | + #include + #include + #include + #include + + lvts: lvts@1100b000 { + compatible = "mediatek,mt6873-lvts"; + reg = <0x1100b000 0x1000>; + clocks = <&infracfg CLK_INFRA_THERM>; + clock-names = "lvts_clk"; + #thermal-sensor-cells = <0>; + interrupts = ; + }; + + soc_max_sensor: soc_max_sensor { + compatible = "virtual,thermal-sensor"; + #thermal-sensor-cells = <1>; + aggregation-function = ; + thermal-sensors = <&lvts 0>, <&lvts 1>; + }; +... diff --git a/include/dt-bindings/thermal/virtual-sensor.h b/include/dt-bindings/thermal/virtual-sensor.h new file mode 100644 index 0000000000000..93f6dc2453b85 --- /dev/null +++ b/include/dt-bindings/thermal/virtual-sensor.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * This header provides constants for virtual thermal sensor bindings. + * + * Copyright (C) 2021 BayLibre + */ + +#ifndef _DT_BINDINGS_THERMAL_VIRTUAL_SENSOR_H +#define _DT_BINDINGS_THERMAL_VIRTUAL_SENSOR_H + +#define VIRTUAL_THERMAL_SENSOR_MIN 0 +#define VIRTUAL_THERMAL_SENSOR_MAX 1 +#define VIRTUAL_THERMAL_SENSOR_AVG 2 + +#endif /* _DT_BINDINGS_THERMAL_VIRTUAL_SENSOR_H */ From patchwork Fri Sep 17 07:27:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Bailon X-Patchwork-Id: 12501315 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.7 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,URIBL_BLOCKED,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 BF532C4332F for ; Fri, 17 Sep 2021 07:25:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9F3046113A for ; Fri, 17 Sep 2021 07:25:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239717AbhIQH1J (ORCPT ); Fri, 17 Sep 2021 03:27:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234295AbhIQH1I (ORCPT ); Fri, 17 Sep 2021 03:27:08 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3C86C061574 for ; Fri, 17 Sep 2021 00:25:46 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id b21-20020a1c8015000000b003049690d882so9179304wmd.5 for ; Fri, 17 Sep 2021 00:25:46 -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=rKpMtPJV4c7fVpnQEhWgPVwySf6pvGBSBB6MRM4juX0=; b=vxlHtsI8MrgKZ1jumZbkHAT3aT1Ya8U+iG+fJzIab+KLF9G/6RvOl20OM2yQNQ/kuN LD8KJDmDZxrFUF8KgmcP020TCdPIuYetuF5AOmx8iM78iOqO6/dObgvZ9NBlKi/pogVc sA8Pg0WwKgohJ9/AZShFNbVKMt9AsXu6o/WDu5N4z5K7u13LN0XQo6NUvLdefnYKPLDO pUC5Fm5DOHUPnmSn/jojSXYsSWPNHYhXBmzlOv5a6PHtBXcvWW8GSDS3b9DTx6U4miUC CFvBRi3cGzglpBuyML/5MVCf0pP33v/ijoVjk3PL4ZZnB0skpkqcVOES3Ty0puxfS9zG i84A== 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=rKpMtPJV4c7fVpnQEhWgPVwySf6pvGBSBB6MRM4juX0=; b=7L74Ziq4duE5Ua/RrxzFmrNfEOGvV8gwi6JGX0sRdhXCApBU/oNs73xr6XQcliq9UE 2LWTrw/t2V5Fjc4Hv9HRCrJowJ6scL2v6tfyzkOPl5F6GAg36D9lXOSb/fV0+zQOc/uA ccEl7jzBRJTExNLXoHOMlMUB869ZbhLBT+J14un/jtfU4UieImQ8jRsCBvS07hpOth7V XSl6NEu1JRBlg4a9/rIIbkCAidK4HS0RUBTI3jDXeP+pA8DyL6XxyjTsuT8DQMTtHaxL Mg1ouEHwAYl1/Mf8VUUJ08W8LQ5bOMmHYD7YZqPT7y651UIzxwwBffvRtnaeFOc58BqO BfWg== X-Gm-Message-State: AOAM531BAKlmN/wt7BftAD+MImLErRMvraAF84kSa/mCaTv3cjkiKNnv OtHNmab7N9pYAmAD3uS+7HlVGQ== X-Google-Smtp-Source: ABdhPJy4G0wz2dgdPltmEPJRRfQq8LRjVdHP8NNZ81y2E/gbHcSs7OZ2aPO5tFT3tNRMzAGx96JBZw== X-Received: by 2002:a1c:9888:: with SMTP id a130mr13771570wme.102.1631863545546; Fri, 17 Sep 2021 00:25:45 -0700 (PDT) Received: from localhost.baylibre.local (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id z19sm11777565wma.0.2021.09.17.00.25.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Sep 2021 00:25:45 -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, mka@chromium.org, Alexandre Bailon , kernel test robot Subject: [PATCH v2 2/2] thermal: add a virtual sensor to aggregate temperatures Date: Fri, 17 Sep 2021 09:27:32 +0200 Message-Id: <20210917072732.611140-3-abailon@baylibre.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210917072732.611140-1-abailon@baylibre.com> References: <20210917072732.611140-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 supports three operations: the minimum, maximum and average temperature. Reported-by: kernel test robot Signed-off-by: Alexandre Bailon --- drivers/thermal/Kconfig | 8 + drivers/thermal/Makefile | 1 + drivers/thermal/virtual-thermal-sensor.h | 54 ++++ drivers/thermal/virtual_thermal_sensor.c | 350 +++++++++++++++++++++++ 4 files changed, 413 insertions(+) create mode 100644 drivers/thermal/virtual-thermal-sensor.h create mode 100644 drivers/thermal/virtual_thermal_sensor.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index d7f44deab5b1d..20bc93c48f5b1 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 "Virtual thermal sensor driver" + depends on THERMAL_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 82fc3e616e54b..8bf55973059c5 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.h b/drivers/thermal/virtual-thermal-sensor.h new file mode 100644 index 0000000000000..3bbf7c324dddc --- /dev/null +++ b/drivers/thermal/virtual-thermal-sensor.h @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2021 BayLibre + */ + +#ifndef __THERMAL_VIRTUAL_SENSOR_H__ +#define __THERMAL_VIRTUAL_SENSOR_H__ + +#include +#include + +struct virtual_thermal_sensor; +struct thermal_sensor_data; + +#if IS_ENABLED(CONFIG_VIRTUAL_THERMAL) +struct thermal_sensor_data * +thermal_virtual_sensor_register(struct device *dev, int sensor_id, void *data, + const struct thermal_zone_of_device_ops *ops); +void thermal_virtual_sensor_unregister(struct device *dev, + struct thermal_sensor_data *sensor_data); +struct thermal_sensor_data * +devm_thermal_virtual_sensor_register(struct device *dev, int sensor_id, void *data, + const struct thermal_zone_of_device_ops *ops); + +void devm_thermal_virtual_sensor_unregister(struct device *dev, + struct virtual_thermal_sensor *sensor); +#else +static inline struct thermal_sensor_data * +thermal_virtual_sensor_register(struct device *dev, int sensor_id, void *data, + const struct thermal_zone_of_device_ops *ops) +{ + return ERR_PTR(-ENODEV); +} + +void thermal_virtual_sensor_unregister(struct device *dev, + struct thermal_sensor_data *sensor_data) +{ +} + +static inline struct thermal_sensor_data * +devm_thermal_virtual_sensor_register(struct device *dev, int sensor_id, void *data, + const struct thermal_zone_of_device_ops *ops) +{ + return ERR_PTR(-ENODEV); +} + +static inline +void devm_thermal_virtual_sensor_unregister(struct device *dev, + struct virtual_thermal_sensor *sensor) +{ +} +#endif + +#endif /* __THERMAL_VIRTUAL_SENSOR_H__ */ diff --git a/drivers/thermal/virtual_thermal_sensor.c b/drivers/thermal/virtual_thermal_sensor.c new file mode 100644 index 0000000000000..234563af6643e --- /dev/null +++ b/drivers/thermal/virtual_thermal_sensor.c @@ -0,0 +1,350 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2021 BayLibre + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "virtual-thermal-sensor.h" + +struct thermal_sensor_data { + struct list_head node; + + /* sensor interface */ + int id; + void *sensor_data; + const struct thermal_zone_of_device_ops *ops; +}; + +struct virtual_thermal_sensor { + int count; + struct thermal_sensor_data *sensors; + struct thermal_zone_device *tzd; + int (*aggr_temp)(int temp1, int temp2); + + struct list_head node; +}; + +static LIST_HEAD(thermal_sensors); +static LIST_HEAD(virtual_sensors); + +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) / 2) + ((temp2) / 2) + (((temp1) % 2 + (temp2) % 2) / 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_sensor_data *hw_sensor; + + hw_sensor = &sensor->sensors[i]; + if (!hw_sensor->ops) + return -ENODEV; + + hw_sensor->ops->get_temp(hw_sensor->sensor_data, &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, +}; + +static int virtual_sensor_add_sensor(struct virtual_thermal_sensor *sensor, + struct of_phandle_args args, + int index) +{ + struct thermal_sensor_data *sensor_data; + int id; + + list_for_each_entry(sensor_data, &thermal_sensors, node) { + id = args.args_count ? args.args[0] : 0; + if (sensor_data->id == id) { + memcpy(&sensor->sensors[index], sensor_data, + sizeof(*sensor_data)); + return 0; + } + } + + return -ENODEV; +} + +static int virtual_thermal_sensor_probe(struct platform_device *pdev) +{ + struct virtual_thermal_sensor *sensor; + struct device *dev = &pdev->dev; + struct of_phandle_args args; + u32 type; + int ret; + int i; + + sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); + if (!sensor) + return -ENOMEM; + + sensor->count = of_count_phandle_with_args(dev->of_node, + "thermal-sensors", + "#thermal-sensor-cells"); + if (sensor->count <= 0) + return -EINVAL; + + sensor->sensors = devm_kmalloc_array(dev, sensor->count, + sizeof(*sensor->sensors), + GFP_KERNEL); + if (!sensor->sensors) + return -ENOMEM; + + for (i = 0; i < sensor->count; i++) { + ret = of_parse_phandle_with_args(dev->of_node, + "thermal-sensors", + "#thermal-sensor-cells", + i, + &args); + if (ret) + return ret; + + ret = virtual_sensor_add_sensor(sensor, args, i); + if (ret) + return ret; + } + + ret = of_property_read_u32(dev->of_node, "aggregation-function", &type); + if (ret) + return ret; + + switch (type) { + case VIRTUAL_THERMAL_SENSOR_MAX: + sensor->aggr_temp = max_temp; + break; + case VIRTUAL_THERMAL_SENSOR_MIN: + sensor->aggr_temp = min_temp; + break; + case VIRTUAL_THERMAL_SENSOR_AVG: + 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); + list_add(&sensor->node, &virtual_sensors); + + return 0; +} + +static int virtual_thermal_sensor_remove(struct platform_device *pdev) +{ + struct virtual_thermal_sensor *sensor; + + sensor = platform_get_drvdata(pdev); + list_del(&sensor->node); + + 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, + }, +}; + +/** + * thermal_virtual_sensor_register - registers a sensor that could be a virtual + * sensor + * @dev: a valid struct device pointer of a sensor device. Must contain + * a valid .of_node, for the sensor node. + * @sensor_id: a sensor identifier, in case the sensor IP has more + * than one sensor + * @data: a private pointer (owned by the caller) that will be passed + * back, when a temperature reading is needed. + * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. + * + * This function will register a thermal sensor to make it available for later + * usage by a virtual sensor. + * + * The thermal zone temperature is provided by the @get_temp function + * pointer. When called, it will have the private pointer @data back. + * + * Return: On success returns a valid struct thermal_zone_device, + * otherwise, it returns a corresponding ERR_PTR(). Caller must + * check the return value with help of IS_ERR() helper. + */ +struct thermal_sensor_data *thermal_virtual_sensor_register( + struct device *dev, int sensor_id, void *data, + const struct thermal_zone_of_device_ops *ops) +{ + struct thermal_sensor_data *sensor_data; + + sensor_data = devm_kzalloc(dev, sizeof(*sensor_data), GFP_KERNEL); + if (!sensor_data) + return ERR_PTR(-ENOMEM); + + sensor_data->id = sensor_id; + sensor_data->sensor_data = data; + sensor_data->ops = ops; + + list_add(&sensor_data->node, &thermal_sensors); + + return sensor_data; +} +EXPORT_SYMBOL_GPL(thermal_virtual_sensor_register); + +/** + * thermal_virtual_sensor_unregister - unregisters a sensor + * @dev: a valid struct device pointer of a sensor device. + * @sensor_data: a pointer to struct thermal_sensor_data to unregister. + * + * This function removes the sensor from the list of available thermal sensors. + * If the sensor is in use, then the next call to .get_temp will return -ENODEV. + */ +void thermal_virtual_sensor_unregister(struct device *dev, + struct thermal_sensor_data *sensor_data) +{ + struct thermal_sensor_data *sd; + struct virtual_thermal_sensor *sensor; + int i; + + list_del(&sensor_data->node); + + list_for_each_entry(sensor, &virtual_sensors, node) { + for (i = 0; i < sensor->count; i++) { + sd = &sensor->sensors[i]; + if (sd->id == sensor_data->id && + sd->sensor_data == sensor_data->sensor_data) { + sd->ops = NULL; + } + } + } +} +EXPORT_SYMBOL_GPL(thermal_virtual_sensor_unregister); + +static void devm_thermal_virtual_sensor_release(struct device *dev, void *res) +{ + thermal_virtual_sensor_unregister(dev, + *(struct thermal_sensor_data **)res); +} + +static int devm_thermal_virtual_sensor_match(struct device *dev, void *res, + void *data) +{ + struct thermal_sensor_data **r = res; + + if (WARN_ON(!r || !*r)) + return 0; + + return *r == data; +} + +/** + * devm_thermal_virtual_sensor_register - Resource managed version of + * thermal_virtual_sensor_register() + * @dev: a valid struct device pointer of a sensor device. Must contain + * a valid .of_node, for the sensor node. + * @sensor_id: a sensor identifier, in case the sensor IP has more + * than one sensor + * @data: a private pointer (owned by the caller) that will be passed + * back, when a temperature reading is needed. + * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. + * + * Refer thermal_zone_of_sensor_register() for more details. + * + * Return: On success returns a valid struct virtual_sensor_data, + * otherwise, it returns a corresponding ERR_PTR(). Caller must + * check the return value with help of IS_ERR() helper. + * Registered virtual_sensor_data device will automatically be + * released when device is unbound. + */ +struct thermal_sensor_data *devm_thermal_virtual_sensor_register( + struct device *dev, int sensor_id, + void *data, const struct thermal_zone_of_device_ops *ops) +{ + struct thermal_sensor_data **ptr, *sensor_data; + + ptr = devres_alloc(devm_thermal_virtual_sensor_release, sizeof(*ptr), + GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + sensor_data = thermal_virtual_sensor_register(dev, sensor_id, data, ops); + if (IS_ERR(sensor_data)) { + devres_free(ptr); + return sensor_data; + } + + *ptr = sensor_data; + devres_add(dev, ptr); + + return sensor_data; +} +EXPORT_SYMBOL_GPL(devm_thermal_virtual_sensor_register); + +/** + * devm_thermal_virtual_sensor_unregister - Resource managed version of + * thermal_virtual_sensor_unregister(). + * @dev: Device for which resource was allocated. + * @sensor: a pointer to struct thermal_zone_device where the sensor is registered. + * + * This function removes the sensor from the list of sensors registered with + * devm_thermal_virtual_sensor_register() API. + * Normally this function will not need to be called and the resource + * management code will ensure that the resource is freed. + */ +void devm_thermal_virtual_sensor_unregister(struct device *dev, + struct virtual_thermal_sensor *sensor) +{ + WARN_ON(devres_release(dev, devm_thermal_virtual_sensor_release, + devm_thermal_virtual_sensor_match, sensor)); +} +EXPORT_SYMBOL_GPL(devm_thermal_virtual_sensor_unregister); + +module_platform_driver(virtual_thermal_sensor); +MODULE_AUTHOR("Alexandre Bailon "); +MODULE_DESCRIPTION("Virtual thermal sensor"); +MODULE_LICENSE("GPL v2");