From patchwork Sat Nov 28 12:02:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 11937473 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 389DCC63697 for ; Sat, 28 Nov 2020 12:04:12 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id CF3A12084C for ; Sat, 28 Nov 2020 12:04:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="DnkRIkeW"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=gmx.net header.i=@gmx.net header.b="l0asZoOm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CF3A12084C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=2bbiOtTT2fa5Iyb4crpX0JLwiFpHnt1SZIXaLH37IPU=; b=DnkRIkeWFMcyBJxgA/DtH6dg2 +zsXXmWZ70K8PRtOUFqfcd5noDm9yhaBZ5HeoVmDdHYzyTpVpPi91/EBXZnNS7153SffAJf17txcW W4qwnw8pz218e7mo21pHaxKkOJO+HYlXKpCOJQD9oayNz4biyMJ1AvxB9Sa2cDY0m5VfrvXzoUHf9 8mkvBdUjXtCKyiIIKmqJ5kvBv3WjljyacD97yKZ7q0P8/MzN7cHRC/uLWGZLdP8GJONidM89JCsaK +lUaHc4NYu2yQRiKcIF3KhT2qC98kGH3D6OHvDNmryGlXBcMnohMLM6SriFXZi9DIOLUh5JiDLOS3 HTbGSnI5A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kiyvu-0006KI-8v; Sat, 28 Nov 2020 12:02:30 +0000 Received: from mout.gmx.net ([212.227.17.21]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kiyvq-0006JB-BQ; Sat, 28 Nov 2020 12:02:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1606564936; bh=fTNcahD5C5mIibbD0BONpKqc5JTcNDDIjg8VN6MVM/A=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=l0asZoOmdciWa6Z8OOfRwU/cRgSIqfE3848KlozjOr9y3jrOp7nvKOnVonaxh2PtB kAKxhSThS91c9RB60mTUpfV9irycyEBhqi8e17xMTMcXUoOKvCq5EEf4IeRgmiEEu9 cVeimKN0dpicTuhuSsSiULz7S7ffsE/BkzpW+pmY= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from Venus.fritz.box ([78.42.220.31]) by mail.gmx.com (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MTzb8-1kZfWP46l8-00R1QV; Sat, 28 Nov 2020 13:02:16 +0100 From: Lino Sanfilippo To: thierry.reding@gmail.com Subject: [PATCH v2] pwm: bcm2835: Support apply function for atomic configuration Date: Sat, 28 Nov 2020 13:02:06 +0100 Message-Id: <1606564926-19555-1-git-send-email-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <202011281128.54eLfMWr-lkp@intel.com> References: <202011281128.54eLfMWr-lkp@intel.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:og7aglZ3Q+zHmb8EXfjZyCFzh8Ae5o38SlBuTifQqzoXN2JffJR dqmvC5FywZBF4ubQoLkvsXZqbPjAbEKapFnoupSXKXcTlHvuAXXUvjmQ8pqiV4mL3WqHjF1 oXE5JpJErxtKwPFphUY1y30bdXGMo/vd6Eo2oGUcG/WKGGwPLkhMNEHw3bZSSTWk7gEJEqs HHdyD/AJTIR5Ql0alVb7g== X-UI-Out-Filterresults: notjunk:1;V03:K0:GhAvJpPyTmE=:gq3pCutU/1CG93aowxmpnf 8bEMumgIvihJH5/qnKYeJt/zv4diMLsGX+E7qQ7+0fvHRfbV2QFWzUSsNk6KQ10+daqlE2i2C Sx5HxRhU3BzoyFDS94Pu5Vck6LPrDs10FmVHv1FWll95zH8ADpw9ycDCd3Fw707r7BtJzoAn+ eXr0Tc2oLGJrdXu6JQE0JS8fs7kLl79XGvfinDNLc6J6cAHeHTybGDeJ5kT8f+P3geAnawj0n H8arim2tZBR0pmJC3JMv/JZixeErH5BIYhIpR6wy8Czj8W3MdYON7JyOeWjkYQa/ICiLYloUd TeMprFQVAR03ZA05mKGOHGNjkqJUU+E+hIPtfaPwHG/JvSKX75zk7VYbpgWxe9OOpO6TKO1m0 lkPvSRtz1j3m1g0iPZNcgp3ihlXvNcjX8iqK+rcoYbP0RShF1AIVntMlbqdmbblHIaAUr/zG8 vTjV/F2fockG6v/uwQgorWxxu55Kos9lCBN8DKmx0r85JcgrBfvKXp66VaZ5EXuvAmCwWLDMt NhmSUhzLCRpM2UAtnStdM3VOtpYeKAH0y4EDFmk/RqRvd25Y8E2ht6PFONmtrGhZTYIXfndSC 5MQ/I5MbZrZ6aYbUg6sneGXTssTpHebL8iuIuwgc0km3FTfMfZPXsewV+jIEByKhHrtp9+QFm Bw7Mc+lsGjed/fC+rrkGGvzN0GqYW5HcJ9D/1oe9vYYtmiJYYSbzHSxn0RDvwqZ9d+x8TtdT6 EX+LDFu6waaUDdvgyvDDFTG9dJlY9NsOkctc3REUamGdeSgdKwPPEFrhH8V5iEQHLoCByZjqv DkLf49NjPuZjS3ZyGM4OIzirayGyc0S665VMqaGDBUS/Kw98irsuPc2HVzgbKirPbBTdQ1/Ft mP2+CXKgcPeFlnvGMU1g== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201128_070226_785633_EAB53804 X-CRM114-Status: GOOD ( 16.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, linux-pwm@vger.kernel.org, f.fainelli@gmail.com, sbranden@broadcom.com, rjui@broadcom.com, linux-kernel@vger.kernel.org, Lino Sanfilippo , bcm-kernel-feedback-list@broadcom.com, linux-rpi-kernel@lists.infradead.org, u.kleine-koenig@pengutronix.de, lee.jones@linaro.org, nsaenzjulienne@suse.de Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use the newer apply function of pwm_ops instead of config, enable, disable and set_polarity. This guarantees atomic changes of the pwm controller configuration. It also reduces the size of the driver. This has been tested on a Raspberry PI 4. v2: Fixed compiler error Signed-off-by: Lino Sanfilippo --- drivers/pwm/pwm-bcm2835.c | 64 ++++++++++++++++------------------------------- 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c index 6841dcf..dad7443 100644 --- a/drivers/pwm/pwm-bcm2835.c +++ b/drivers/pwm/pwm-bcm2835.c @@ -58,13 +58,14 @@ static void bcm2835_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) writel(value, pc->base + PWM_CONTROL); } -static int bcm2835_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, - int duty_ns, int period_ns) +static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, + const struct pwm_state *state) { + struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); unsigned long rate = clk_get_rate(pc->clk); unsigned long scaler; - u32 period; + u32 value; if (!rate) { dev_err(pc->dev, "failed to get clock rate\n"); @@ -72,65 +73,42 @@ static int bcm2835_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, } scaler = DIV_ROUND_CLOSEST(NSEC_PER_SEC, rate); - period = DIV_ROUND_CLOSEST(period_ns, scaler); + /* set period */ + value = DIV_ROUND_CLOSEST_ULL(state->period, scaler); - if (period < PERIOD_MIN) + if (value < PERIOD_MIN) return -EINVAL; - writel(DIV_ROUND_CLOSEST(duty_ns, scaler), - pc->base + DUTY(pwm->hwpwm)); - writel(period, pc->base + PERIOD(pwm->hwpwm)); - - return 0; -} + writel(value, pc->base + PERIOD(pwm->hwpwm)); -static int bcm2835_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) -{ - struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); - u32 value; + /* set duty cycle */ + value = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, scaler); + writel(value, pc->base + DUTY(pwm->hwpwm)); + /* set polarity */ value = readl(pc->base + PWM_CONTROL); - value |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm); - writel(value, pc->base + PWM_CONTROL); - - return 0; -} -static void bcm2835_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) -{ - struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); - u32 value; - - value = readl(pc->base + PWM_CONTROL); - value &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm)); - writel(value, pc->base + PWM_CONTROL); -} - -static int bcm2835_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm, - enum pwm_polarity polarity) -{ - struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); - u32 value; - - value = readl(pc->base + PWM_CONTROL); - - if (polarity == PWM_POLARITY_NORMAL) + if (state->polarity == PWM_POLARITY_NORMAL) value &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm)); else value |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm); + /* enable/disable */ + if (state->enabled) + value |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm); + else + value &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm)); + writel(value, pc->base + PWM_CONTROL); return 0; } + static const struct pwm_ops bcm2835_pwm_ops = { .request = bcm2835_pwm_request, .free = bcm2835_pwm_free, - .config = bcm2835_pwm_config, - .enable = bcm2835_pwm_enable, - .disable = bcm2835_pwm_disable, - .set_polarity = bcm2835_set_polarity, + .apply = bcm2835_pwm_apply, .owner = THIS_MODULE, };