From patchwork Tue May 17 14:26:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Stein X-Patchwork-Id: 1632402 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=nuKKGG6F; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=e7Ga3th8; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4L2djR5tZrz9s2R for ; Wed, 18 May 2022 00:26:51 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241347AbiEQO0s (ORCPT ); Tue, 17 May 2022 10:26:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239516AbiEQO0r (ORCPT ); Tue, 17 May 2022 10:26:47 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D9C742A27; Tue, 17 May 2022 07:26:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797605; x=1684333605; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rFECBIxcXYfz0pDlsSadifgxEhqSYOY8Lf0coZ96Zww=; b=nuKKGG6Fmckp0yg8UB/VO46qFQRtgNY/sQGxXsguvK1DDuxkv8HxiQGv 5NXg1Cis597+ZmK5DCsFgmpCJD3PsfpkbaDO0QYNR8uqOvwpnVXTg1GLZ hUNjMzi36mVetAJYLD5UFhb4X1IKhvm5e/hW6xFjCDq+LUs3hmza6lHKw 5LdUGlf8AEzmCwF6EMtPF5WOQYJazYTNVmQH8YHmPSq8Wt0q+56GHyyqN enaGJa0KHEZC1VfNlF7d6+S9U28SXKISztq/AShoQ5JWXKYdSpjNkAFwd 7le8ie7YaXDVR8S6m6WvD8pyguRFLJsKLdRPucv9XHFaYWit/t1fvSLaN Q==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925932" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 17 May 2022 16:26:26 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Tue, 17 May 2022 16:26:26 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Tue, 17 May 2022 16:26:26 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797586; x=1684333586; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rFECBIxcXYfz0pDlsSadifgxEhqSYOY8Lf0coZ96Zww=; b=e7Ga3th8KCe2NGmRvKbCy8sIXHlMKYT21CIY0oBhhM33hfKfpyo9jfIU fO8MgEPZwMSxMFIEh+OMtiHUi0vv0ARDdYIjkk/tSLIXdNt24XstlyMHK KMOMINxQOaWyJREVSidAWlo9BHWL8MGXAVS5dwef6zcZ4LUG95CeL9Ftd 00kvUIb2bVwz5Yblpl2Y6erCSKoEgBuvQbp8Jz9T3/UrxcpKyYQo1RSDT epKT73NJdA6LdgDygNQm4t9pC+aevqm+TGAONuGmQr7wtYlsRaZyyIfhj N9OpAWqMf35mWvXvxXZC3886mfzmhjU/rMkRZi1bwIH9oDOhHAh3QFN/I Q==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925926" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 17 May 2022 16:26:25 +0200 Received: from steina-w.tq-net.de (unknown [10.123.49.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by vtuxmail01.tq-net.de (Postfix) with ESMTPSA id 327AC280075; Tue, 17 May 2022 16:26:25 +0200 (CEST) From: Alexander Stein To: Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck , Jonathan Corbet , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Lee Jones Cc: Alexander Stein , linux-hwmon@vger.kernel.org, linux-pwm@vger.kernel.org, Markus Niebel Subject: [PATCH v3 1/6] hwmon: pwm-fan: Refactor fan power on/off Date: Tue, 17 May 2022 16:26:15 +0200 Message-Id: <20220517142620.1523143-2-alexander.stein@ew.tq-group.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> References: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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-pwm@vger.kernel.org In preparation for dynamically switching regulator, split the power on and power off sequence into separate functions. Signed-off-by: Alexander Stein --- drivers/hwmon/pwm-fan.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 6c08551d8d14..458914d01155 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -82,23 +82,47 @@ static void sample_timer(struct timer_list *t) mod_timer(&ctx->rpm_timer, jiffies + HZ); } -static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) +static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) { + struct pwm_state *state = &ctx->pwm_state; unsigned long period; + int ret; + + period = state->period; + state->duty_cycle = DIV_ROUND_UP(ctx->pwm_value * (period - 1), MAX_PWM); + state->enabled = true; + ret = pwm_apply_state(ctx->pwm, state); + + return ret; +} + +static int pwm_fan_power_off(struct pwm_fan_ctx *ctx) +{ + struct pwm_state state; + + state.enabled = false; + state.duty_cycle = 0; + pwm_apply_state(ctx->pwm, &state); + + return 0; +} + +static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) +{ int ret = 0; - struct pwm_state *state = &ctx->pwm_state; mutex_lock(&ctx->lock); if (ctx->pwm_value == pwm) goto exit_set_pwm_err; - period = state->period; - state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); - state->enabled = pwm ? true : false; + if (pwm > 0) + ret = pwm_fan_power_on(ctx); + else + ret = pwm_fan_power_off(ctx); - ret = pwm_apply_state(ctx->pwm, state); if (!ret) ctx->pwm_value = pwm; + exit_set_pwm_err: mutex_unlock(&ctx->lock); return ret; From patchwork Tue May 17 14:26:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Stein X-Patchwork-Id: 1632403 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=mQd+rkZe; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=qXgFBIDR; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4L2djT0wpYz9s5V for ; Wed, 18 May 2022 00:26:53 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348881AbiEQO0w (ORCPT ); Tue, 17 May 2022 10:26:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239516AbiEQO0s (ORCPT ); Tue, 17 May 2022 10:26:48 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5BB742A31; Tue, 17 May 2022 07:26:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797607; x=1684333607; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RHY+Uqy+B8OW1co9+ZEM3AZ7L4FCao6DaFnGqdfR86A=; b=mQd+rkZebzMbWIOQQYcdi+AWYK4XPD79LPq2hZeM9ClC5XgNjJ9Sa5pR 3Rcz4GvKQZYUr2MxZktM1Eg3h5BjP1SI+aMYWTYR6BTe21HKqmNjZa3Ka dosYB9/Px/fV9Xz/5FyXtHotTI4dVar3zsOJDsHZg6QhSAP8ppXBxIvAK p185KaT7nQQI6w2geZKKa1fYvQKAzFMARFyjcwxyEmAB1H460MWP5b+4I SZY+wkKIwuSzTdmi8xq2Otvq+1MhwAa/Gp86TrnNWivYJvciBrAqsTMtA U9xzCmgLDSUkWawbRG5k32b5twNsjZHlBTiTv8eelvMHpcI76gMPSB66+ w==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925938" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 17 May 2022 16:26:35 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Tue, 17 May 2022 16:26:35 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Tue, 17 May 2022 16:26:35 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797595; x=1684333595; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RHY+Uqy+B8OW1co9+ZEM3AZ7L4FCao6DaFnGqdfR86A=; b=qXgFBIDR3R7e/MpZ3qn1frxZWSFXS8hGSgppNcJC6CAjSyD6Ravt3v5t FC9p+H7ull8G+5gwMouePzxrVIBsLFJuNUSRahRjbv8Fn+W4pkDdZeySC 1sVSak2LbVvPIX39H0xx0Dnjxm3A5InBh9cDvmKRQU3YnbSsDOQr4gxVm QSmIloHen9bXkPEAceU0jpA5Gc18h5NbGr2cEoNNSkKIFxC+D7b0Zwbqo AbsidZYxlCFUB1hhH4mYsdAYmsmvu70B6fAeTMzXsNTSTHHVSmtkDKzDm ZQoJ2si8+3yu1Ad4lR51n0WyF7GAvCdBRDVk/E7+j3qIOv2nkXjhlKy50 w==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925929" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 17 May 2022 16:26:25 +0200 Received: from steina-w.tq-net.de (unknown [10.123.49.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by vtuxmail01.tq-net.de (Postfix) with ESMTPSA id 648A0280078; Tue, 17 May 2022 16:26:25 +0200 (CEST) From: Alexander Stein To: Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck , Jonathan Corbet , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Lee Jones Cc: Alexander Stein , linux-hwmon@vger.kernel.org, linux-pwm@vger.kernel.org, Markus Niebel Subject: [PATCH v3 2/6] hwmon: pwm-fan: Simplify enable/disable check Date: Tue, 17 May 2022 16:26:16 +0200 Message-Id: <20220517142620.1523143-3-alexander.stein@ew.tq-group.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> References: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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-pwm@vger.kernel.org Instead of comparing the current to the new pwm duty to decide whether to enable the PWM, use a dedicated flag. Also apply the new PWM duty in any case. This is a preparation to enable/disable the regulator dynamically. Signed-off-by: Alexander Stein --- drivers/hwmon/pwm-fan.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 458914d01155..2b7b26ab9b88 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -29,10 +29,13 @@ struct pwm_fan_tach { }; struct pwm_fan_ctx { + struct device *dev; + struct mutex lock; struct pwm_device *pwm; struct pwm_state pwm_state; struct regulator *reg_en; + bool enabled; int tach_count; struct pwm_fan_tach *tachs; @@ -85,14 +88,21 @@ static void sample_timer(struct timer_list *t) static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) { struct pwm_state *state = &ctx->pwm_state; - unsigned long period; int ret; - period = state->period; - state->duty_cycle = DIV_ROUND_UP(ctx->pwm_value * (period - 1), MAX_PWM); + if (ctx->enabled) + return 0; + state->enabled = true; ret = pwm_apply_state(ctx->pwm, state); + if (ret) { + dev_err(ctx->dev, "failed to enable PWM\n"); + goto err; + } + ctx->enabled = true; + +err: return ret; } @@ -100,26 +110,36 @@ static int pwm_fan_power_off(struct pwm_fan_ctx *ctx) { struct pwm_state state; + if (!ctx->enabled) + return 0; + state.enabled = false; state.duty_cycle = 0; pwm_apply_state(ctx->pwm, &state); + ctx->enabled = false; + return 0; } static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) { + struct pwm_state *state = &ctx->pwm_state; + unsigned long period; int ret = 0; mutex_lock(&ctx->lock); - if (ctx->pwm_value == pwm) - goto exit_set_pwm_err; - if (pwm > 0) + if (pwm > 0) { + period = state->period; + state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); + ret = pwm_apply_state(ctx->pwm, state); + if (ret) + goto exit_set_pwm_err; ret = pwm_fan_power_on(ctx); - else + } else { ret = pwm_fan_power_off(ctx); - + } if (!ret) ctx->pwm_value = pwm; @@ -326,6 +346,7 @@ static int pwm_fan_probe(struct platform_device *pdev) mutex_init(&ctx->lock); + ctx->dev = &pdev->dev; ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL); if (IS_ERR(ctx->pwm)) return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n"); From patchwork Tue May 17 14:26:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Stein X-Patchwork-Id: 1632405 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=H9T5hbE1; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=l746PcLa; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4L2djT5vWwz9s2R for ; Wed, 18 May 2022 00:26:53 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348834AbiEQO0x (ORCPT ); Tue, 17 May 2022 10:26:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348836AbiEQO0t (ORCPT ); Tue, 17 May 2022 10:26:49 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46AE942A3C; Tue, 17 May 2022 07:26:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797607; x=1684333607; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mDjd1Czri8IG4oOu2iAnNPTxvmKzQtBIO6FCC4N5SZc=; b=H9T5hbE1qbAxAPRczbjRqgrgl+0Hh3j6ezZasZ2oCiVMuwc1U7cJe35T 0I3RKN7OJZTLNCIdINa62tXNzw5gohxQOXlc8FSkNPF9ig4eVCdiT9VVQ U2UM8fGix23uZjYtGoAkKbsExJK60rdXxMdzlMbHwjlPGdU7YKt1DW4cq ggK6LrXAOgIDRqg0mvxdl7rmp4P9Iv8eY+pBV2BOBrwbg55ZNtNYFRfOe umMsc/NQ/NTluPahFSwAIBWt8FHPpUvbUfq4GfaD+JKFU3twaXSAJgeR2 6IxoCZ9Dav52JeZhKEhsw3sydrHoKtB9hvfFktQ9I+gvQrooQ2vg9umiJ Q==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925940" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 17 May 2022 16:26:35 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Tue, 17 May 2022 16:26:35 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Tue, 17 May 2022 16:26:35 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797595; x=1684333595; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mDjd1Czri8IG4oOu2iAnNPTxvmKzQtBIO6FCC4N5SZc=; b=l746PcLaj/s2K1yir+PBsTXlOfL1gOOSV9+AFEMJtOMlKSUVnuw15C9M QcWkGlw+opFjqOzXYT01oStp1mgwSa8yJZpDFUr1URups427wrDqXGmtJ oUhTIeo8lENTIn4ELoq6N1iXz4ApcvGXPBP+YWk92hiclRDtsJtujBujE Ar9WHAYQnHQdtUUuScmNvM/XYj27SRZ63SaRdepTbTTJSN9kMS+3h21QL scIWXWbrGtMtAKv1KZVorOHE6Suk+bo4R9v5QzJAncyDWSZ1ORuCbv2Z7 eXQ7tjlV7xsZov+A3CRy6DoMSIwz42uHzTkBIsX7cf5QTGptFe4NchzIm g==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925931" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 17 May 2022 16:26:25 +0200 Received: from steina-w.tq-net.de (unknown [10.123.49.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by vtuxmail01.tq-net.de (Postfix) with ESMTPSA id 997CC28007C; Tue, 17 May 2022 16:26:25 +0200 (CEST) From: Alexander Stein To: Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck , Jonathan Corbet , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Lee Jones Cc: Alexander Stein , linux-hwmon@vger.kernel.org, linux-pwm@vger.kernel.org, Markus Niebel Subject: [PATCH v3 3/6] hwmon: pwm-fan: Dynamically switch off regulator if PWM duty is 0 Date: Tue, 17 May 2022 16:26:17 +0200 Message-Id: <20220517142620.1523143-4-alexander.stein@ew.tq-group.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> References: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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-pwm@vger.kernel.org Instead of powering the PWM fan all the time, turn off the regulator if the PWM is disabled. This is especially important for fan using inverted PWM signal polarity. Having regulator supplied and PWM disabled, some PWM controllers provide the active, rather than inactive signal. With this change the shutdown as well as suspend/resume paths require modifcations as well. Signed-off-by: Alexander Stein --- drivers/hwmon/pwm-fan.c | 91 ++++++++++++----------------------------- 1 file changed, 27 insertions(+), 64 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 2b7b26ab9b88..c757af514ede 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -93,16 +93,25 @@ static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) if (ctx->enabled) return 0; + ret = regulator_enable(ctx->reg_en); + if (ret < 0) { + dev_err(ctx->dev, "failed to enable power supply\n"); + return ret; + } + state->enabled = true; ret = pwm_apply_state(ctx->pwm, state); if (ret) { dev_err(ctx->dev, "failed to enable PWM\n"); - goto err; + goto disable_regulator; } ctx->enabled = true; -err: + return 0; + +disable_regulator: + regulator_disable(ctx->reg_en); return ret; } @@ -117,6 +126,8 @@ static int pwm_fan_power_off(struct pwm_fan_ctx *ctx) state.duty_cycle = 0; pwm_apply_state(ctx->pwm, &state); + regulator_disable(ctx->reg_en); + ctx->enabled = false; return 0; @@ -314,18 +325,12 @@ static int pwm_fan_of_get_cooling_data(struct device *dev, return 0; } -static void pwm_fan_regulator_disable(void *data) -{ - regulator_disable(data); -} - -static void pwm_fan_pwm_disable(void *__ctx) +static void pwm_fan_cleanup(void *__ctx) { struct pwm_fan_ctx *ctx = __ctx; - ctx->pwm_state.enabled = false; - pwm_apply_state(ctx->pwm, &ctx->pwm_state); del_timer_sync(&ctx->rpm_timer); + pwm_fan_power_off(ctx); } static int pwm_fan_probe(struct platform_device *pdev) @@ -359,16 +364,6 @@ static int pwm_fan_probe(struct platform_device *pdev) return PTR_ERR(ctx->reg_en); ctx->reg_en = NULL; - } else { - ret = regulator_enable(ctx->reg_en); - if (ret) { - dev_err(dev, "Failed to enable fan supply: %d\n", ret); - return ret; - } - ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable, - ctx->reg_en); - if (ret) - return ret; } pwm_init_state(ctx->pwm, &ctx->pwm_state); @@ -383,14 +378,17 @@ static int pwm_fan_probe(struct platform_device *pdev) return -EINVAL; } - /* Set duty cycle to maximum allowed and enable PWM output */ + /* + * Set duty cycle to maximum allowed and enable PWM output as well as + * the regulator. In case of error nothing is changed + */ ret = __set_pwm(ctx, MAX_PWM); if (ret) { dev_err(dev, "Failed to configure PWM: %d\n", ret); return ret; } timer_setup(&ctx->rpm_timer, sample_timer, 0); - ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx); + ret = devm_add_action_or_reset(dev, pwm_fan_cleanup, ctx); if (ret) return ret; @@ -496,61 +494,26 @@ static int pwm_fan_probe(struct platform_device *pdev) return 0; } -static int pwm_fan_disable(struct device *dev) -{ - struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); - int ret; - - if (ctx->pwm_value) { - /* keep ctx->pwm_state unmodified for pwm_fan_resume() */ - struct pwm_state state = ctx->pwm_state; - - state.duty_cycle = 0; - state.enabled = false; - ret = pwm_apply_state(ctx->pwm, &state); - if (ret < 0) - return ret; - } - - if (ctx->reg_en) { - ret = regulator_disable(ctx->reg_en); - if (ret) { - dev_err(dev, "Failed to disable fan supply: %d\n", ret); - return ret; - } - } - - return 0; -} - static void pwm_fan_shutdown(struct platform_device *pdev) { - pwm_fan_disable(&pdev->dev); + struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); + + pwm_fan_cleanup(ctx); } #ifdef CONFIG_PM_SLEEP static int pwm_fan_suspend(struct device *dev) { - return pwm_fan_disable(dev); + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + + return pwm_fan_power_off(ctx); } static int pwm_fan_resume(struct device *dev) { struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); - int ret; - - if (ctx->reg_en) { - ret = regulator_enable(ctx->reg_en); - if (ret) { - dev_err(dev, "Failed to enable fan supply: %d\n", ret); - return ret; - } - } - - if (ctx->pwm_value == 0) - return 0; - return pwm_apply_state(ctx->pwm, &ctx->pwm_state); + return __set_pwm(ctx, ctx->pwm_value); } #endif From patchwork Tue May 17 14:26:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Stein X-Patchwork-Id: 1632406 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=HaAPxOpI; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=E/swFrcX; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4L2djV2M9Mz9s5V for ; Wed, 18 May 2022 00:26:54 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348836AbiEQO0x (ORCPT ); Tue, 17 May 2022 10:26:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348855AbiEQO0u (ORCPT ); Tue, 17 May 2022 10:26:50 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B98F42A27; Tue, 17 May 2022 07:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797608; x=1684333608; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uoe6C6uZ9UgWU6o7KK3hz1/ZIrJ6jOKQYwm+FlWuZ14=; b=HaAPxOpIuPJqCyWHCQm+yvQYrF9iRmVOlJucSLJnMUilynP7+xUrEbEE K07zJwwlKtVPftkGNZejh3nWNv5VvfGrvaMFxvI3jHBSW0MPi+9/gQ5/S zo842V8vZQxYilRKJH1Qo84jzp0B2y2Ic3P3U7z2/W7V4w80457ByAIPA 5oy7zFQGM7vCdByjI6a81qN9sPafTDCii5hWkp+FGq9bLzTNOEZOLK0so mzjGxigjlqXRIDeUQ8kt1vUFAyYORvBffScqHwOm6/8OXIoW3OQjVAeLA DkKTRlpf6oTMFeODo05dDe2g9/Cpc6ixahD2L0kbT9zpbZhDX+2/qASe1 g==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925939" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 17 May 2022 16:26:35 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Tue, 17 May 2022 16:26:35 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Tue, 17 May 2022 16:26:35 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797595; x=1684333595; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uoe6C6uZ9UgWU6o7KK3hz1/ZIrJ6jOKQYwm+FlWuZ14=; b=E/swFrcXJOcRTgUEGG+1Ax8YTwFN7aH4yrlawsDK/S0f7bLCh6HKBY86 vxkEQ2Vtt7rjjPQnzW+3ptBNvKZi1gF4n6zwJP5FEXRt/YBfV2OOrOJQy GMi6f06DUGNVyc6WVAYlCBChq3y4Q9sAJi1BqEeHxMgSzTSZ/ToPeNGh1 Uy5ojz8VM60Jk73e1St0CaJzjNViznKI1qHPjjvdQF67q6muPJ+f6MAQG 9umkJEcMdHR7mVTVb2WVkdeE5jumLAnYfIIiVJ5WzUrZW7th3mQkHtPcH jFlaYejPUpi+lJ+ClhvPVUuTnSUsEnYhbC2OZg/1t+ngJ8Vfum8KvH8yO w==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925933" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 17 May 2022 16:26:26 +0200 Received: from steina-w.tq-net.de (unknown [10.123.49.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by vtuxmail01.tq-net.de (Postfix) with ESMTPSA id CAC23280070; Tue, 17 May 2022 16:26:25 +0200 (CEST) From: Alexander Stein To: Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck , Jonathan Corbet , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Lee Jones Cc: Alexander Stein , linux-hwmon@vger.kernel.org, linux-pwm@vger.kernel.org, Markus Niebel Subject: [PATCH v3 4/6] hwmon: pwm-fan: Remove internal duplicated pwm_state Date: Tue, 17 May 2022 16:26:18 +0200 Message-Id: <20220517142620.1523143-5-alexander.stein@ew.tq-group.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> References: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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-pwm@vger.kernel.org Each pwm device has already a pwm_state. Use this one instead of managing an own copy of it. Signed-off-by: Alexander Stein --- drivers/hwmon/pwm-fan.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index c757af514ede..21bfd0e931ba 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -33,7 +33,6 @@ struct pwm_fan_ctx { struct mutex lock; struct pwm_device *pwm; - struct pwm_state pwm_state; struct regulator *reg_en; bool enabled; @@ -87,7 +86,7 @@ static void sample_timer(struct timer_list *t) static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) { - struct pwm_state *state = &ctx->pwm_state; + struct pwm_state state; int ret; if (ctx->enabled) @@ -99,8 +98,9 @@ static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) return ret; } - state->enabled = true; - ret = pwm_apply_state(ctx->pwm, state); + pwm_get_state(ctx->pwm, &state); + state.enabled = true; + ret = pwm_apply_state(ctx->pwm, &state); if (ret) { dev_err(ctx->dev, "failed to enable PWM\n"); goto disable_regulator; @@ -122,6 +122,7 @@ static int pwm_fan_power_off(struct pwm_fan_ctx *ctx) if (!ctx->enabled) return 0; + pwm_get_state(ctx->pwm, &state); state.enabled = false; state.duty_cycle = 0; pwm_apply_state(ctx->pwm, &state); @@ -135,16 +136,17 @@ static int pwm_fan_power_off(struct pwm_fan_ctx *ctx) static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) { - struct pwm_state *state = &ctx->pwm_state; + struct pwm_state state; unsigned long period; int ret = 0; mutex_lock(&ctx->lock); if (pwm > 0) { - period = state->period; - state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); - ret = pwm_apply_state(ctx->pwm, state); + pwm_get_state(ctx->pwm, &state); + period = state.period; + state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); + ret = pwm_apply_state(ctx->pwm, &state); if (ret) goto exit_set_pwm_err; ret = pwm_fan_power_on(ctx); @@ -338,6 +340,7 @@ static int pwm_fan_probe(struct platform_device *pdev) struct thermal_cooling_device *cdev; struct device *dev = &pdev->dev; struct pwm_fan_ctx *ctx; + struct pwm_state state; struct device *hwmon; int ret; const struct hwmon_channel_info **channels; @@ -366,18 +369,25 @@ static int pwm_fan_probe(struct platform_device *pdev) ctx->reg_en = NULL; } - pwm_init_state(ctx->pwm, &ctx->pwm_state); + pwm_init_state(ctx->pwm, &state); /* * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned * long. Check this here to prevent the fan running at a too low * frequency. */ - if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) { + if (state.period > ULONG_MAX / MAX_PWM + 1) { dev_err(dev, "Configured period too big\n"); return -EINVAL; } + /* Apply modified PWM default state */ + ret = pwm_apply_state(ctx->pwm, &state); + if (ret) { + dev_err(dev, "failed to apply initial PWM state: %d\n", ret); + return -EINVAL; + } + /* * Set duty cycle to maximum allowed and enable PWM output as well as * the regulator. In case of error nothing is changed From patchwork Tue May 17 14:26:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Stein X-Patchwork-Id: 1632407 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=pA0RU4nN; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=i4C8OLJp; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4L2djV6tQGz9s2R for ; Wed, 18 May 2022 00:26:54 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348855AbiEQO0y (ORCPT ); Tue, 17 May 2022 10:26:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348868AbiEQO0u (ORCPT ); Tue, 17 May 2022 10:26:50 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C800D42A3D; Tue, 17 May 2022 07:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797609; x=1684333609; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s9Y16Fi0agI4kORhbSwpsI13j5cDz6fUFfAn4VgiYiQ=; b=pA0RU4nNtmkwCDVxaZfhxREJnmJoJHxBNNwt6kUAHS7DPRC7/jybMA24 fI7igQ+Sr8mrKJmKWleC7J206CiM73eE200qA85W9oMd7NweYaij98zzk CUAN0Zp8FZo4zrvJvw8Wm13wVDIv0zvnVP+KqmxmcMSRi06SEB66MOzFv 5UESbb8zAVW7b0cOEKmaPi4CcXIKWSLDunTZeDq+96MnUOeUv2Z5vPTIb rEAaGFIac17dp/CvQSxJ5Bl9o9JLU9wOoCsL+C4nmAO8t1BVJVtk31opG d9EsungJ+/fMuepFIoU213f++1SsRSDcqu3e8vR/cu+phnkeSgwoSUhUf g==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925941" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 17 May 2022 16:26:35 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Tue, 17 May 2022 16:26:35 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Tue, 17 May 2022 16:26:35 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797595; x=1684333595; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s9Y16Fi0agI4kORhbSwpsI13j5cDz6fUFfAn4VgiYiQ=; b=i4C8OLJpEuYalziNKXFZjd7WEAuHcOJO22cxfmvTT1YJoAHaLkqtG3Te aYlKaHCckYdZDoI8jHdDr/U5IdRY4AKdkLSJkii5BmFER6q026Yuduir2 HqJZ3TePRosJgSftNp4BDV+M4MxtBOoFvTxIyV06HuAhnqfT1/I+nliwZ h+Qvp5PqGHOgp3AwO78vni9FYmdM+M4vi7skd5DRdO9D99APiwJFBnaql VRvrJMqzySw9TnED0u7es/FFPhhDXYCUm/rm+7t0HQbeYjVBxXjSxB3+D gaGt6kEOJ7kvnKw80ALeMKFs4AsKmt+gZ535ARlGhT/zwMHQZxMb/vm1q Q==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925934" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 17 May 2022 16:26:26 +0200 Received: from steina-w.tq-net.de (unknown [10.123.49.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by vtuxmail01.tq-net.de (Postfix) with ESMTPSA id 1EFAE280075; Tue, 17 May 2022 16:26:26 +0200 (CEST) From: Alexander Stein To: Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck , Jonathan Corbet , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Lee Jones Cc: Alexander Stein , linux-hwmon@vger.kernel.org, linux-pwm@vger.kernel.org, Markus Niebel Subject: [PATCH v3 5/6] hwmon: pwm-fan: Move PWM enable into separate function Date: Tue, 17 May 2022 16:26:19 +0200 Message-Id: <20220517142620.1523143-6-alexander.stein@ew.tq-group.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> References: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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-pwm@vger.kernel.org This allows reusage in other code paths. Signed-off-by: Alexander Stein --- drivers/hwmon/pwm-fan.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 21bfd0e931ba..9ebe958cc908 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -84,9 +84,17 @@ static void sample_timer(struct timer_list *t) mod_timer(&ctx->rpm_timer, jiffies + HZ); } -static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) +static int pwm_fan_enable_pwm(struct pwm_fan_ctx *ctx) { struct pwm_state state; + + pwm_get_state(ctx->pwm, &state); + state.enabled = true; + return pwm_apply_state(ctx->pwm, &state); +} + +static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) +{ int ret; if (ctx->enabled) @@ -98,9 +106,7 @@ static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) return ret; } - pwm_get_state(ctx->pwm, &state); - state.enabled = true; - ret = pwm_apply_state(ctx->pwm, &state); + ret = pwm_fan_enable_pwm(ctx); if (ret) { dev_err(ctx->dev, "failed to enable PWM\n"); goto disable_regulator; From patchwork Tue May 17 14:26:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Stein X-Patchwork-Id: 1632404 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=WHLWwAW+; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.a=rsa-sha256 header.s=key1 header.b=I+LhpO8I; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4L2djT3YrCz9s75 for ; Wed, 18 May 2022 00:26:53 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239516AbiEQO0w (ORCPT ); Tue, 17 May 2022 10:26:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348834AbiEQO0s (ORCPT ); Tue, 17 May 2022 10:26:48 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0C1342A39; Tue, 17 May 2022 07:26:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797607; x=1684333607; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uAQEobNYq5e7zqIN9kNTGCuqIpHhyYNp3YJZCqF6qww=; b=WHLWwAW+n+xUzP/VO06O+GQVFKgvqDm1Wt7ih1ZscQc+YhWa2g/L98Ub Ba9Nexqbk5JjgvRnr2tnJgMcmzo2YzSlBHCsU9Rulh62IrtNuys3I5ld3 YDjJCNO61zDEdjjCvXeyXGCtmel2sZqKhoh79P+lJzyKw1+JDaHjG/4ze ILdODpVWLiiYxuDQeBt8lD/dZb8Rv9O0tqooKm9qaRQPyU9clQafwMRsq hH9YuFQUXrDtPeNSACUvc8uGymtMhZLKSkKyFygjnfnNXHX7WVj6afVTJ EyhZzSxe6Aj1rV0Rpx+jvr8m6hxb9OgfbEYJDuSqAAgIei5QtHHdmCYxi A==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925943" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 17 May 2022 16:26:35 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Tue, 17 May 2022 16:26:35 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Tue, 17 May 2022 16:26:35 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1652797595; x=1684333595; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uAQEobNYq5e7zqIN9kNTGCuqIpHhyYNp3YJZCqF6qww=; b=I+LhpO8Ic2Glwm4qgLY7pLn9kUfYwU16261jfPlIWT1TlHtysBdDnOpH 2to8AIxNAhAqtPBkFpuMmot+dAG9GCX/SK0AfkL1HCmA1jU3Fhye/jdkG /bVgGGZz4HAZy3kDFm05bz7VGThb0z1aUwyJ/nxrxYRJA9DAv3lbtoLVj aH6QJtJMZdWG2tSVGL4kpH2EUjluMQ1IvH/BG/ZWL8ouYUii70eWQxNk9 M/FdTXZ7tBdyJzqC7LupZ4oD7Sk6ybyYt4DQlM184yxkSom82JxedmOlK IwKcY8Hopr9AKzz6irdjMvVkZnawCLyRxz2S/uDOr8FZZbftYjeSONtrI w==; X-IronPort-AV: E=Sophos;i="5.91,233,1647298800"; d="scan'208";a="23925935" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 17 May 2022 16:26:26 +0200 Received: from steina-w.tq-net.de (unknown [10.123.49.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by vtuxmail01.tq-net.de (Postfix) with ESMTPSA id 5059C280078; Tue, 17 May 2022 16:26:26 +0200 (CEST) From: Alexander Stein To: Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck , Jonathan Corbet , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Lee Jones Cc: Alexander Stein , linux-hwmon@vger.kernel.org, linux-pwm@vger.kernel.org, Markus Niebel Subject: [PATCH v3 6/6] hwmon: pwm-fan: Add hwmon_pwm_enable attribute Date: Tue, 17 May 2022 16:26:20 +0200 Message-Id: <20220517142620.1523143-7-alexander.stein@ew.tq-group.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> References: <20220517142620.1523143-1-alexander.stein@ew.tq-group.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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-pwm@vger.kernel.org This adds the enable attribute which is used to differentiate if PWM duty means to switch off regulator and PWM or to keep them enabled but at inactive PWM output level. Signed-off-by: Alexander Stein --- Documentation/hwmon/pwm-fan.rst | 10 ++++ drivers/hwmon/pwm-fan.c | 95 +++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/Documentation/hwmon/pwm-fan.rst b/Documentation/hwmon/pwm-fan.rst index 82fe96742fee..0083480068d1 100644 --- a/Documentation/hwmon/pwm-fan.rst +++ b/Documentation/hwmon/pwm-fan.rst @@ -18,3 +18,13 @@ the hwmon's sysfs interface. The fan rotation speed returned via the optional 'fan1_input' is extrapolated from the sampled interrupts from the tachometer signal within 1 second. + +The driver provides the following sensor accesses in sysfs: + +=============== ======= ======================================================= +fan1_input ro fan tachometer speed in RPM +pwm1_enable rw keep enable mode, defines behaviour when pwm1=0 + 0=switch off regulator and disable PWM + 1=keep regulator enabled and set PWM to inactive level +pwm1 rw relative speed (0-255), 255=max. speed. +=============== ======= ======================================================= diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 9ebe958cc908..cb29206ddcdc 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -35,6 +35,7 @@ struct pwm_fan_ctx { struct pwm_device *pwm; struct regulator *reg_en; bool enabled; + bool keep_enabled; int tach_count; struct pwm_fan_tach *tachs; @@ -129,7 +130,8 @@ static int pwm_fan_power_off(struct pwm_fan_ctx *ctx) return 0; pwm_get_state(ctx->pwm, &state); - state.enabled = false; + if (!ctx->keep_enabled) + state.enabled = false; state.duty_cycle = 0; pwm_apply_state(ctx->pwm, &state); @@ -178,20 +180,87 @@ static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) ctx->pwm_fan_state = i; } +static int pwm_fan_update_enable(struct pwm_fan_ctx *ctx, long val) +{ + struct pwm_state old_state; + int ret; + + pwm_get_state(ctx->pwm, &old_state); + + if (val) { + /* + * If PWM is already enabled, nothing will change + * If PWM is disabled, it will enable with inactive level (duty == 0) + */ + ret = pwm_fan_enable_pwm(ctx); + if (ret) { + dev_err(ctx->dev, "failed to apply PWM state\n"); + return ret; + } + + /* Increase regulator reference counter to prevent switching off */ + ret = regulator_enable(ctx->reg_en); + if (ret < 0) { + dev_err(ctx->dev, "failed to enable regulator\n"); + /* Restore old state */ + pwm_apply_state(ctx->pwm, &old_state); + } + } else { + /* Only disable PWM if currently on inactive state */ + if (!ctx->pwm_value) { + struct pwm_state disable_state; + + pwm_get_state(ctx->pwm, &disable_state); + disable_state.duty_cycle = 0; + disable_state.enabled = false; + ret = pwm_apply_state(ctx->pwm, &disable_state); + if (ret) { + dev_err(ctx->dev, "failed to apply PWM state\n"); + return ret; + } + } + /* Decrease regulator reference counter */ + ret = regulator_disable(ctx->reg_en); + if (ret < 0) { + dev_err(ctx->dev, "failed to decrease power supply usage\n"); + /* Restore old state */ + pwm_apply_state(ctx->pwm, &old_state); + } + } + if (ret == 0) + ctx->keep_enabled = val; + + return ret; +} + static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val) { struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); int ret; - if (val < 0 || val > MAX_PWM) - return -EINVAL; + switch (attr) { + case hwmon_pwm_input: + if (val < 0 || val > MAX_PWM) + return -EINVAL; + ret = __set_pwm(ctx, val); + if (ret) + return ret; + pwm_fan_update_state(ctx, val); + break; + case hwmon_pwm_enable: + mutex_lock(&ctx->lock); + if (val < 0 || val > 1) + ret = -EINVAL; + else if (ctx->keep_enabled != val) + ret = pwm_fan_update_enable(ctx, val); + mutex_unlock(&ctx->lock); - ret = __set_pwm(ctx, val); - if (ret) return ret; + default: + return -EOPNOTSUPP; + } - pwm_fan_update_state(ctx, val); return 0; } @@ -202,9 +271,15 @@ static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type, switch (type) { case hwmon_pwm: - *val = ctx->pwm_value; - return 0; - + switch (attr) { + case hwmon_pwm_input: + *val = ctx->pwm_value; + return 0; + case hwmon_pwm_enable: + *val = ctx->keep_enabled; + return 0; + } + return -EOPNOTSUPP; case hwmon_fan: *val = ctx->tachs[channel].rpm; return 0; @@ -436,7 +511,7 @@ static int pwm_fan_probe(struct platform_device *pdev) if (!channels) return -ENOMEM; - channels[0] = HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT); + channels[0] = HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE); for (i = 0; i < ctx->tach_count; i++) { struct pwm_fan_tach *tach = &ctx->tachs[i];