From patchwork Thu Jul 21 10:31:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 12924964 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8F495C43334 for ; Thu, 21 Jul 2022 10:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=TAULwTvD6oqQuChJ7VFMnD1UsXUkTXhb7Xs2thQjSAE=; b=a2FtopD2QfnuIo EBqU5AiLzj6WUcnfZTl0o2EECtetSR475KL6oObQqI4HFGmaCSn0rrOFvXm5qGpTE0Tfz26mjqPI/ jS4qGRwf0myUtZ0i9pOoSSZRp4Evnz5sB2zXje5LSNuFUYN/tR2BCYPeqNPyszSooM7htW1mck0Gc nzSMz0rujJCmoQPBlEStGBPqpitYXB+Y3gyyhDOQVLm4Y0PFwsk9gh89eZbWxN1B/5NvEUlkDgAYN D8qHEqtpAteIcCp0vLLdfe1ZXJVzKOV71YhEtXWIZuJUyd6rV41w7IdMXJe/HjiJthbOlSaLsfbvz gtHm5fmYINPdxo83t8FA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETTI-003ro2-5h; Thu, 21 Jul 2022 10:31:56 +0000 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETT9-003rcn-Nb for linux-riscv@lists.infradead.org; Thu, 21 Jul 2022 10:31:51 +0000 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oETSz-0002Vy-QN; Thu, 21 Jul 2022 12:31:37 +0200 Received: from [2a0a:edc0:0:900:1d::77] (helo=ptz.office.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1oETSy-002IMk-IB; Thu, 21 Jul 2022 12:31:36 +0200 Received: from ukl by ptz.office.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1oETSx-006ZIS-UD; Thu, 21 Jul 2022 12:31:35 +0200 From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Thierry Reding Cc: Wan Jiabing , kernel@pengutronix.de, linux-pwm@vger.kernel.org, linux-riscv@lists.infradead.org, Palmer Dabbelt , Paul Walmsley , Yash Shah , Atish Patra , "Wesley W. Terpstra" Subject: [PATCH 5/7] pwm: sifive: Simplify clk handling Date: Thu, 21 Jul 2022 12:31:27 +0200 Message-Id: <20220721103129.304697-5-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220721103129.304697-1-u.kleine-koenig@pengutronix.de> References: <20220721103129.304697-1-u.kleine-koenig@pengutronix.de> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1817; h=from:subject; bh=0fFwEZkhwtAHdV9/NUnEXXx4U/lJdZQ7HP84P2BZd3U=; b=owEBbQGS/pANAwAKAcH8FHityuwJAcsmYgBi2Sr4PlqUFDXOkb1eWHlVrO1iKgp5d1w/4C1/TlH0 MQzpDGWJATMEAAEKAB0WIQR+cioWkBis/z50pAvB/BR4rcrsCQUCYtkq+AAKCRDB/BR4rcrsCdnlB/ 9ZVYl1QMEBzPLGFquFuYMSf8xCwaQK/lcVrfciCCMgu4uOZ6A3srCMqVyMBQwJo9ZPa+jf+UFw4/GE n2aV5tusUN6QVOVZ5a3qgmCzehMnZv5hNWyjMPM+DPB38nHdMVebwHIrgFZB1QGC7zEx4VGNQ/NGN6 5rVN58ustUhIbTBPSlYKxA5pYdfBVSOYsw+M/pzQa1ALWRYW8dxmzxmLcnS0tvlltknksaVmSeKurf u/d4W3x8A6aoA7JTUpuCJsL83OnrXkVhmVqtH8Uc3OLUu4LwA4TrxgGvTmWx8J3TdC4KalbjkwANpZ KfbRdkbB2YdwEezoyAWsxjXwKhCdie X-Developer-Key: i=u.kleine-koenig@pengutronix.de; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-riscv@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220721_033147_822786_2C293181 X-CRM114-Status: GOOD ( 13.57 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The clk is necessary for both register access and (enabled) operation of the PWM. Instead of clk_enable() update_hw() if pwm_got_enabled(): clk_enable() elif pwm_got_disabled(): clk_disable() clk_disable() which is some cases only calls clk_enable() to immediately afterwards call clk_disable again, do: if (!prev_state.enabled) clk_enable() # clk enabled exactly once update_hw() if (!next_state.enabled) clk_disable() which is much easier. Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-sifive.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index d833536b5e7a..34d23d56fa25 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -168,24 +168,24 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, } mutex_unlock(&ddata->lock); - ret = clk_enable(ddata->clk); - if (ret) { - dev_err(ddata->chip.dev, "Enable clk failed\n"); - return ret; + /* + * If the PWM is enabled the clk is already on. So only enable it + * conditionally to have it on exactly once afterwards independent of + * the PWM state. + */ + if (!enabled) { + ret = clk_enable(ddata->clk); + if (ret) { + dev_err(ddata->chip.dev, "Enable clk failed\n"); + return ret; + } } writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); - if (state->enabled != enabled) { - if (state->enabled) { - if (clk_enable(ddata->clk)) - dev_err(ddata->chip.dev, "Enable clk failed\n"); - } else { - clk_disable(ddata->clk); - } - } + if (!state->enabled) + clk_disable(ddata->clk); - clk_disable(ddata->clk); return 0; }