From patchwork Wed Dec 9 20:48:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 11962541 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 2F76BC4361B for ; Wed, 9 Dec 2020 20:50:25 +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 D52F623C90 for ; Wed, 9 Dec 2020 20:50:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D52F623C90 Authentication-Results: mail.kernel.org; dmarc=fail (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=Xy1ls1uLItACZhRXQ2hBdlLpw+Wlo6s2bdb5HgWFJZI=; b=3aIi+sGuVTVCcrjPoUl7n9G2qh Ex3HVFZsweUpaieLBRTMRLCSvVAv2GhK2WjkWQTvsS/YUT5RnSMxZs+RWb2wLFh7DjL0e+wvvuN8J PwVxf5+nlGha8aXoq4gy+ohilCFxYLUxmjGcyHXLDRl3D/X5909Cp7m5wUcZsucqv7D5fKhQZmrRh Hxg6aqkCwpc3Rzn6wzlNvj4VDIjyfHshQj8hDsJqWm8xixiovnofyvuTol7j8fJQSJjVvPh8iyEgU ZNFvENTEmyLPjOV0vS7Thkaj9Vm+S7d6Gf8DrLMy/y4X9d4oNKNIjBXVSJZQZpx9K4Ouo1gQpJQ/o zP3vyW/g==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kn6Of-0008Rq-CT; Wed, 09 Dec 2020 20:49:13 +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 1kn6Od-0008RI-Cw; Wed, 09 Dec 2020 20:49:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1607546940; bh=cZK9lXe+lnP5mffhlgig8QrTV7IO6552Uuy5B8WoglA=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=DtsuGwCnXW+TGgzpJs9Hmkmi91Js//KOffuNTb6ilk2CjFYZ4mjzAgiTzVnO2SP5l 9dyd8kUfeTjF5qr53mMPRb3hBOa65krsEuPOy5kV8VPjrtZuB+25pt7xlk84zGxmZW OQBPEsIOLGaDgzSTpb248isrf0EbrLcmIaTFCaPc= 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 1MZktZ-1kbIpI3RLD-00WqNi; Wed, 09 Dec 2020 21:48:59 +0100 From: Lino Sanfilippo To: thierry.reding@gmail.com Subject: [PATCH v4] pwm: bcm2835: Support apply function for atomic configuration Date: Wed, 9 Dec 2020 21:48:25 +0100 Message-Id: <1607546905-20549-1-git-send-email-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Provags-ID: V03:K1:Jv47Zx8hJDQO6NMVX5/d9WEbTrKJ8taFw5fjTtl2u6kKHkEV1aG +no7QZOePQ3HfIvSvBco9NIs4V9CsEliAJ6eUxtnV4IHcI/7izOqxILns33APkl835Fi+R4 BLmleqNWmAoc596/3YvWonROoK9V7+ibWgkPkRLyAawgxNo6QfgTuX8RRBm0R9Wh1H2ou9P Kp7ql3cGJ6CC6rF5nPqSw== X-UI-Out-Filterresults: notjunk:1;V03:K0:hm6ziXOeYcc=:sppEYLbECBwTAJKPqBS/sL 8WZMMd9JMQPE08AduLKgl++IFUwCYidEKVX0ZApGDQCrzq4CEvgWkM028oIu+49ktGXVwwQGw y7IKuILyGym65vXnwnpnqyHfi7Gk9xavEAiw2BNrXvV4iDzdTtp5dE6+nZdjet6BAQHCZ3fsd mcCXwlPhXMg+yDQ4xEoQYyM17XEO38uJAo0H7d5VS8A5eiNo4yE3y+ip3nL9epCZ/1qisyeKa BT8plJojAP6n/930GyNPJa/6ElVhDVRkTsnJbBbhKE/8Hn1pDwedOog1Tseq6yWHcNDPr14MQ m3SLQegNrQ4/6XEflPKKuX7NB74U9ATT+67zKLh1+WGYP1eykaTrLOS+Ru/rGcf5zMZesSad0 gG9UjujE0xKGBf0/Tn4dqUZvqClUSKJCXWEz/YzEUGKyjOAOS7pSfYkxVU8cBDB+PjbSvSej7 5UCJ4Wz2jN4h7T/USc8V364OGUYJM1gN7OTy1s9/iMV7MCU4u4TzGZI0ijPlbzyikXDTbZrEX v3hdxmDkdws67bxcUpvtpblxhlUvU/Xpnmt3qjQCZPriBQSnJtYVoGtbpPnhJo/a/18eJtnlX alBxLxsQhNIaWFnK8lKa7BfDjsBMZqgYju2fxC8M1T0PFooau3IuaZjoQEV7GzxzHhgxjdAoT qUcd3trgwPSDfD02fqffMlru1rNG+DOL+yqk8PLUddHrOz9F560IdRBJtiZdjOSUADa9AQfoa Whqz597mF9EBLLxNwQP+SH0ROBr4JNjgGhyjaYsvsO7kFlealXtFBg1qfAv4M9XbYhzaJkUBh stqDmjBjboya8cHcVP3dJfsCclFgRPmBYPlG04z6JI9wAslwn9A/zD2+d/+a4IyF7LSMjaVih 2cOLFY0+eE/J2rSPUEHQ== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201209_154911_738082_839958CF X-CRM114-Status: GOOD ( 17.42 ) 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 --- v4: Remove a superfluous blank line Remove an unneeded cast (both requested by Uwe Kleine-König) v3: Check against period truncation (also based on a review by Uwe) v2: Fix compiler error for 64 bit builds drivers/pwm/pwm-bcm2835.c | 69 +++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 45 deletions(-) j diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c index d78f86f..4b1ee44 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,54 +74,34 @@ 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); + /* set duty cycle */ + val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, scaler); + writel(val, pc->base + DUTY(pwm->hwpwm)); - return 0; -} + /* set polarity */ + val = readl(pc->base + PWM_CONTROL); -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 (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; } @@ -127,10 +109,7 @@ static int bcm2835_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm, 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, };