From patchwork Sat Nov 28 00:55:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 11937333 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 209B8C3E8C5 for ; Sat, 28 Nov 2020 00:58:30 +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 B7CDA2224B for ; Sat, 28 Nov 2020 00:58:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="1FbG9cvF"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=gmx.net header.i=@gmx.net header.b="RZh0tbC7" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B7CDA2224B 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=AIVQySXN1T1/P8iSHRsxyD+R8yQm1KcElwkdVGcXpyw=; b=1FbG9cvFudwzNV6ckTBozHRsWl OxjzEOmNdQpvyiNz8CK/I/ijSiOVE6kzWZ1EKLYHG0M10GrnRuFcdVePKiZqAEH5o+ahJCETEy02A hi1RZcK3rhC0G4kFNx5sYABOaC9mY05gOUqIQy7u9Xd350BFdEBPwxUK6/r8Q7oQjwngbaZcP0qms PPQJiWetqAQWjo5KppaG0A967etnGGBkrXTaIK7trFp8wthkFhZ1/yR/APrbkarn+y4XWFOUxS4df BUEd533GmfxfGd2BrQyipHpdLDSQc5dLCQMi+2GrXfeSaSfPQvVTVbLJaJx9K9s4vUq60bha5hrrv +hiDRyyA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kioXe-0007cl-7B; Sat, 28 Nov 2020 00:56:46 +0000 Received: from mout.gmx.net ([212.227.15.15]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kioXa-0007bz-HE; Sat, 28 Nov 2020 00:56:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1606524989; bh=ZzHUIAcJkfQ4yMJ+3D8uhEGiaG82a6I1rM1VddR6Ljk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=RZh0tbC7BlG0urshsTaRhgFxZFwJwAE99KQ9gqlTWfS07OvuIide2+xUBup1eB/bA 493XIIWAPsKeAB5nplcU0KoaAKszBiRb2uSqx+omZNsDKoS3BKQyqXMgBk+XZQXk8c 4L2z4x0YlwL2o9Yb3H78x+ST82kV8ZqwYZOVootA= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from Venus.fritz.box ([78.42.220.31]) by mail.gmx.com (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1Mlf0K-1kInRX2KxO-00igwL; Sat, 28 Nov 2020 01:56:29 +0100 From: Lino Sanfilippo To: thierry.reding@gmail.com Subject: [PATCH] pwm: bcm2835: Support apply function for atomic configuration Date: Sat, 28 Nov 2020 01:55:12 +0100 Message-Id: <1606524912-16683-1-git-send-email-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Provags-ID: V03:K1:jdfU7DsUEvhMkWh8Prv5e4yo2mrgbndU0LflzdY48Lv5rZPE6cJ 2s7lKs5atD/6eozd9PQDBiPuGTV4mKVTsRsEMM9JxYr6tEW9Xii7Y1QmBIk+I40LvULPejH SFVQbnUguaqaAR125/XzqnmsPGnGYN50LM2f9iiu9oF8WA7CGbL+uRCVLgbOw2SJWQUCLxI eCXXmAo6KONu6luLslK7g== X-UI-Out-Filterresults: notjunk:1;V03:K0:ymX10MsVhas=:Cgj/yIxRX4k6/e+KKr3Usp Kr4vbvRCOFUnGClJOdcte3iAemlH+Iqud0V54Lss0U0+qgFEY+/ZLxDfREvOGyI0qf68cFOob piVdq0acfdCkNpVIfrPbDZFRzMRyX2K/no499Vt0x0P06L7pyb0C2WhZWmXXASo75Af5PTgeJ 7yKUL0QzR91fWo/3nUOqpACAFdtBjhevk90LLnZ2RW6zp+JZLZe42L9y3fB3Dv4Fmx1GNu6JP I9kR61MfvLCBr5r+jlFfU7vCkr7r1NXkfY0gvQmVOCDXWwMPcBHMXh4ORAuUo/9tlJUGhhcT9 qmCzrUZn/ead1d6VvwE2XZG1Pj4TQwpPGktwBBbSx/e8DjL92KCsxW54QF7rAXBWQNXObG1qH HhccDMOgtt45sgCZzUrNFdNVK7Iu9QfoZgDBD9jMSg0JV5hQlCg7F3Z+EtNpYWVqQOob4VW44 5D56CGmYAcjSa5YTClTvh46MkATqQYXo7UncBjMR4muQCSDg+PdcEZsfG25XCakVJc2H/GTjr giCvkqgiBXpnPXN8xExo/qccE+oMHXYV1AzY8opT41YSDy6qn3RvrWAHyo1euvG2RQOpOcmNh 0kLWp5uqYtWRwUszAOQM+LfuvVPijVr1aM2Y0cMKNrgElNkTnvN5CF1mzdbjWbDp3GiKJ/l2E 3Va4Wg+LiAh3W35w4MywWErLFOmz7rRI6d9KvwKO79CesDqfntCP/7wECyesezZRtvIXeEEdB Bz3OrPbpgSz4oVeagLSXEuSyw8wbp9xm9CLVLMXXvdSddznLFGuZrsGBZXqjcVOIkNfSofZkW 71Id5q0dvcYG3hLEvjCTTeClBAn0zV0o8Epx6QcOy4W31CxxGv5PM3GECaY6/x/blHE3n9HZH uukY62E8stXfHj2ciBHA== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201127_195642_796569_E47F2BD4 X-CRM114-Status: GOOD ( 15.95 ) 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. 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 d78f86f..2eadfa5 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(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(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, };