From patchwork Wed Dec 20 14:24:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 13500180 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 662F3C3DA6E for ; Wed, 20 Dec 2023 14:25: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: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=2Elvt1wJgpqdW1J8aflMV8AYpOkHliYwX5BUE6CMYzs=; b=mntFUiN+4xhpfw dejHmdZXDC2jjxdVYy+qzf8XRcKR6rg353+ZQqK10SJlXrNPk2CawNM+CVKIpCnzabau95euKWtUq GzWNiS28pbSskGEp6zTKRHkeWQdpCRoRfCBUbeETwpf9J6gI6/UtkiMUNvINDaZiY6dswTQg6av9p YtIo5gp6O4AKsXzugmfR8EmB2CBpJRWGaZu2BpwJQX6lNakUCZO9vkjbxpFxOiUlltDz81xNuzBSF lp1jyu9NvfW22qwgbemVcCq7AR2QSAUe4HBzX0QovZOyOE7tfhNYNumXd/iEJJIz5pog0/O4ZDg96 qrb9HBZOFG6Owj+zdCug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFxV7-00055M-0c; Wed, 20 Dec 2023 14:24:45 +0000 Received: from gofer.mess.org ([2a02:8011:d000:212::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFxV4-00054u-1n; Wed, 20 Dec 2023 14:24:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1703082278; bh=3nJY5azeXpwacoa6a2SmWI95bGDplAo5X6yyrstIjmU=; h=From:To:Cc:Subject:Date:From; b=fsIgbuOCsPqt2uskYUYZh1wJwArRP6uO97NbODoc+qf9AkVTPtj+79/J/mPMUDLzf X+AlAwWPxBf47mSCj89pGu141jLGFIcVPgleMxrC/rLMgpNgxifRi0YiieE+k/zFpc qUxOfQBFd8tjkHmRzO27T1KYQfnx8YTmca82ZeArUfiXsb/e0/v5tgqTOcnadjYgce Y2lKz2Ynn1pCF9qkKXxAHq/vfxV+SA4PmghCuQuobfAg5aO3eI4AEy9gafq7LOjgi8 lpWVOURnlu0s4BnCNALzDoXCKQPTNC+KpuQ3FL+9NR4UJ7CWJGtDmsf4Ufm5MCAtEt eH7z9cDskVxYA== Received: by gofer.mess.org (Postfix, from userid 501) id 2A4171009FC; Wed, 20 Dec 2023 14:24:38 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1703082276; bh=3nJY5azeXpwacoa6a2SmWI95bGDplAo5X6yyrstIjmU=; h=From:To:Cc:Subject:Date:From; b=CJ77FQKTi3a7yYSw14QDo+huocBfxWeIAdaGsdHQQS+UfVhiOgGZPKyb8hzW6PNr6 BlOlb/fp4aRMahIpP8qgNMuRbZALcOb79ezlUDY0FYSV/Cz4aTY+eukU53BuyU5HSk s6tlYfVOyWK6vf6R6uNOJ5S4ieUwXiqOIC9HqjI3E92xYEkEavngA0H/ogI584SXVT 2SXI91OnJ/lm0ZHy5dFjL7RuOIx4m86LkbYBTBehVp88iQhWxfXWMO9DQBYl/Zup2T pRDykH5Haz0S5TwhdHKIISE89lT07Soq6BmQ+65CHk6bLX7runTYduRMs39Mj09mJi Wpx02ysD7lPUA== Received: from localhost.localdomain (bigcore.local [IPv6:2a02:8011:d000:212:ca7f:54ff:fe51:14d6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by gofer.mess.org (Postfix) with ESMTPSA id 077791000CC; Wed, 20 Dec 2023 14:24:36 +0000 (GMT) From: Sean Young To: Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6n?= =?utf-8?q?ig?= , Florian Fainelli , Ray Jui , Scott Branden , Broadcom internal kernel review list Cc: Sean Young , linux-pwm@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v11] pwm: bcm2835: Allow PWM driver to be used in atomic context Date: Wed, 20 Dec 2023 14:24:25 +0000 Message-ID: <20231220142426.1275052-1-sean@mess.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231220_062442_735043_9D6091FA X-CRM114-Status: GOOD ( 15.12 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org clk_get_rate() may do a mutex lock. Fetch the clock rate once, and prevent rate changes using clk_rate_exclusive_get(). Signed-off-by: Sean Young Reviewed-by: Florian Fainelli --- drivers/pwm/pwm-bcm2835.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c index ab30667f4f95..307c0bd5f885 100644 --- a/drivers/pwm/pwm-bcm2835.c +++ b/drivers/pwm/pwm-bcm2835.c @@ -28,6 +28,7 @@ struct bcm2835_pwm { struct device *dev; void __iomem *base; struct clk *clk; + unsigned long rate; }; static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip) @@ -63,17 +64,11 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, { struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); - unsigned long rate = clk_get_rate(pc->clk); unsigned long long period_cycles; u64 max_period; u32 val; - if (!rate) { - dev_err(pc->dev, "failed to get clock rate\n"); - return -EINVAL; - } - /* * period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC * must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the @@ -88,13 +83,13 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, * <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate * <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1 */ - max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, rate) - 1; + max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1; if (state->period > max_period) return -EINVAL; /* set period */ - period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * rate, NSEC_PER_SEC); + period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC); /* don't accept a period that is too small */ if (period_cycles < PERIOD_MIN) @@ -103,7 +98,7 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, writel(period_cycles, pc->base + PERIOD(pwm->hwpwm)); /* set duty cycle */ - val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * rate, NSEC_PER_SEC); + val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC); writel(val, pc->base + DUTY(pwm->hwpwm)); /* set polarity */ @@ -131,6 +126,13 @@ static const struct pwm_ops bcm2835_pwm_ops = { .apply = bcm2835_pwm_apply, }; +static void devm_clk_rate_exclusive_put(void *data) +{ + struct clk *clk = data; + + clk_rate_exclusive_put(clk); +} + static int bcm2835_pwm_probe(struct platform_device *pdev) { struct bcm2835_pwm *pc; @@ -151,8 +153,26 @@ static int bcm2835_pwm_probe(struct platform_device *pdev) return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk), "clock not found\n"); + ret = clk_rate_exclusive_get(pc->clk); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "fail to get exclusive rate\n"); + + ret = devm_add_action_or_reset(&pdev->dev, devm_clk_rate_exclusive_put, + pc->clk); + if (ret) { + clk_rate_exclusive_put(pc->clk); + return ret; + } + + pc->rate = clk_get_rate(pc->clk); + if (!pc->rate) + return dev_err_probe(&pdev->dev, -EINVAL, + "failed to get clock rate\n"); + pc->chip.dev = &pdev->dev; pc->chip.ops = &bcm2835_pwm_ops; + pc->chip.atomic = true; pc->chip.npwm = 2; platform_set_drvdata(pdev, pc);