From patchwork Tue Dec 8 22:01:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 11959901 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 6191BC2BB40 for ; Tue, 8 Dec 2020 22:04:16 +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 18F5E207B0 for ; Tue, 8 Dec 2020 22:04:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 18F5E207B0 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: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:In-Reply-To:References:List-Owner; bh=z6CKnEBCwrBo9PK4OUx0EAc6MciLtLQfyYlIzCcLl7k=; b=YcKoy0hoJfC9tJGKOFOstmUmqg zc4AFvQLZlwWnonsBRylXfJBlTj4j4rBr/yx8axjc3nGLSog1lFSMn3uxsiMp4RpwC8RCRgHuUPdS 7RjV4or5xTx/l6LuZ6GN02KDzIKMs30EqEGX8BsygEZsYNZdHGggNsZXOEFqFOhxCiRsY/OKKY996 JDBeMrzKqJjDHDncInslgGVcY9dWXuP8PTSPbyxYLSp1+w8KgUcFLuGgVZf6fAZDpoaRo92C4g1Kr OlC1kg3/sBtEf+PfR+tFmzo2/1ktbHA7XZyLFDiZ21YVbdt6K3q7VgPug0zjW4fojf+SsZE0pDwJ/ Iv5y824w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kml4H-0001OO-0Y; Tue, 08 Dec 2020 22:02:45 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kml4F-0001O9-Ep; Tue, 08 Dec 2020 22:02:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:Content-Type: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:In-Reply-To:References; bh=0W8L1s/+E8M+O5m3NU5Cjqur7xln1/V/UMJdSR+R264=; b=pAaq1A0/4NY5l1AlWhjVkFSCRr 8Vh540jdKms5F1VjCOYLTkuk2TsOrKgk7VSKRQkTWOs9YFlPe2T9sbAIV8wMHvhcKlHiZO6YjWwfK eeHf9XPQE1xm+NFYoqVhH4C4iYEs7g8e8UMMi6SlcWqBwJqFVgeYltkB8RRDWqmSCyaFUQsd+mMKG P4S55mdCdc9RkBy9CCPlrAsMLqMBofbGLU+WEZikRQFt7lBfxlN0dnIzQbwG73pcMndE1ExHC3RRx BE74mTspH9VyV3/8XTalUkLmLUatadUjKhlxg0DBisZkRMN+qj8AECwu8iFGWtOqliuyk3VnLdrST 1+S22odQ==; Received: from mout.gmx.net ([212.227.17.22]) by casper.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kml4B-0004rT-B2; Tue, 08 Dec 2020 22:02:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1607464945; bh=0W8L1s/+E8M+O5m3NU5Cjqur7xln1/V/UMJdSR+R264=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=UVFUKXAKROTOprum0Bxa8AAlMCbTb8S2jdMLfGiaG0tBNk91Oo0MyMlrfA9v7qdrX zMLoUlsnO4vp5NDEuQW64URRH61u/huMZO/0n36ILGq4CURhs0j7+PLa1rzi8KuRhF lYGhXeLdTjJQBH0KdSpj8HI3VZWeSgEf0zm5rbqM= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from Venus.fritz.box ([78.42.220.31]) by mail.gmx.com (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N63VY-1k6i7M3dxg-016NUN; Tue, 08 Dec 2020 23:02:24 +0100 From: Lino Sanfilippo To: thierry.reding@gmail.com Subject: [PATCH v3] pwm: bcm2835: Support apply function for atomic configuration Date: Tue, 8 Dec 2020 23:01:45 +0100 Message-Id: <1607464905-16630-1-git-send-email-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Provags-ID: V03:K1:aKuR+reSMDTjWPkMggxhK4pXa7GsX+soPSBxaw2X6SiRgJ3gyEf eHuRyhr+2p1cY4DXylGklrqH2aM0gQTB+K5IkpcPy8olTJ8MnIdWfT1BIV/jvPBt9Kjvtep 8u+LRLFuHosJIDegsOBzZH/oiNvbpdmdzw9niv09U5u4Kzm7sjXf1DLoHJjsVWclPYaiVlI zov13P17h6H9/yvHgmFyg== X-UI-Out-Filterresults: notjunk:1;V03:K0:myZg7KeR1cw=:MRonv6eSgpTO/yMAIm6e4I d1W+NUCwofXYYR0/Sqnir9wtHFzgSa2PO/yvwVVOMklldh6o6df2Hmlj8AuX/ErxK8unb7yz6 sOh48kAXa27KfcHX0Z3rYzFVer0YIYO0QJJr5SRgfjHxREmn9uI46njGNbqoLBwW/QYcUfbez tNUky/SCZr6UqMUItZPiaT4kxSjmjTnGt+0Yz2xRBhU7HzXh6t/9TC52RsAiCEL5mriRuraau CsODQ6HPHcRHHQV0hB/kUonDEcOK6pgL2iF15KorczqaWw97tDuvEsSNlj8mTbtmOboIagnw8 cuiIg0k8NJIOsYYZe+U4xXt5Za2bo8PWNgdtqemM8dpBKkYGKgbTuAZ0xoNNn3C8I06+xnGOR VwbTrHvlxJG+dXO3CknAWO2ougD/oyNVMRUPF9vkn8reuJG1crchjfU5OaldY7jGpAKRRPRNe QSxJrWiP/EdOLkhhi/ddxnxZCtO+feqz08hBGO/A2121Mth8ctN+uXXpYFQUtan5gKZ3WDpsP ByyOYi8iLqi5e1DRIGoeLM8BfGuSoqNpVpP6HFOm/9KepSOdXIpaHbRR5vMEH6PTfqCRS/Itz fN2VDgQgc0ONIPx6h+sA/wn9f6X9YzbBNV+EBQVBiwwvsGCOpW6u1picVmHUH8tAsiStFRk/t 1bdJtRbqpapSYmZHzTUIYna+JCg1EF9BBrwV2jltA/lep4ojiTmWVHxMyo9yEDnvwCEoL/5Fa jykH4XD0g5cCikHTgVjQc3881DfAMyK0JbtQbwUHZJT5GjGJK7d0qajGjf1/11FRiSCyfVgSY QdqVKyMljJBeirSTWd00ue5CTfQwbg7tCMBtSptJzBojZNvfXcnD0qDWoz/uQmw3OgH4JKFIN y5vvwFKstMdTPHgDh4ZA== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201208_220239_685110_0B38CD1B X-CRM114-Status: GOOD ( 15.55 ) 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, sean@mess.org, 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. Since now period is a 64 bit value, add an extra check to reject periods that exceed the possible max value for the 32 bit register. This has been tested on a Raspberry PI 4. Signed-off-by: Lino Sanfilippo Reviewed-by: Uwe Kleine-König --- v3: Check against period truncation (based on a review by Uwe Kleine-König) v2: Fix compiler error for 64 bit builds drivers/pwm/pwm-bcm2835.c | 72 +++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 46 deletions(-) diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c index 6841dcf..d339898 100644 --- a/drivers/pwm/pwm-bcm2835.c +++ b/drivers/pwm/pwm-bcm2835.c @@ -58,13 +58,15 @@ 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 long period; unsigned long scaler; - u32 period; + u32 val; if (!rate) { dev_err(pc->dev, "failed to get clock rate\n"); @@ -72,65 +74,43 @@ 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 */ + period = DIV_ROUND_CLOSEST_ULL(state->period, scaler); - if (period < PERIOD_MIN) + /* dont accept a period that is too small or has been truncated */ + if ((period < PERIOD_MIN) || (period > U32_MAX)) return -EINVAL; - writel(DIV_ROUND_CLOSEST(duty_ns, scaler), - pc->base + DUTY(pwm->hwpwm)); - writel(period, pc->base + PERIOD(pwm->hwpwm)); - - return 0; -} - -static int bcm2835_pwm_enable(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); - - 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; + writel((u32) period, pc->base + PERIOD(pwm->hwpwm)); - value = readl(pc->base + PWM_CONTROL); - value &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm)); - writel(value, pc->base + PWM_CONTROL); -} + /* set duty cycle */ + val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, scaler); + writel(val, pc->base + DUTY(pwm->hwpwm)); -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; + /* set polarity */ + val = readl(pc->base + PWM_CONTROL); - value = readl(pc->base + PWM_CONTROL); + if (state->polarity == PWM_POLARITY_NORMAL) + val &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm)); + else + val |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm); - if (polarity == PWM_POLARITY_NORMAL) - value &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm)); + /* enable/disable */ + if (state->enabled) + val |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm); else - value |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm); + val &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm)); - writel(value, pc->base + PWM_CONTROL); + writel(val, 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, };