From nobody Thu Dec 25 10:59:53 2025 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A52594C639 for ; Fri, 19 Jan 2024 11:08:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705662531; cv=none; b=bktR6Ev8UyoQPK/UVBNZRPr/h5RebK7a57BDcrctNP2q/pCSq8mi7hqKQC2t2JTxH/UDXG1KlCf5vrO9nqAZf9Zqh9C6WW0t9PiX6J4cIFPgCpFqEZBU8SDw0rWiyzaNc/54ZEJ5MsjjFY3wlyWhoNNACCEc9JdPmFblDHaLg2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705662531; c=relaxed/simple; bh=1DhpLZ/QC2ikMIv8OW+4cNzhlMmWDJJXRcZs198iPlk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jA5QoqxCtZbyZYKsj4dvKOkKQgEfhizMzAxiORVEQ9rZyywj8CA4cSQIblcV0q9qJ50TfzvYI8H5rjmycf3KlqdbuXdgcaIjShIlNTKyMO1WUBSAc+P9IFAQ0Zs1RIlCuVWYfptICaDBifKVgyOIQwCIK7SbnAWdGtubj9chqvs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=wLC3Vf1u; arc=none smtp.client-ip=209.85.218.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="wLC3Vf1u" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a2c375d2430so71131866b.1 for ; Fri, 19 Jan 2024 03:08:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1705662528; x=1706267328; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=okEOgUTTvG10NiN/AqLj194Y6CTF03Wx1MeaENsTf1o=; b=wLC3Vf1uFLuxbO6Kz244e2x6oe9f5MU4ljeQMVtcBycE6W5UtjqQWtm8SWgqCtGjFa N9zOMkI9+y6Wrfk/JUmJChcDs2codLgNGmZByIHk2k3tBoPu5o1WDP6GWFIFM4fRDuq6 4FCoPEURt4BX702K01E0HuQoXO9opzV/VygDkN6urCGOm9HGGPKcHQ16tKJ3ZlrnvqaK YO0RBlwSX9hXSaFgoIQkQ5aCDIJrEeEpV5eJzxNENEEwCVBUZ1pn427sNedmRF9BJB5k xuYT1RsjwzuhNuB/kr1Wt+eeu7OH9tIXKH5VhEIRRbbI6J8kWy91IosDlH0D2ugr/KTN lblA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705662528; x=1706267328; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=okEOgUTTvG10NiN/AqLj194Y6CTF03Wx1MeaENsTf1o=; b=nwydmDJqopJ9frjWvb3mjlizm/fKS3JXaLUrbByfAgashL5sz/nv4VsrpEEQbpmjBd TDGVx2EGpwjeiTNGSuEQfPGdSMepzunVYHumZ0Dcf5TRWGHJe/d/DFZ0rzwA/smUgNQC ssAG3x9GNLFSf8DrcaQg3vvvID1BYP9oD1MZHn/aehCQOXvzt4LOKOLim6Q5Y8tqeZU5 BI7m8zDT28FRnIVc8MYuGu1A4IyOVAXYnCTswA93Xi0rbfNxse7zTy5F0ti4J6E+wrmo 62J1qaNBHxcpEjVIbvLeGU3Mvjz7xO0kYyV+0RiEbyihPPMzN3x5GRJKP0d7j0IYqGuP LEKw== X-Gm-Message-State: AOJu0YwnFWiH6m8y6a+6RVd1gcaqqcbSulBEttBijhKCt6MJfpSRikUj rktki7e7CUOdy5Hc4nlIIuZG257fUJVsdD1ApfWyI0zFcXJxE1DDHhqDSAJZPJc= X-Google-Smtp-Source: AGHT+IExGlo4SMY6bavsiNwBVukS5zNpiw1btzBfUGvzwWyix6ifH0LNGzSKIkssylX/XCDVEEzw9g== X-Received: by 2002:a17:906:5a8d:b0:a2e:92dd:80a3 with SMTP id l13-20020a1709065a8d00b00a2e92dd80a3mr1400562ejq.123.1705662527753; Fri, 19 Jan 2024 03:08:47 -0800 (PST) Received: from blaptop.baylibre (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id h20-20020a170906261400b00a28d438a1b0sm10091661ejc.83.2024.01.19.03.08.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 03:08:46 -0800 (PST) From: Alexandre Bailon To: rafael@kernel.org, daniel.lezcano@linaro.org, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, conor+dt@kernel.org Cc: rui.zhang@intel.com, lukasz.luba@arm.com, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Bailon Subject: [PATCH v2 1/3] dt-bindings: thermal: Restore the thermal-sensors property Date: Fri, 19 Jan 2024 12:08:40 +0100 Message-ID: <20240119110842.772606-2-abailon@baylibre.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240119110842.772606-1-abailon@baylibre.com> References: <20240119110842.772606-1-abailon@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" thermal-sensors was defined in thermal.txt but when the yaml binding has been defined, its definition has changed, dropping support of multi sensors. Since we are adding support of multi sensors, use the original definition for thermal-sensors property. Signed-off-by: Alexandre Bailon Reviewed-by: Rob Herring --- Documentation/devicetree/bindings/thermal/thermal-zones.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Documentation/devicetree/bindings/thermal/thermal-zones.yaml b= /Documentation/devicetree/bindings/thermal/thermal-zones.yaml index 4a8dabc48170..b2988758d6ff 100644 --- a/Documentation/devicetree/bindings/thermal/thermal-zones.yaml +++ b/Documentation/devicetree/bindings/thermal/thermal-zones.yaml @@ -77,10 +77,9 @@ patternProperties: =20 thermal-sensors: $ref: /schemas/types.yaml#/definitions/phandle-array - maxItems: 1 description: - The thermal sensor phandle and sensor specifier used to monitor = this - thermal zone. + A list of thermal sensor phandles and sensor specifier + used while monitoring the thermal zone. =20 coefficients: $ref: /schemas/types.yaml#/definitions/uint32-array --=20 2.41.0 From nobody Thu Dec 25 10:59:53 2025 Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80D154CB38 for ; Fri, 19 Jan 2024 11:08:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705662534; cv=none; b=TsZ7K1aN60Kt2eNTkBqbOY+1ufZaWrw+u3B9AdUGuLfFsJ+VhGZEwLsiMGbMkg6cXyU62uGupswWuULU56oWEboep89BbSBfBKYcR4WXDDLSoFqOBbhCUDGGMxtxMnvC3vH2jz5/xk6ofAcIX1dJ0rf1WKJhaFRJolsoKL/Ts+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705662534; c=relaxed/simple; bh=HRG1HDvXfUhASA/toQUA5TK6BWaaBrkdi7tMwdoiI7s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E7mRGUQiJKS3lQ8whNtedkTfKKSyPgN4o901MV5imRt4zUEp7KiUQVDDYfV5M+Q5iCkJZeMWD/bYN6f5TUknc/tp3qOmaPK72KfwBsNV41LBt9TJ7aRvyqNJoXrGSCYcueZvC7QK7FG1JTIYh7ZIUlwzYfRR+5SkmFrpQ9pyo4M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=y8bOLWJK; arc=none smtp.client-ip=209.85.167.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="y8bOLWJK" Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-50e7b273352so668827e87.1 for ; Fri, 19 Jan 2024 03:08:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1705662529; x=1706267329; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OjR2LFuZQNm/ecVtMuUirJ38DZRHEGKXOqw4QzmaE6o=; b=y8bOLWJK/8FAcgtJQWxBfl/RXgRY6V/tCBKi4njhyPBnOaBcF7eHvwlnrBJLjJRa1J hjJnW4hTVikrTB1On2xnjf9GZTyFJY4txk8J5ASy0ADbNJkOW1eLL5AAq2NiI6RoOdyG l0sE19uqYZkxlbSWYoBdWTwbL6DuBDwFArvRqu+PZj2cgZjf7wDDhfLDqGPNOfh9KYJG eyx47CJxw17jGRfWBNN3CVuqtI/ADAXwVgcr+gaglDjmV7+rQM9ON9jgMqUhyIC561yN d7yg9k/fV2u40T5J7cyx/r3YYNuzih8vfi9k6SlbEVHHFXAyWIn0lliHMFla37HuByJT 5ypw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705662529; x=1706267329; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OjR2LFuZQNm/ecVtMuUirJ38DZRHEGKXOqw4QzmaE6o=; b=uh1u8a2qTDrX8xXWrdjtRTc1tvpy+TruDnj3Ar0ikMFFM0yL2ddguzWhcY2fEtcj1o Yhs01SQIiLe6X56TuDRIZqM1e4lud4Y+RjNE7vJhaooSN5WpqwRL889AKPFuSjSsuDbw WvrEiq8XEc8CvPIl60CjfYi8MqPU5UM2U9iSwzjS/RcZoj0TTbnLXW/HpC5Ir0eTCkPl GSepUiBwpviGAOKv0zJMr2Feph3C+LLquFCemleAlDHZLqT8euyJL8Yug+g9rOCBMYH8 Obqkt35r/L5paHlHBb02kDOUB6k6IjAxBkW5D+CB59qw2NpMBD1ccBAz07IUfSC5cRyP Y+Ag== X-Gm-Message-State: AOJu0YyxgNBjw2VsuKQSTZfv1VdGnxaVyRvXcej2K4ZBtA5Jf106gSQm 89nXm092eL85rYhQagG8CGZES8CQl2Jtck4b8ctLZW5KiZkReeOAkjUgRlq4IGk= X-Google-Smtp-Source: AGHT+IGmR08sudaVigkCkvIuDOcA3NhhCx2amlFjKxzmQKMiGI3DmISrs0NKDpbSBRiThecXvRsMAA== X-Received: by 2002:a19:2d4e:0:b0:50e:771a:9ff9 with SMTP id t14-20020a192d4e000000b0050e771a9ff9mr496796lft.78.1705662529382; Fri, 19 Jan 2024 03:08:49 -0800 (PST) Received: from blaptop.baylibre (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id h20-20020a170906261400b00a28d438a1b0sm10091661ejc.83.2024.01.19.03.08.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 03:08:48 -0800 (PST) From: Alexandre Bailon To: rafael@kernel.org, daniel.lezcano@linaro.org, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, conor+dt@kernel.org Cc: rui.zhang@intel.com, lukasz.luba@arm.com, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Bailon Subject: [PATCH v2 2/3] thermal: Add support of multi sensors to thermal_core Date: Fri, 19 Jan 2024 12:08:41 +0100 Message-ID: <20240119110842.772606-3-abailon@baylibre.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240119110842.772606-1-abailon@baylibre.com> References: <20240119110842.772606-1-abailon@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This adds support of multi sensors to thermal. Currently, this only support the get_temp operation. This returns an average temperature of all the sensors. If defined, a coefficient is applied to the value read from the sensor before computing the average. Signed-off-by: Alexandre Bailon --- drivers/thermal/Makefile | 1 + drivers/thermal/thermal_core.h | 7 ++ drivers/thermal/thermal_multi.c | 178 ++++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 drivers/thermal/thermal_multi.c diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index c934cab309ae..757289a406f7 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -6,6 +6,7 @@ CFLAGS_thermal_core.o :=3D -I$(src) obj-$(CONFIG_THERMAL) +=3D thermal_sys.o thermal_sys-y +=3D thermal_core.o thermal_sysfs.o thermal_sys-y +=3D thermal_trip.o thermal_helpers.o +thermal_sys-y +=3D thermal_multi.o =20 # netlink interface to manage the thermal framework thermal_sys-$(CONFIG_THERMAL_NETLINK) +=3D thermal_netlink.o diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 0a3b3ec5120b..26e83a5c8298 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -138,6 +138,13 @@ ssize_t weight_show(struct device *, struct device_att= ribute *, char *); ssize_t weight_store(struct device *, struct device_attribute *, const cha= r *, size_t); =20 +/* Multi sensors */ +int thermal_multi_sensor_validate_coeff(int *coeff, int count, int offset); +int thermal_multi_sensor_register(const char *name, + struct thermal_zone_device *sensor_tz, int coeff); +void thermal_multi_sensor_unregister(struct thermal_zone_device *sensor_tz= ); + + #ifdef CONFIG_THERMAL_STATISTICS void thermal_cooling_device_stats_update(struct thermal_cooling_device *cd= ev, unsigned long new_state); diff --git a/drivers/thermal/thermal_multi.c b/drivers/thermal/thermal_mult= i.c new file mode 100644 index 000000000000..a5a4f1f2d594 --- /dev/null +++ b/drivers/thermal/thermal_multi.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include + +#include "thermal_core.h" + +struct sensor_interface { + struct thermal_zone_device *tz; + int coeff; + + struct list_head node; +}; + +struct multi_sensor_thermal_zone { + struct thermal_zone_device *tz; + struct mutex sensors_lock; + struct list_head sensors; + + struct list_head node; +}; + +static DEFINE_MUTEX(multi_tz_mutex); +static LIST_HEAD(multi_tz_list); + +#define TJ_MAX 120000 + +static int multi_sensor_get_temp(struct thermal_zone_device *tz, int *temp) +{ + struct multi_sensor_thermal_zone *multi_tz =3D tz->devdata; + struct sensor_interface *sensor; + int accumulated_temp =3D 0; + u32 accumulated_coeff; + int ret; + + mutex_lock(&multi_tz->sensors_lock); + + if (list_empty(&multi_tz->sensors)) { + mutex_unlock(&multi_tz->sensors_lock); + return -ENODEV; + } + + list_for_each_entry(sensor, &multi_tz->sensors, node) { + ret =3D thermal_zone_get_temp(sensor->tz, temp); + if (ret) { + mutex_unlock(&multi_tz->sensors_lock); + return ret; + } + + accumulated_temp +=3D *temp * sensor->coeff; + accumulated_coeff +=3D sensor->coeff; + } + + mutex_unlock(&multi_tz->sensors_lock); + + *temp =3D accumulated_temp / accumulated_coeff; + return ret; +} + +struct thermal_zone_device_ops multi_sensor_ops =3D { + .get_temp =3D multi_sensor_get_temp, +}; + +int thermal_multi_sensor_validate_coeff(int *coeff, int count, int offset) +{ + int max_accumulated_temp =3D 0; + int i; + + for (i =3D 0; i < count; i++) { + max_accumulated_temp +=3D TJ_MAX * coeff[i]; + if (max_accumulated_temp < 0) + return -EOVERFLOW; + } + + max_accumulated_temp +=3D offset; + return max_accumulated_temp < 0 ? -EOVERFLOW : 0; +} + +static struct thermal_zone_device *multi_sensor_tz_alloc(const char *name) +{ + struct thermal_zone_device *tz; + struct thermal_zone_params tzp =3D {}; + struct multi_sensor_thermal_zone *multi_tz; + + tz =3D thermal_zone_get_zone_by_name(name); + if (!IS_ERR(tz)) { + mutex_unlock(&multi_tz_mutex); + return tz; + } + + multi_tz =3D kzalloc(sizeof(*multi_tz), GFP_KERNEL); + if (!multi_tz) + return ERR_PTR(-ENOMEM); + mutex_init(&multi_tz->sensors_lock); + INIT_LIST_HEAD(&multi_tz->sensors); + + tzp.no_hwmon =3D true; + tzp.slope =3D 1; + tzp.offset =3D 0; + + tz =3D thermal_tripless_zone_device_register(name, multi_tz, + &multi_sensor_ops, &tzp); + if (IS_ERR(tz)) { + kfree(multi_tz); + } else { + multi_tz->tz =3D tz; + list_add(&multi_tz->node, &multi_tz_list); + } + + return tz; +} + +int thermal_multi_sensor_register(const char *name, + struct thermal_zone_device *sensor_tz, int coeff) +{ + struct thermal_zone_device *tz; + struct multi_sensor_thermal_zone *multi_tz; + struct sensor_interface *sensor; + + mutex_lock(&multi_tz_mutex); + + tz =3D multi_sensor_tz_alloc(name); + if (IS_ERR(tz)) { + mutex_unlock(&multi_tz_mutex); + return PTR_ERR(tz); + } + multi_tz =3D tz->devdata; + + sensor =3D kzalloc(sizeof(*sensor), GFP_KERNEL); + if (!sensor) { + mutex_unlock(&multi_tz_mutex); + return -ENOMEM; + } + + sensor->tz =3D sensor_tz; + sensor->coeff =3D coeff; + mutex_lock(&multi_tz->sensors_lock); + list_add(&sensor->node, &multi_tz->sensors); + mutex_unlock(&multi_tz->sensors_lock); + + thermal_zone_device_enable(tz); + + mutex_unlock(&multi_tz_mutex); + + return 0; +} + +void thermal_multi_sensor_unregister(struct thermal_zone_device *sensor_tz) +{ + struct multi_sensor_thermal_zone *multi_tz; + struct sensor_interface *sensor, *tmp; + + mutex_lock(&multi_tz_mutex); + list_for_each_entry(multi_tz, &multi_tz_list, node) { + mutex_lock(&multi_tz->sensors_lock); + list_for_each_entry_safe(sensor, tmp, &multi_tz->sensors, node) { + if (sensor->tz =3D=3D sensor_tz) { + list_del(&sensor->node); + kfree(sensor); + break; + } + } + + if (list_empty(&multi_tz->sensors)) { + thermal_zone_device_unregister(multi_tz->tz); + mutex_unlock(&multi_tz->sensors_lock); + kfree(multi_tz); + } else { + mutex_unlock(&multi_tz->sensors_lock); + } + } + mutex_unlock(&multi_tz_mutex); +} --=20 2.41.0 From nobody Thu Dec 25 10:59:53 2025 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB26B4CDE0 for ; Fri, 19 Jan 2024 11:08:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705662534; cv=none; b=q8vr6sIs43X/aHyARYG+6nzQXNnuXgWSpTGiKajAYwi1IcMMRRNNIDaKglwW8jUbQtwRCRNclyaJZik3Ar2+vhkOLQyuFY1tMTpmrKBqzkQ1LpDpygYU/QQ5uilRufmNwT6GxXvyjaI/58Gp01riFGmIkEdwTus73Em3Vi9I8hQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705662534; c=relaxed/simple; bh=b/1cDuU1FPYFRLGVprzXpiPsw0rIQZ8Z7rTjJT6ZLPI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LGVNz7MIORya4Do3JI8BjFZdn17mC7sNVDYVz5vIccEXeOL6z3ThgVA5XztRLl+050FmheUviF6893ujvFfb7mUxVHedBWq4oY5BtlJQudrAwF1Tjier0+aOXK4CM9KDphsqtu5PDE/9OJGdULj39LuR3Uh6oAI/5WHzcSy0q+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=R5oRRDYq; arc=none smtp.client-ip=209.85.218.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="R5oRRDYq" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a2f22bfb4e6so62437966b.0 for ; Fri, 19 Jan 2024 03:08:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1705662531; x=1706267331; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dkXNmIyIg21DM466Px7ATM2iJxIu/N6buiuxFMSlXzc=; b=R5oRRDYqeSE6E2BSnEElXqR8Dvbmekn64eyTgNK9+4YS9XqdkfqXfg1EQKmfmZmiJd PKhZXvksi2Gx+8vQbyJ+k/LL3PnAlNgW7HSb6L7o0vgDyabgbyT1xtX0o5t5fPk8ybV6 A8swsJ6ZRcJe/CQPnfBvHOe0YE/EUF6VWMW20Tmqstj9poTWY6Bv7B6i8ZjRpLeLpYVb xEQolSJOuVW68fKTCZZCph5/R/1M8It13Sag8UF6YZTRZVMF+7RUWrDJBtD9XBRgDDLd ixZ+9AouJ7lDL25FJqauxFbTTGWAPCLekQsYv/B7iZ00BbDuBM2G/f30X/CbRqFsxeca cYqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705662531; x=1706267331; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dkXNmIyIg21DM466Px7ATM2iJxIu/N6buiuxFMSlXzc=; b=SswBEFkjll43JIONyM4bH/i+j/IZmPCUV/zabHyzGpdIZOrtXZzQ1qr+d7M4L9ssnd 82/njMhjr/B7xO/iQ8PlYIY6jDzC9aGiYsNrf02G3dBsqkJeHSe59e5am5kOydc4azVm wyFY82LWlYI9IF9Onc6aEl+ItYc0BHD6JTZXQEMN0iMxqfA2rrlF5CcGESuHyuvqMvaO kBMix9E8ByLPJ5J6qR9TNqi8RoqXJjGAfJICHL32BQ4nX4v2KzxatrHiZAUPE3/r50P/ R9yEwgYecmDKbPBfR/r4hT6pDbTU2EooHrfB04A61FCQxPk1gMQ74fG8hkTS6HXZRyg/ ABhw== X-Gm-Message-State: AOJu0YzUTbj6sd3pK1CBn8v5B6oUKwpuly+Air5MdnuB4ps7hODFvaiT NDRCzMeJ/hUqe9NBgzv0N26QTsot6RaWv6dU3GhasCMPyrUmMzqABRvRQ46PrUVDBIpH43SCCMO 1 X-Google-Smtp-Source: AGHT+IEcel0/MRjzIWp/N/wJ0VcDTRpDNsY2OCnzcyjdM0dOAhpKdX8F/+naKtz1jCQ7v01Qmo8KNA== X-Received: by 2002:a17:906:da04:b0:a2b:55a:9e13 with SMTP id fi4-20020a170906da0400b00a2b055a9e13mr1483087ejb.11.1705662530991; Fri, 19 Jan 2024 03:08:50 -0800 (PST) Received: from blaptop.baylibre (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id h20-20020a170906261400b00a28d438a1b0sm10091661ejc.83.2024.01.19.03.08.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 03:08:50 -0800 (PST) From: Alexandre Bailon To: rafael@kernel.org, daniel.lezcano@linaro.org, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, conor+dt@kernel.org Cc: rui.zhang@intel.com, lukasz.luba@arm.com, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Bailon Subject: [PATCH v2 3/3] thermal: Add support of multi sensors to thermal_of Date: Fri, 19 Jan 2024 12:08:42 +0100 Message-ID: <20240119110842.772606-4-abailon@baylibre.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240119110842.772606-1-abailon@baylibre.com> References: <20240119110842.772606-1-abailon@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This updates thermal_of to support more than one sensor. If during the registration we find another thermal zone referencing this sensors and some other, then we create the multi sensor thermal zone (if it doesn't exist) and register the sensor to it. Signed-off-by: Alexandre Bailon --- drivers/thermal/thermal_of.c | 139 +++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c index 1e0655b63259..3f36d8a3d8e8 100644 --- a/drivers/thermal/thermal_of.c +++ b/drivers/thermal/thermal_of.c @@ -441,12 +441,146 @@ static void thermal_of_zone_unregister(struct therma= l_zone_device *tz) struct thermal_trip *trips =3D tz->trips; struct thermal_zone_device_ops *ops =3D tz->ops; =20 + thermal_multi_sensor_unregister(tz); thermal_zone_device_disable(tz); thermal_zone_device_unregister(tz); kfree(trips); kfree(ops); } =20 +int thermal_of_get_sensor_id(struct device_node *tz_np, + struct device_node *sensor_np, + int phandle_index, u32 *id) +{ + struct of_phandle_args sensor_specs; + int ret; + + ret =3D of_parse_phandle_with_args(tz_np, + "thermal-sensors", + "#thermal-sensor-cells", + phandle_index, + &sensor_specs); + if (ret) + return ret; + + if (sensor_specs.np !=3D sensor_np) { + of_node_put(sensor_specs.np); + return -ENODEV; + } + + if (sensor_specs.args_count > 1) + pr_warn("%pOFn: too many cells in sensor specifier %d\n", + sensor_specs.np, sensor_specs.args_count); + + *id =3D sensor_specs.args_count ? sensor_specs.args[0] : 0; + of_node_put(sensor_specs.np); + + return 0; +} + +static int thermal_of_has_sensor_id(struct device_node *tz_np, + struct device_node *sensor_np, + u32 sensor_id) +{ + int count; + int i; + + count =3D of_count_phandle_with_args(tz_np, + "thermal-sensors", + "#thermal-sensor-cells"); + if (count <=3D 0) + return -ENODEV; + + for (i =3D 0; i < count; i++) { + int ret; + u32 id; + + ret =3D thermal_of_get_sensor_id(tz_np, sensor_np, i, &id); + if (ret) + return ret; + + if (id =3D=3D sensor_id) + return i; + + } + + return -ENODEV; +} + +static int thermal_of_register_mutli_sensor(struct device_node *sensor, in= t id, + struct thermal_zone_device *tz, + struct device_node *tz_np) +{ + struct device_node *child; + u32 *coeff; + int ret; + int i; + + /* + * Go through all the thermal zone and check if the sensor is + * referenced. If so, find or create a multi sensor thermal zone + * and register the sensor to it. + */ + for_each_available_child_of_node(of_get_parent(tz_np), child) { + int count; + int index; + int offset; + + /* Skip the tz that is currently registering */ + if (child =3D=3D tz_np) + continue; + + /* Test if the sensor is referenced by a tz*/ + index =3D thermal_of_has_sensor_id(child, sensor, id); + if (index < 0) + continue; + + /* + * Get the coefficients and offset and assign them to the + * multi sensor thermal zone. + */ + count =3D of_count_phandle_with_args(child, + "thermal-sensors", + "#thermal-sensor-cells"); + coeff =3D kmalloc_array(count, sizeof(*coeff), GFP_KERNEL); + if (!coeff) + goto err; + + for (i =3D 0; i < count; i++) { + ret =3D of_property_read_u32_index(child, + "coefficients", + i, coeff + i); + if (ret) + coeff[i] =3D 1; + } + + ret =3D of_property_read_u32_index(child, "coefficients", + count, &offset); + if (ret) + offset =3D 0; + + /* Make sure the coeff and offset won't cause an overflow */ + ret =3D thermal_multi_sensor_validate_coeff(coeff, count, offset); + if (ret) + goto err_free_coeff; + + ret =3D thermal_multi_sensor_register(child->name, tz, + coeff[index]); + if (ret) + goto err_free_coeff; + kfree(coeff); + } + + return 0; + +err_free_coeff: + kfree(coeff); +err: + thermal_multi_sensor_unregister(tz); + + return ret; +} + /** * thermal_of_zone_register - Register a thermal zone with device node * sensor @@ -528,6 +662,11 @@ static struct thermal_zone_device *thermal_of_zone_reg= ister(struct device_node * return ERR_PTR(ret); } =20 + /* Register the sensor to all other thermal zone referencing it */ + ret =3D thermal_of_register_mutli_sensor(sensor, id, tz, np); + if (ret) + pr_warn("Failed to register a sensor to a multi sensor tz\n"); + return tz; =20 out_kfree_trips: --=20 2.41.0