From patchwork Fri Oct 13 10:46:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 13420651 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 913ABCDB482 for ; Fri, 13 Oct 2023 10:48:03 +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=jPNSyqpHQMEdevh+8AJX1K2mNt8c7MSfmue6ZFPXtMw=; b=US/Oujup1GfB0y UE4UYmezCDJ1f8HF6+Vgqh5kcvcyFs8nYl4z1JNEhGp+agcjE4/ubvDlzRd0YFGhBJPfri+9cgYPm fGAtQG7swfJmFFarMwoDRWP+/vn+nXcYLFbxHAYiHaslnWXptMwMa1nKRXEWZMyf0Vn9PXFz7kl54 VpjmRGdGjAPFKp4+U/fCZ2va4r2yWzhLs65RRTWRfFwrsNXd7F81Y5c2KOqtTJU8nST9FYCI7VaO4 zzE9D+jcu89sl1y3TkdZwfT/Y3+hnRus+RvQpZHYBD/x7ZgybPZo/9IiUezSeTlrH4AVt2nqH0N3l oryi3KzBxjaEt/LyQORw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qrFhi-003Elz-05; Fri, 13 Oct 2023 10:47:38 +0000 Received: from gofer.mess.org ([88.97.38.141]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qrFhe-003Ekc-2h; Fri, 13 Oct 2023 10:47:36 +0000 Received: by gofer.mess.org (Postfix, from userid 501) id 71EAC100101; Fri, 13 Oct 2023 11:47:25 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194045; bh=a+OT+HzNlnPxyf1ivSREODdEglTMX6uJLENTDZVv+3Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o2rYGmFbzU16Zo9TZ5PgbeTlCgt2UAm/9upTR5nZ/Fh/NatlJIEH+dLl8DxMacDLs KwQwemhkJHGkejK2zCttay/P5x3SRy3Afe4ML4IlUbX7EHPffPGG0PiYzrlzy77maB iMLyil3Y4qKit5K365OXf1o+UWhkNx6OgKVaMkVcZA3QUoSzfeqgIWk/xHT3BwfDHt pjmp09NJ9UJfH1/kfvxZ90eP3Ke96ZWPFwvASr2qVzWwVAfCl5QiknABL4QoC6t80d E7AD8rAZ9iAP3oUFyWoZgWdUWaJSLHAcO1Pvk2Oznz5Bc5rZonordQRO1SWEOT2AM8 kfQ/jMqphyllQ== Received: from bigcore.mess.org (unknown [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 95D831000C6; Fri, 13 Oct 2023 11:47:23 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194043; bh=a+OT+HzNlnPxyf1ivSREODdEglTMX6uJLENTDZVv+3Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SyFMY6Cp/t2LA/IOZwLp4An5hFfJhhWgS9n8uhI7JwF3aR63b0IceINN0kJpQfTFi i700a30JMLprBEYm3Vduw3pD5FUXcS/T9OjLF2aU/D9fezt2GVGLYp8wdDk2VSLfaI 53ossojhah6OONUpD48GxW93FitpVkDhy7QZsRShrmi/7GtCqG77Sj97QC9knIwL8p HpqnO2LyqV9sb+27mRDxtfC+e1ITLwJ/aXv51PYHDpKq2xt0Jsi3jFMv7NAaIDcype 9dPaFAFBHNxa3/41gjHyOZZDLcCuajJ5dkKyAUDZreLEooHxh6Y8p5sBoNjzt50rM6 /n4UEhoDXHhMg== From: Sean Young To: linux-media@vger.kernel.org, Ivaylo Dimitrov , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , 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 v2 2/3] pwm: bcm2835: allow pwm driver to be used in atomic context Date: Fri, 13 Oct 2023 11:46:15 +0100 Message-ID: <6ce73b2688f059e7169935699044104cf37b2425.1697193646.git.sean@mess.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231013_034735_029455_84FE75B4 X-CRM114-Status: GOOD ( 16.28 ) 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. Since the clock rate cannot change on an rpi, simply fetch it once. Signed-off-by: Sean Young --- drivers/pwm/pwm-bcm2835.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c index bdfc2a5ec0d6..59ea154dd657 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 */ @@ -129,6 +124,7 @@ static const struct pwm_ops bcm2835_pwm_ops = { .request = bcm2835_pwm_request, .free = bcm2835_pwm_free, .apply = bcm2835_pwm_apply, + .atomic = true, .owner = THIS_MODULE, }; @@ -156,6 +152,13 @@ static int bcm2835_pwm_probe(struct platform_device *pdev) if (ret) return ret; + pc->rate = clk_get_rate(pc->clk); + if (!pc->rate) { + dev_err(pc->dev, "failed to get clock rate\n"); + ret = -EINVAL; + goto add_fail; + } + pc->chip.dev = &pdev->dev; pc->chip.ops = &bcm2835_pwm_ops; pc->chip.npwm = 2;