Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2558086rwb; Mon, 3 Oct 2022 02:31:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4olXZIT+fWd/AgVeEhbF5Qjf+QXO34wQaZg6w7K0VinVHozDPvpbXAqGSjr6MkQ2jm+a5/ X-Received: by 2002:a05:6402:11cc:b0:458:f610:8404 with SMTP id j12-20020a05640211cc00b00458f6108404mr4094024edw.75.1664789518306; Mon, 03 Oct 2022 02:31:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664789518; cv=none; d=google.com; s=arc-20160816; b=xVH8PVj+XF717elb4D6TGn50EoJ7bylS24zqUUaUkNubVLVCCwTXEkrKIEHXDp4IDb JuPQ4YHlvqm8JplA6B5CNslRXkdsnTQlU1gcmkIL2b/IxuKvi814LKxsWwNhVPcVOXUB 0ClrJNdQjMA7bJnYyJgPdcyqwpirEAl12+nx8/JFYLaZEj/VHedldnL5A0+oTbR0U50P yRyY7eYIR/mzzchltTQY3NtQvD7Yk6uHCGU9oDi2bJF3D4RWHFExqadU51o8IuDNDA4C ixxfFD+SJOl7E75Hz0mM3zHkqz+2OB4fFsi/cLlbyGzbop4ycTUQrdUhqwa25Em0jn79 SloA== 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=x5YnRpxQlPTxi3ib1GcmFVKg7aq0OaYheolOuxRT8AU=; b=zOkmZjzVUJCT0nu3nMKLJjy8Mp22yTWnsQFCyOmVrKd4vG2JWPIJq+Gzd+N9m/EEcK Bzuy/1qYQeKABNFHJDx1aQAHT4jHULY1UamQ1QE9989Zk7h84DUGihtYXZCxHLZWzyL7 h4yPy0QuKVFJV8P+vGfiJ1mVZ8OVElE7d9NOiLAcBUYfmxnWNNWiqH8A2yhbFK+yHkia crJqrfoq91ZEREOxZHN1lzJ2lqH8XztcTOX7cJpTSoG5nyp1hJ1+9JpA3nGGSy+UJl/2 jOzLUg57WUHjDE1FNcfi+To0O3EbKxQNicXe8pDK+r2eUqCAHvZNGfWDHysivoyINfgU M5gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NEuw9ALz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js6-20020a17090797c600b0072a84586c63si7578174ejc.564.2022.10.03.02.31.30; Mon, 03 Oct 2022 02:31:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NEuw9ALz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229661AbiJCJ1b (ORCPT + 99 others); Mon, 3 Oct 2022 05:27:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231321AbiJCJ0u (ORCPT ); Mon, 3 Oct 2022 05:26:50 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17E85349A2 for ; Mon, 3 Oct 2022 02:26:36 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id o5so6616591wms.1 for ; Mon, 03 Oct 2022 02:26:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=x5YnRpxQlPTxi3ib1GcmFVKg7aq0OaYheolOuxRT8AU=; b=NEuw9ALzlbh0Ugwj0RJ5qGD4F0uuBJ48wpE3HF/O9a2wcxKFEnYRWE2PvytS55RaFW 50nQEgJFXpk/HrzGvAlIacEsWszbT9vG0yFd9ODCkNUkwnOpV56kXKmEWQk8XzikZkQO 9vcwRXBSj9suVCyQWfx3gCzIpC9hxnH9mzHaw/95V6n/Hgmmvorac98TDEEQAyaZQLLM /1vSIfSa6WWoCxrrd58sQX9w06Z0iUQH8APaIlWBZkGDlDqbYBzf2+wzXFZf0j/R3Qwh 1eE71IzXGhebaUVFO9sxcLxF7Lytd9L9lPBxBmCDDJ5bN9jk9R9bc/5xD+B6N578LeR7 e53w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=x5YnRpxQlPTxi3ib1GcmFVKg7aq0OaYheolOuxRT8AU=; b=NRMFj7xvJPePtiWD1e2ePujdr787QUfh4/L+SlWyVVdHfy2v2gqQ3plO+B+2uJ8zQ/ MX/Px7bwV52TwuyE4iONairM0riBC3SfbKGMW8htLK2PEUED7BrAVbCXdk69eutnpLn/ zIA6N3tv30Iz7ew4Zov4eEmrZaWsjjFWRlSKCxg87TqDXUlIhLWYjUoC2jTCdaV58M8/ +9+OMN5eZUv74GEJ+6ct+0L7qj8xuDsoVGOy1DIsp7TSzKMTU9DimdgqcqT2BmKJCofu FAYKVwmZlWvj72o4tP2e8XwoYQ4wdZqnKkm1UBiMx97yCYATo8uSN0IxgDCUYx/ZbGG/ pdSg== X-Gm-Message-State: ACrzQf0PM9mU2iQ4s4oDbDHxF4R2bJEu5oMtTSe6FSqdJvhLqEMxKExV VFusyuZ6KJFS/P/2Zv5g0iNZPA== X-Received: by 2002:a05:600c:19cd:b0:3b9:af1f:1b3b with SMTP id u13-20020a05600c19cd00b003b9af1f1b3bmr2343088wmq.37.1664789183890; Mon, 03 Oct 2022 02:26:23 -0700 (PDT) Received: from mai.. ([2a05:6e02:1041:c10:f3a:9f5e:1605:a75a]) by smtp.gmail.com with ESMTPSA id ay3-20020a5d6f03000000b0022cc157bf26sm9707520wrb.85.2022.10.03.02.26.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 02:26:23 -0700 (PDT) From: Daniel Lezcano To: daniel.lezcano@linaro.org, rafael@kernel.org Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, rui.zhang@intel.com, Raju Rangoju , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Peter Kaestle , Hans de Goede , Mark Gross , Miquel Raynal , Amit Kucheria , Nicolas Saenz Julienne , Broadcom Kernel Team , Florian Fainelli , Ray Jui , Scott Branden , Support Opensource , Lukasz Luba , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Thara Gopinath , Andy Gross , Bjorn Andersson , =?UTF-8?q?Niklas=20S=C3=B6derlund?= , Bartlomiej Zolnierkiewicz , Krzysztof Kozlowski , Alim Akhtar , Thierry Reding , Jonathan Hunter , Eduardo Valentin , Keerthy , Kunihiko Hayashi , Masami Hiramatsu , Antoine Tenart , Srinivas Pandruvada , Dmitry Osipenko , netdev@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-samsung-soc@vger.kernel.org, linux-tegra@vger.kernel.org, linux-omap@vger.kernel.org Subject: [PATCH v8 03/29] thermal/core: Add a generic thermal_zone_set_trip() function Date: Mon, 3 Oct 2022 11:25:36 +0200 Message-Id: <20221003092602.1323944-4-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221003092602.1323944-1-daniel.lezcano@linaro.org> References: <20221003092602.1323944-1-daniel.lezcano@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The thermal zone ops defines a set_trip callback where we can invoke the backend driver to set an interrupt for the next trip point temperature being crossed the way up or down, or setting the low level with the hysteresis. The ops is only called from the thermal sysfs code where the userspace has the ability to modify a trip point characteristic. With the effort of encapsulating the thermal framework core code, let's create a thermal_zone_set_trip() which is the writable side of the thermal_zone_get_trip() and put there all the ops encapsulation. Signed-off-by: Daniel Lezcano --- V8: - pretty one line condition and parenthesis removal (Rafael J. Wysocki) --- drivers/thermal/thermal_core.c | 46 +++++++++++++++++++++++++++++ drivers/thermal/thermal_sysfs.c | 52 +++++++++++---------------------- include/linux/thermal.h | 3 ++ 3 files changed, 66 insertions(+), 35 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 16ef91dc102f..3a9915824e67 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1211,6 +1211,52 @@ int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, } EXPORT_SYMBOL_GPL(thermal_zone_get_trip); +int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, + const struct thermal_trip *trip) +{ + struct thermal_trip t; + int ret = -EINVAL; + + mutex_lock(&tz->lock); + + if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips) + goto out; + + ret = __thermal_zone_get_trip(tz, trip_id, &t); + if (ret) + goto out; + + if (t.type != trip->type) { + ret = -EINVAL; + goto out; + } + + if (t.temperature != trip->temperature && tz->ops->set_trip_temp) { + ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature); + if (ret) + goto out; + } + + if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) { + ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis); + if (ret) + goto out; + } + + if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis)) + tz->trips[trip_id] = *trip; +out: + mutex_unlock(&tz->lock); + + if (!ret) { + thermal_notify_tz_trip_change(tz->id, trip_id, trip->type, + trip->temperature, trip->hysteresis); + thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED); + } + + return ret; +} + /** * thermal_zone_device_register_with_trips() - register a new thermal zone device * @type: the thermal zone device type diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index 6c45194aaabb..8d7b25ab67c2 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -115,32 +115,19 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr, struct thermal_trip trip; int trip_id, ret; - if (!tz->ops->set_trip_temp && !tz->trips) - return -EPERM; - if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1) return -EINVAL; - if (kstrtoint(buf, 10, &trip.temperature)) - return -EINVAL; - - if (tz->ops->set_trip_temp) { - ret = tz->ops->set_trip_temp(tz, trip_id, trip.temperature); - if (ret) - return ret; - } - - if (tz->trips) - tz->trips[trip_id].temperature = trip.temperature; - ret = thermal_zone_get_trip(tz, trip_id, &trip); if (ret) return ret; - thermal_notify_tz_trip_change(tz->id, trip_id, trip.type, - trip.temperature, trip.hysteresis); + if (kstrtoint(buf, 10, &trip.temperature)) + return -EINVAL; - thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + ret = thermal_zone_set_trip(tz, trip_id, &trip); + if (ret) + return ret; return count; } @@ -168,29 +155,24 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct thermal_zone_device *tz = to_thermal_zone(dev); - int trip, ret; - int temperature; - - if (!tz->ops->set_trip_hyst) - return -EPERM; + struct thermal_trip trip; + int trip_id, ret; - if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1) + if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1) return -EINVAL; - if (kstrtoint(buf, 10, &temperature)) - return -EINVAL; + ret = thermal_zone_get_trip(tz, trip_id, &trip); + if (ret) + return ret; - /* - * We are not doing any check on the 'temperature' value - * here. The driver implementing 'set_trip_hyst' has to - * take care of this. - */ - ret = tz->ops->set_trip_hyst(tz, trip, temperature); + if (kstrtoint(buf, 10, &trip.hysteresis)) + return -EINVAL; - if (!ret) - thermal_zone_set_trips(tz); + ret = thermal_zone_set_trip(tz, trip_id, &trip); + if (ret) + return ret; - return ret ? ret : count; + return count; } static ssize_t diff --git a/include/linux/thermal.h b/include/linux/thermal.h index feb8b61df746..66373f872237 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -338,6 +338,9 @@ static inline void devm_thermal_of_zone_unregister(struct device *dev, int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, struct thermal_trip *trip); +int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, + const struct thermal_trip *trip); + int thermal_zone_get_num_trips(struct thermal_zone_device *tz); int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp); -- 2.34.1