From patchwork Thu Jul 21 10:31:23 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: 12924968 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 92114C43334 for ; Thu, 21 Jul 2022 10:32:07 +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: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:In-Reply-To:References: List-Owner; bh=CRtUVuspOEXdcjzwqbAmdbpK6z/kHIS8C5gtwskBOz8=; b=FNREgPponwJ/Jw 9LWt2wreWdeVom8rFNn3rkoLGAd2X3m7npbBtOkhQgc0hvYR+F9/YBSy95F3jbuCQEajKcvuRSYV/ Gexgvv5xBPl8au96NzHrPWgRmlPgU7Y69LAf5izT2yaPhGRxGKjdCVnuF0l+ctXfCiCzhylAPn91y 6yBnl4/ESMiipQgSlrNs0DpkhgTgRamEWm7UseHscJgki+9+vqh53NKkuZ0XwRfojJKjEhGNMqJjD TzZ3b08eqCkusmxRg6c2gVoqIsrBv+AvbqOYDazxJNN0SZnwC15D+8IlAIzXsaqByTuBVio2jLZSW rAeYvfnqRDnWPao6Im4A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETTL-003rpW-3h; Thu, 21 Jul 2022 10:31:59 +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-003rcq-OR 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-0002Vw-QO; Thu, 21 Jul 2022 12:31:38 +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-002IMc-9X; 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-006ZIG-8g; 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 1/7] pwm: sifive: Simplify offset calculation for PWMCMP registers Date: Thu, 21 Jul 2022 12:31:23 +0200 Message-Id: <20220721103129.304697-1-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2206; h=from:subject; bh=S9GIGL+Cct/zyqgr/ikWe3LQhbttxTcRa9DxXEHmNkE=; b=owEBbQGS/pANAwAKAcH8FHityuwJAcsmYgBi2SrrxbR6WH/xT1XiSbvMGpd7KdnUxvzpxZ52l/3U 7p4plO2JATMEAAEKAB0WIQR+cioWkBis/z50pAvB/BR4rcrsCQUCYtkq6wAKCRDB/BR4rcrsCdCLCA ChSWsJTopykEY66sAPoljOUbgwbe5BclCZKRJyMy7TtVK5CtIsUs5cBly/ahVIJX4fFuX012X++rop WkUTBtyyB2GNIwvjL4PapHBuV2zOdrljrtPEyjZ2bJGh+4fEXQJR8u6aHpcGaWKoodorvT+OzhegBl 3C9isWIU/SJeALqb9OPWT6tSV3WAjJ1T4o6XnsxuJhKJvq5bEJWqYwIyizoGD/UT7w6BRHrnEUjDSx L45pMXhfRf6aI7ozoSaTE9oq0bppgFSu4rW61kqx5yxzSHiWlgGdis5CAkAFdNktUjpOJY+J/GE7wR 9uIJP94GSbMvn6Th/K02QgD1lFOlj7 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_838530_29DE86C3 X-CRM114-Status: GOOD ( 13.80 ) 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 Instead of explicitly using PWM_SIFIVE_PWMCMP0 + pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP for each access to one of the PWMCMP registers, introduce a macro that takes the hwpwm id as parameter. For the register definition using a plain 4 instead of the cpp constant PWM_SIFIVE_SIZE_PWMCMP is easier to read, so define the offset macro without the constant. The latter can then be dropped as there are no users left. Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-sifive.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) base-commit: f2906aa863381afb0015a9eb7fefad885d4e5a56 diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index e6d05a329002..b7fc33b08d82 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -23,7 +23,7 @@ #define PWM_SIFIVE_PWMCFG 0x0 #define PWM_SIFIVE_PWMCOUNT 0x8 #define PWM_SIFIVE_PWMS 0x10 -#define PWM_SIFIVE_PWMCMP0 0x20 +#define PWM_SIFIVE_PWMCMP(i) (0x20 + 4 * (i)) /* PWMCFG fields */ #define PWM_SIFIVE_PWMCFG_SCALE GENMASK(3, 0) @@ -36,8 +36,6 @@ #define PWM_SIFIVE_PWMCFG_GANG BIT(24) #define PWM_SIFIVE_PWMCFG_IP BIT(28) -/* PWM_SIFIVE_SIZE_PWMCMP is used to calculate offset for pwmcmpX registers */ -#define PWM_SIFIVE_SIZE_PWMCMP 4 #define PWM_SIFIVE_CMPWIDTH 16 #define PWM_SIFIVE_DEFAULT_PERIOD 10000000 @@ -112,8 +110,7 @@ static void pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); u32 duty, val; - duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP0 + - pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP); + duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); state->enabled = duty > 0; @@ -193,8 +190,7 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk)); } - writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP0 + - pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP); + writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); if (state->enabled != enabled) pwm_sifive_enable(chip, state->enabled); From patchwork Thu Jul 21 10:31:24 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: 12924965 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 75FAFC433EF 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=SLFcDvyDj/C++kpiwKomAuYVq5TURh5hNAseLt5iaPc=; b=1imBS1OwSfde7V o9A/+au7lpJkHRir2TUEETIbibBF+fNr17KODEB7c4jt0PtGyHsru95Nyh4dHyfJoZ8drFuNjt8QV YpRorqaHhAf2IxID9ex/K6TDJcDElo/tEmcZhPdNLPsNecnZZ1qcw+Dc9Ux50s71p4Rg8WmmPmXH7 IEDW7fqp9JEBQVESnWkcaWRYJ/6TI26jZq5Qb/7O3sgcGJRWbTl9iIyURcHUTfJeQZ1Mp6nowaep3 JKNjYI5kFHOovGzRooRmHSiU3lRhVvWuDYax4V6hSffcyvCaK8ibXln+M+qOVpFXCiSkZ2HmkMPMn 8svaX1WxP4T+XRtZXLhA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETTG-003rn4-Pc; Thu, 21 Jul 2022 10:31:54 +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-003rco-Ke for linux-riscv@lists.infradead.org; Thu, 21 Jul 2022 10:31:50 +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-0002Vx-QO; Thu, 21 Jul 2022 12:31:38 +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-002IMg-FZ; 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-006ZIJ-Dh; 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 2/7] pwm: sifive: Fold pwm_sifive_enable() into its only caller Date: Thu, 21 Jul 2022 12:31:24 +0200 Message-Id: <20220721103129.304697-2-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=1745; h=from:subject; bh=sKOWXDCL91kq58t0FdekJeukQkU6Bwjo3++M3hvFQls=; b=owEBbQGS/pANAwAKAcH8FHityuwJAcsmYgBi2Srv+TgOLs1G9g7euiKgebUIZ8RhD3suhSpXS9Vw WaUQQwiJATMEAAEKAB0WIQR+cioWkBis/z50pAvB/BR4rcrsCQUCYtkq7wAKCRDB/BR4rcrsCaQaB/ 0UHu0XNU/HMCeI8tIqAxgqTzAnvVsOD0HiVNK5OiNnKzTx5up6NUB1+1vZj5Jog1MjDfMpaBUTekek UYsWcQ4T/l21c0qoq1sKJZHDMFf0H/d/mdj8/1u5Oafi5Ky79GyIuUpzfaEDEppmH9SmwEjf72b3/T K1Q9AAW3+bQF3hpHfrgXgwYpuI7x2GQc4tbxSm3/KCbhnGogkdzkUf8dYc33zYfqcU3LBXVZGMsJuz Qu5JbIkJo3W8zmeKkOVROAtuhAwtJBRIlQFe2EWzZqD39MG2wojWy9R/m4GsutzgIIsXz0Qv7B+0LG WU/grNk7VedR5GloHAORBj5FJaibM2 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_714324_4F69E93A X-CRM114-Status: GOOD ( 11.94 ) 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 There is only a single caller of pwm_sifive_enable() which only enables or disables the clk. Put this implementation directly into pwm_sifive_apply() which allows further simplification in the next change. There is no change in behaviour. Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-sifive.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index b7fc33b08d82..91cf90bd4083 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -124,24 +124,6 @@ static void pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, state->polarity = PWM_POLARITY_INVERSED; } -static int pwm_sifive_enable(struct pwm_chip *chip, bool enable) -{ - struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); - int ret; - - if (enable) { - ret = clk_enable(ddata->clk); - if (ret) { - dev_err(ddata->chip.dev, "Enable clk failed\n"); - return ret; - } - } else { - clk_disable(ddata->clk); - } - - return 0; -} - static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state) { @@ -192,8 +174,14 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); - if (state->enabled != enabled) - pwm_sifive_enable(chip, state->enabled); + 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); + } + } exit: clk_disable(ddata->clk); From patchwork Thu Jul 21 10:31:25 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: 12924967 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 DF32ACCA479 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=0Ib/Xxug3QFeKQ/U0itMCKLzl6WEOGgPHfBCHz1eST8=; b=WVYl+keCEQgrIr xje1iaDFI9E7n+TJiW/75RN98VowBQw6qZQxQ1sCLwbzLtcZaTEZwhvcPiPmHYS81B3U0ntrYv8UN Z8bpU84PBPQnllb3xC6jMVXCYecGWtgABgoR31cKP2nk+jCQp7veI084avQUgxv6LTyrDjkwtAqbP 1tdgerjUPAJzkWtCY++T1TeZks8fCCNmgvuKs5pRyXZF74naSAh5MM4LUD0+vStNMQ0LFQtuxIf7g qTTaBUxvE2Vl5XEHlAfUBqSfaWZe/gE1nWfQWo7a8wbtLljzJoA0Emwk4RYSRS/TjNhroLDqNfT8v Awpgur2Uq7gJJIxF8DLw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETTF-003rlz-ED; Thu, 21 Jul 2022 10:31:53 +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-003rcl-Ge for linux-riscv@lists.infradead.org; Thu, 21 Jul 2022 10:31:49 +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-0002Vz-QO; 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-002IMl-IZ; 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-006ZIM-JH; 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 3/7] pwm: sifive: Reduce time the controller lock is held Date: Thu, 21 Jul 2022 12:31:25 +0200 Message-Id: <20220721103129.304697-3-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=2153; h=from:subject; bh=J40tL02o9FO4AFx8pwx3mZeFbRQCIoDJSyD9wHvo7Bo=; b=owEBbQGS/pANAwAKAcH8FHityuwJAcsmYgBi2Srys/0egwS0iYQbrd+sk9wZXWLS+eWFR8ARYEas Y8+EXc2JATMEAAEKAB0WIQR+cioWkBis/z50pAvB/BR4rcrsCQUCYtkq8gAKCRDB/BR4rcrsCcG1B/ 4ipirIA6LplygiukGiSZZJ+OP+ym5swDwJGcI5D7TWYk/jD+kmQTsvzSnbG2mCSIP16fp1MyZKacCf 96W3WZvxXLQBs4XkAP60pZ2YX/5puZJE77TNMepcAAD18FENyT6EwnfYeeWy3Ben9qjUepVX3iyhhR 7qIlP6yK+fkUnH7ml5OgvkVCTf3sOwYoMoiEEPafbAMlJImjxDxy0OvQagk4X9KQCYkxBfJ8Y4QpUM goss5DoYNMsC3TruNIMaLoeWecmB6+mZgTh3xNE8vgg+4KqT4yQyW1BrjVzIjhhiWAX3a1ZkGJ4X+q FmnEpj4OvVzXhVtxEisAM8PTwPpApB 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_575450_E974A2EB X-CRM114-Status: GOOD ( 14.04 ) 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 lock is only to serialize access and update to user_count and approx_period between different PWMs served by the same pwm_chip. So the lock needs only to be taken during the check if the (chip global) period can and/or needs to be changed. Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-sifive.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index 91cf90bd4083..6017e311a879 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -41,7 +41,7 @@ struct pwm_sifive_ddata { struct pwm_chip chip; - struct mutex lock; /* lock to protect user_count */ + struct mutex lock; /* lock to protect user_count and approx_period */ struct notifier_block notifier; struct clk *clk; void __iomem *regs; @@ -76,6 +76,7 @@ static void pwm_sifive_free(struct pwm_chip *chip, struct pwm_device *pwm) mutex_unlock(&ddata->lock); } +/* Called holding ddata->lock */ static void pwm_sifive_update_clock(struct pwm_sifive_ddata *ddata, unsigned long rate) { @@ -144,7 +145,6 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, return ret; } - mutex_lock(&ddata->lock); cur_state = pwm->state; enabled = cur_state.enabled; @@ -163,14 +163,17 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, /* The hardware cannot generate a 100% duty cycle */ frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1); + mutex_lock(&ddata->lock); if (state->period != ddata->approx_period) { if (ddata->user_count != 1) { + mutex_unlock(&ddata->lock); ret = -EBUSY; goto exit; } ddata->approx_period = state->period; pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk)); } + mutex_unlock(&ddata->lock); writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); @@ -185,7 +188,6 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, exit: clk_disable(ddata->clk); - mutex_unlock(&ddata->lock); return ret; } From patchwork Thu Jul 21 10:31:26 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: 12924966 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 13478CCA485 for ; Thu, 21 Jul 2022 10:32:06 +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=1+uo8ohBWFGcKIOXb/7abhJkXOi7oRENVJS76BCsF/Y=; b=CwTHcGoAeaBp7r Kn/2kOSE9Z5QZsSgY2hEaiRjmP4qv0na15RccAimDKVSAkgSsQ1lWeuLA+Qi+Bxsw6jg1f+wt8XgM gYOoyHwlhZd3cnAK/y+/OVs0sfJCsOnv+LvfeAEYPFXiOyr3xKtJKFJwts6w3dgG3JdJi1jvQOF1S dHHJrZTrV3mF2re63NJZXTdxjhsK04GGD2yAuvAON9GMSWWWiv9noJ4HwYNsmcIaePMjzoPOjHb8k Bawo9ifULtKkWrFqpT4gXRclDJOR+3TU8ciJIt2pIVF2cAvxKtCI9VXwC/5KJSQ99wQm9K66zfQav E+ISGh4cwnyM/72jaO3w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETTD-003rjw-S5; Thu, 21 Jul 2022 10:31:51 +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-003rcz-KB for linux-riscv@lists.infradead.org; Thu, 21 Jul 2022 10:31:49 +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-0002W0-QW; Thu, 21 Jul 2022 12:31:38 +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-002IMq-Rm; 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-006ZIP-Od; 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 4/7] pwm: sifive: Enable clk only after period check in .apply() Date: Thu, 21 Jul 2022 12:31:26 +0200 Message-Id: <20220721103129.304697-4-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=1762; h=from:subject; bh=c0fLWKgziDkgWoI6+H8lb5WHbrXxOiw4wARKI8YtL8w=; b=owEBbQGS/pANAwAKAcH8FHityuwJAcsmYgBi2Sr1Zw4qH9xQt0jcTvyTAVVW8eBROsYO15Sy4QQA WoS5sU2JATMEAAEKAB0WIQR+cioWkBis/z50pAvB/BR4rcrsCQUCYtkq9QAKCRDB/BR4rcrsCRvCB/ 9frF0VhcjV6YFcVmo+uGdJ5lcNa5Z7TmrJDlamHndttyGzLZ+3xD5JrHqwsCmGwYnxL+Z5qP1BWD3a cASV0TRseaXW0A+SbyGVyMOhZXGTpArSCUO1jCqoWcDsb+kavyPZJresq507ZPqpBQANP5X5sWG2Y/ 9l37G0coHGDTAN8NzI+qDZcynQVZcKIe+K+rhQ3ssLxVtFCiQPjUiuvslDNc1/wXyucXdeKgep9Azr f9ksZ+YmZYHN5pkJ+1ftn8gD7PuFLqqjy2rZUOd5U9fs2c5SAxtREuFlE7k5Xz4mXh40vYmNQlU7O1 wfq+fXcovK5KMepW7TroZ9ixWEnv2W 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_690039_852DFBDF X-CRM114-Status: GOOD ( 13.09 ) 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 For the period check and the initial calculations of register values there is no hardware access needed. So delay enabling the clk a bit to simplify the code flow a bit. Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-sifive.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index 6017e311a879..d833536b5e7a 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -139,12 +139,6 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, if (state->polarity != PWM_POLARITY_INVERSED) return -EINVAL; - ret = clk_enable(ddata->clk); - if (ret) { - dev_err(ddata->chip.dev, "Enable clk failed\n"); - return ret; - } - cur_state = pwm->state; enabled = cur_state.enabled; @@ -167,14 +161,19 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, if (state->period != ddata->approx_period) { if (ddata->user_count != 1) { mutex_unlock(&ddata->lock); - ret = -EBUSY; - goto exit; + return -EBUSY; } ddata->approx_period = state->period; pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk)); } mutex_unlock(&ddata->lock); + 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) { @@ -186,9 +185,8 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, } } -exit: clk_disable(ddata->clk); - return ret; + return 0; } static const struct pwm_ops pwm_sifive_ops = { 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; } From patchwork Thu Jul 21 10:31:28 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: 12924970 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 EAE59C433EF for ; Thu, 21 Jul 2022 10:32:08 +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=il3Ag3VPwvDeoG588AFq7KBckvk8HHB0AR3w5KHISfs=; b=0ZmnwbCZLmgdcy adnT7Y6igodNoyCkI0z08BugCfTVNK8gaNVYMRXeGgjRp3QFJ5PlnVASeL+CCoJoom3IZYoRQuI4U Besy+1cykFO/FFH9fAUem4v45vcl6rGjoJopRkXcfhCbz3oGETspTD6AHQkI/ksgUXlYxdTcdfCAD 8zpW5F6ATbt71VwY4tjwNdQNH68Xt/LiZcbLwZZ914Bmuz/U1iGyX326ODx7w2FUpiP2NZZGXZnvj s8IAY77WRlHBz6gNeiZCZiy9yo0Lg270RXd8BMhXFT2Ed0/tTisBNU4rvuHHksytbB3LDJ0XijQRN kILeNkMOZe0iJ1wcQ7Nw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETTM-003rqO-Ik; Thu, 21 Jul 2022 10:32:00 +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-003rcm-OE 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 1oETT0-0002W2-4g; Thu, 21 Jul 2022 12:31:38 +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 1oETSz-002IMw-C6; Thu, 21 Jul 2022 12:31:37 +0200 Received: from ukl by ptz.office.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1oETSy-006ZIV-3T; Thu, 21 Jul 2022 12:31:36 +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 6/7] pwm: sifive: Ensure the clk is enabled exactly one per running PWM Date: Thu, 21 Jul 2022 12:31:28 +0200 Message-Id: <20220721103129.304697-6-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=3052; h=from:subject; bh=3zYgBnySD37iMkEgERXBOZoG3gCLgbZr9s4K5gsrSaA=; b=owEBbQGS/pANAwAKAcH8FHityuwJAcsmYgBi2Sr7s1Xee6KOfmM8IAOEhYJt8QASWiil0jdPQCuL 7xvik02JATMEAAEKAB0WIQR+cioWkBis/z50pAvB/BR4rcrsCQUCYtkq+wAKCRDB/BR4rcrsCWApB/ 9NmQ9Q2qgZCanYCyvqEAK0xeFbuNNq+pNUjV8I2bonDCLHyDawvdSynIyKzsXmnJsroYGrZuYcARPs ucb5MqG89QIvc94pYH/Nl2EAOz46lzslSUeKuml/WX8qndJbdqwnOYSHvX/8wo8aSdqgrOCH+/IHQ2 NT/n8pFBMDGO482OwHemUB7URNV1IJezr3U/ga5yxdZn9ZIfV2vhzt/jKn7J/WBeI10WQ2snXRz0eI uWDsQg04L2Nb/8Phl5S+mtQ5tTX+uUOziII2Dop+FtcJ5/fww+nEZeBSj2R39btihQA5zuPaWtQpky t198LoUZn2O2oeQ0LtHN95Gu9wfjRK 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_835507_40FA7B11 X-CRM114-Status: GOOD ( 20.31 ) 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 .apply() assumes the clk to be for a given PWM iff the PWM is enabled. So make sure this is the case when .probe() completes. And in .remove() disable the according number of times. This fixes a clk enable/disable imbalance, if some PWMs are already running at probe time. Fixes: 9e37a53eb051 (pwm: sifive: Add a driver for SiFive SoC PWM) Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-sifive.c | 46 ++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index 34d23d56fa25..da40ade0ebdf 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -216,6 +216,8 @@ static int pwm_sifive_probe(struct platform_device *pdev) struct pwm_sifive_ddata *ddata; struct pwm_chip *chip; int ret; + u32 val; + unsigned int enabled_pwms = 0, enabled_clks = 1; ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); if (!ddata) @@ -242,6 +244,33 @@ static int pwm_sifive_probe(struct platform_device *pdev) return ret; } + val = readl(ddata->regs + PWM_SIFIVE_PWMCFG); + if (val & PWM_SIFIVE_PWMCFG_EN_ALWAYS) { + unsigned int i; + + for (i = 0; i < chip->npwm; ++i) { + val = readl(ddata->regs + PWM_SIFIVE_PWMCMP(i)); + if (val > 0) + ++enabled_pwms; + } + } + + /* The clk should be on once for each running PWM. */ + if (enabled_pwms) { + while (enabled_clks < enabled_pwms) { + /* This is not expected to fail as the clk is already on */ + ret = clk_enable(ddata->clk); + if (unlikely(ret)) { + dev_err_probe(dev, ret, "Failed to enable clk\n"); + goto disable_clk; + } + ++enabled_clks; + } + } else { + clk_disable(ddata->clk); + enabled_clks = 0; + } + /* Watch for changes to underlying clock frequency */ ddata->notifier.notifier_call = pwm_sifive_clock_notifier; ret = clk_notifier_register(ddata->clk, &ddata->notifier); @@ -264,7 +293,11 @@ static int pwm_sifive_probe(struct platform_device *pdev) unregister_clk: clk_notifier_unregister(ddata->clk, &ddata->notifier); disable_clk: - clk_disable_unprepare(ddata->clk); + while (enabled_clks) { + clk_disable(ddata->clk); + --enabled_clks; + } + clk_unprepare(ddata->clk); return ret; } @@ -272,21 +305,16 @@ static int pwm_sifive_probe(struct platform_device *pdev) static int pwm_sifive_remove(struct platform_device *dev) { struct pwm_sifive_ddata *ddata = platform_get_drvdata(dev); - bool is_enabled = false; struct pwm_device *pwm; int ch; for (ch = 0; ch < ddata->chip.npwm; ch++) { pwm = &ddata->chip.pwms[ch]; - if (pwm->state.enabled) { - is_enabled = true; - break; - } + if (pwm->state.enabled) + clk_disable(ddata->clk); } - if (is_enabled) - clk_disable(ddata->clk); - clk_disable_unprepare(ddata->clk); + clk_unprepare(ddata->clk); pwmchip_remove(&ddata->chip); clk_notifier_unregister(ddata->clk, &ddata->notifier); From patchwork Thu Jul 21 10:31:29 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: 12924969 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 825C5CCA479 for ; Thu, 21 Jul 2022 10:32:08 +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=bV9hwGQ6zpAIpJ8e/rAoUHfMVQ7n9+l6BBsPLS0JbyY=; b=OQi4grF9s6u18o /bDb+ToE6uKT7IEkvejKNPHQpiui/E1ZwbWWUQWA0qcBm6GvFjDoSBwt8CNizyVdR7etFR3xB7KF6 0zOjB6A+Q4vq1adIXInrvzbdVJalzmfgOe8nMrFF1iRkp5YmzzzaptOa+/Hw7Cbmr7xy4vIblkMr+ TDSBjoc2UKrD0E88MnLKmdFB1mA645nkXClgXdvtTZ1P5VMvJTzhNO8Hc5FELtiKlUKRhAlSEGbz/ YhgU7nTktEPqYxOArZJu8SGQxGFOze6UwXtM4aOWhLUBarnjhSYOdkCYE46YfQK83s8zQUhoxTduv JCI9nOUbB7SrcMQPipzQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oETTJ-003roY-ML; Thu, 21 Jul 2022 10:31:57 +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-003rcj-MV 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-0002W1-QS; 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-002IMt-Ti; 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 1oETSy-006ZIY-8Y; Thu, 21 Jul 2022 12:31:36 +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 7/7] pwm: sifive: Shut down hardware only after pwmchip_remove() completed Date: Thu, 21 Jul 2022 12:31:29 +0200 Message-Id: <20220721103129.304697-7-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=1079; h=from:subject; bh=zQLWxgV295stqWE/56SMfwXF5tRzx6Wmb0QC4HH8Cw8=; b=owEBbQGS/pANAwAKAcH8FHityuwJAcsmYgBi2Sr+XeA/kTOnz7lLWTP39p9qGkAtgJshtXu2A/vv Td2ybxGJATMEAAEKAB0WIQR+cioWkBis/z50pAvB/BR4rcrsCQUCYtkq/gAKCRDB/BR4rcrsCfIuB/ 4787jU2mMlX1tFBEAWmtDC1irHTvudx25tIFJeJWAZPkm9MasFS3qs8e2lDL3q4NXCOm49Z2IH4m6d rf9raH1+cAJxza7sO8LZnMp4YDaptnSFqKpJG6sqbPxwEKj0aZU1g2g+1Zg5AvrQGQgnwmptatgELU rAXNiH8Psm0aDLStJ5UHJtOBxlEIuQtw466jtcMF06/wMT3LfV36QKIYW8GXF+WwZOshuklCHu4xyS dMdvFn2RkGwyxwAM7fyLiOMDpMyxejx9cUHy3+KQN86EoJhAwHOtamHQAGqAGX8Xr8Bhq7RRSMZ8GA IHNDiElI3/fnq2H2oIyE6eJd9gMs7D 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_765238_1321C5AD X-CRM114-Status: GOOD ( 11.91 ) 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 PWMs are expected to be functional until pwmchip_remove() is called. So disable the clks only afterwards. Fixes: 9e37a53eb051 ("pwm: sifive: Add a driver for SiFive SoC PWM") Signed-off-by: Uwe Kleine-König Tested-by: Emil Renner Berhing --- drivers/pwm/pwm-sifive.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index da40ade0ebdf..2d4fa5e5fdd4 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -308,6 +308,9 @@ static int pwm_sifive_remove(struct platform_device *dev) struct pwm_device *pwm; int ch; + pwmchip_remove(&ddata->chip); + clk_notifier_unregister(ddata->clk, &ddata->notifier); + for (ch = 0; ch < ddata->chip.npwm; ch++) { pwm = &ddata->chip.pwms[ch]; if (pwm->state.enabled) @@ -315,8 +318,6 @@ static int pwm_sifive_remove(struct platform_device *dev) } clk_unprepare(ddata->clk); - pwmchip_remove(&ddata->chip); - clk_notifier_unregister(ddata->clk, &ddata->notifier); return 0; }