Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6548476imu; Mon, 21 Jan 2019 10:49:30 -0800 (PST) X-Google-Smtp-Source: ALg8bN7gUZ8KRsNjoH9h5ZffYGlDw7Dc8us4HYodiaHF08AWSfg1qpZD4cCcFS/oD+fKJCFiOM6b X-Received: by 2002:a17:902:4025:: with SMTP id b34mr31499855pld.181.1548096570767; Mon, 21 Jan 2019 10:49:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548096570; cv=none; d=google.com; s=arc-20160816; b=U/dW/jGAYbzfCbd4v8PNhe9h8wfZKnyb1lde73esDua4Y49BizY7joLoFhx7UW0M0z bbMDpjASfDMsSrPsIEfffaE65FsYBDm5l1G+PbdsHFCa+t6iz4OOVdmD6zTZS1ZX2zca pMS4eabcMqJNast8zPXL56o2UaaL5mYPpcnQe2onF4FegGT6UM1JzRtWZDMuKnH9vdRJ FuJ2PpOJS208byfCWuxvo1VBF+Z8UjFbHhQXFIlAsMTBdQaoY0jKB9Z2V0QPg6K66lA+ 3yiKG6tQZk4xzltLnaiYCJBIIniOqcg+CICwgWMrCF+f9n9jjal4SmVRoWdbWLBvbU81 DCLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=ZcdO1HyCmtZzzlMwvIoxj++BUyYPS2IqGvZ2IJi5X2M=; b=w+xJhK/OYrGvfojwfeiVq+C1dKNBMrEx8yVpepd+Txph7ydx69ZKWxHfTd7fkmowf6 0+Xm+oUiuKzsQbR9B14bKROvEwYg2capRT3ZWsKx6Qt6BBAtlAlotSA+g4LSUR0R/KmY yfpTKe9JSe0oHBsmgvhpnje9A5ZCUAv0rVGmLUMqtM98rWnNvb5yzDFXIM/LleKPKi7T KYWwK2l9Cw1/KSdbaGb3hWIQEI5FkCSWCCOoSaMVsNS2B6GU7emiHzGx/tAKBwKO12L0 M8PhKja9MM3me/Qs0HdgYsEOQzcVzcGO4aze5wBak03mIjyCjN4WisMIsBrIeFCerEpr 2xUA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t77si10781pgb.51.2019.01.21.10.49.14; Mon, 21 Jan 2019 10:49:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728198AbfAUSqp (ORCPT + 99 others); Mon, 21 Jan 2019 13:46:45 -0500 Received: from metis.ext.pengutronix.de ([85.220.165.71]:51737 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727709AbfAUSqp (ORCPT ); Mon, 21 Jan 2019 13:46:45 -0500 Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gleak-0003E7-Cu; Mon, 21 Jan 2019 19:46:38 +0100 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1gleaj-0004Pp-RH; Mon, 21 Jan 2019 19:46:37 +0100 Date: Mon, 21 Jan 2019 19:46:37 +0100 From: Uwe =?iso-8859-1?Q?Kleine-K=F6nig?= To: Sheetal Tigadoli Cc: Thierry Reding , Rob Herring , Mark Rutland , Florian Fainelli , Ray Jui , Scott Branden , bcm-kernel-feedback-list@broadcom.com, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel@pengutronix.de Subject: Re: [PATCH V2 2/4] drivers: pwm: pwm-bcm-kona: Switch to using atomic PWM Framework Message-ID: <20190121184637.httmrtd3coz2m5ie@pengutronix.de> References: <1547669716-20070-1-git-send-email-sheetal.tigadoli@broadcom.com> <1547669716-20070-3-git-send-email-sheetal.tigadoli@broadcom.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1547669716-20070-3-git-send-email-sheetal.tigadoli@broadcom.com> User-Agent: NeoMutt/20170113 (1.7.2) X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, On Thu, Jan 17, 2019 at 01:45:14AM +0530, Sheetal Tigadoli wrote: > Switch to using atomic PWM Framework on broadcom PWM kona driver > > Signed-off-by: Sheetal Tigadoli > --- > drivers/pwm/pwm-bcm-kona.c | 201 +++++++++++++++++++-------------------------- > 1 file changed, 83 insertions(+), 118 deletions(-) > > diff --git a/drivers/pwm/pwm-bcm-kona.c b/drivers/pwm/pwm-bcm-kona.c > index 09a95ae..fe63289 100644 > --- a/drivers/pwm/pwm-bcm-kona.c > +++ b/drivers/pwm/pwm-bcm-kona.c > @@ -108,151 +108,116 @@ static void kona_pwmc_apply_settings(struct kona_pwmc *kp, unsigned int chan) > ndelay(400); > } > > -static int kona_pwmc_config(struct pwm_chip *chip, struct pwm_device *pwm, > - int duty_ns, int period_ns) > -{ > - struct kona_pwmc *kp = to_kona_pwmc(chip); > - u64 val, div, rate; > - unsigned long prescale = PRESCALE_MIN, pc, dc; > - unsigned int value, chan = pwm->hwpwm; > - > - /* > - * Find period count, duty count and prescale to suit duty_ns and > - * period_ns. This is done according to formulas described below: > - * > - * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE > - * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE > - * > - * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1)) > - * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1)) > - */ > - > - rate = clk_get_rate(kp->clk); > - > - while (1) { > - div = 1000000000; > - div *= 1 + prescale; > - val = rate * period_ns; > - pc = div64_u64(val, div); > - val = rate * duty_ns; > - dc = div64_u64(val, div); > - > - /* If duty_ns or period_ns are not achievable then return */ > - if (pc < PERIOD_COUNT_MIN || dc < DUTY_CYCLE_HIGH_MIN) > - return -EINVAL; > - > - /* If pc and dc are in bounds, the calculation is done */ > - if (pc <= PERIOD_COUNT_MAX && dc <= DUTY_CYCLE_HIGH_MAX) > - break; > - > - /* Otherwise, increase prescale and recalculate pc and dc */ > - if (++prescale > PRESCALE_MAX) > - return -EINVAL; > - } > - > - /* > - * Don't apply settings if disabled. The period and duty cycle are > - * always calculated above to ensure the new values are > - * validated immediately instead of on enable. > - */ > - if (pwm_is_enabled(pwm)) { > - kona_pwmc_prepare_for_settings(kp, chan); > - > - value = readl(kp->base + PRESCALE_OFFSET); > - value &= ~PRESCALE_MASK(chan); > - value |= prescale << PRESCALE_SHIFT(chan); > - writel(value, kp->base + PRESCALE_OFFSET); > - > - writel(pc, kp->base + PERIOD_COUNT_OFFSET(chan)); > - > - writel(dc, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan)); > - > - kona_pwmc_apply_settings(kp, chan); > - } > - > - return 0; > -} > - > -static int kona_pwmc_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm, > - enum pwm_polarity polarity) > +static void kona_pwmc_disable(struct pwm_chip *chip, struct pwm_device *pwm) > { > struct kona_pwmc *kp = to_kona_pwmc(chip); > unsigned int chan = pwm->hwpwm; > unsigned int value; > - int ret; > - > - ret = clk_prepare_enable(kp->clk); > - if (ret < 0) { > - dev_err(chip->dev, "failed to enable clock: %d\n", ret); > - return ret; > - } > > kona_pwmc_prepare_for_settings(kp, chan); > > - value = readl(kp->base + PWM_CONTROL_OFFSET); > - > - if (polarity == PWM_POLARITY_NORMAL) > - value |= 1 << PWM_CONTROL_POLARITY_SHIFT(chan); > - else > - value &= ~(1 << PWM_CONTROL_POLARITY_SHIFT(chan)); > + /* Simulate a disable by configuring for zero duty */ > + writel(0, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan)); > + writel(0, kp->base + PERIOD_COUNT_OFFSET(chan)); > > - writel(value, kp->base + PWM_CONTROL_OFFSET); > + /* Set prescale to 0 for this channel */ kona_pwmc_apply uses PRESCALE_MIN instead of a plain 0. To make the comment more helpful tell *why* you do it instead of stating the obvious for the fluent C programmer. > + value = readl(kp->base + PRESCALE_OFFSET); > + value &= ~PRESCALE_MASK(chan); > + writel(value, kp->base + PRESCALE_OFFSET); > > kona_pwmc_apply_settings(kp, chan); > > clk_disable_unprepare(kp->clk); > - > - return 0; > } > > -static int kona_pwmc_enable(struct pwm_chip *chip, struct pwm_device *pwm) > +static int kona_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm, > + struct pwm_state *state) > { > struct kona_pwmc *kp = to_kona_pwmc(chip); > + struct pwm_state cstate; > + u64 val, div, rate; > + unsigned long prescale = PRESCALE_MIN, pc, dc; > + unsigned int value, chan = pwm->hwpwm; > int ret; > > - ret = clk_prepare_enable(kp->clk); > - if (ret < 0) { > - dev_err(chip->dev, "failed to enable clock: %d\n", ret); > - return ret; > - } > - > - ret = kona_pwmc_config(chip, pwm, pwm_get_duty_cycle(pwm), > - pwm_get_period(pwm)); > - if (ret < 0) { > - clk_disable_unprepare(kp->clk); > - return ret; > - } > + pwm_get_state(pwm, &cstate); The pwm_get_state function is designed for PWM-consumers. It is an implementation detail that it also works for drivers. So I'd like to see its usage dropped in drivers. (Note that Thierry might not agree here.) > + > + if (state->enabled) { > + /* > + * Find period count, duty count and prescale to suit duty_ns > + * and period_ns. This is done according to formulas described > + * below: > + * > + * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE > + * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE > + * > + * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1)) > + * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1)) > + */ > + rate = clk_get_rate(kp->clk); > + > + while (1) { > + div = 1000000000; > + div *= 1 + prescale; > + val = rate * state->period; > + pc = div64_u64(val, div); > + val = rate * state->duty_cycle; > + dc = div64_u64(val, div); > + > + /* If duty_ns or period_ns are not achievable then Please stick to the usual style for multi-line comments, i.e. "/*" on a separate line. > + * return > + */ > + if (pc < PERIOD_COUNT_MIN || dc < DUTY_CYCLE_HIGH_MIN) > + return -EINVAL; > + > + /* If pc & dc are in bounds, the calculation is done */ > + if (pc <= PERIOD_COUNT_MAX && dc <= DUTY_CYCLE_HIGH_MAX) > + break; > + > + /* Otherwise, increase prescale & recalculate pc & dc */ > + if (++prescale > PRESCALE_MAX) > + return -EINVAL; > + } > + > + if (!cstate.enabled) { > + ret = clk_prepare_enable(kp->clk); > + if (ret < 0) { > + dev_err(chip->dev, > + "failed to enable clock: %d\n", ret); > + return ret; > + } > + } > > - return 0; > -} > - > -static void kona_pwmc_disable(struct pwm_chip *chip, struct pwm_device *pwm) > -{ > - struct kona_pwmc *kp = to_kona_pwmc(chip); > - unsigned int chan = pwm->hwpwm; > - unsigned int value; > + kona_pwmc_prepare_for_settings(kp, chan); > > - kona_pwmc_prepare_for_settings(kp, chan); > + value = readl(kp->base + PRESCALE_OFFSET); > + value &= ~PRESCALE_MASK(chan); > + value |= prescale << PRESCALE_SHIFT(chan); > + writel(value, kp->base + PRESCALE_OFFSET); > + writel(pc, kp->base + PERIOD_COUNT_OFFSET(chan)); > + writel(dc, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan)); > > - /* Simulate a disable by configuring for zero duty */ > - writel(0, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan)); > - writel(0, kp->base + PERIOD_COUNT_OFFSET(chan)); > + if (cstate.polarity != state->polarity) { > + value = readl(kp->base + PWM_CONTROL_OFFSET); > + if (state->polarity == PWM_POLARITY_NORMAL) > + value |= 1 << PWM_CONTROL_POLARITY_SHIFT(chan); > + else > + value &= ~(1 << > + PWM_CONTROL_POLARITY_SHIFT(chan)); > > - /* Set prescale to 0 for this channel */ > - value = readl(kp->base + PRESCALE_OFFSET); > - value &= ~PRESCALE_MASK(chan); > - writel(value, kp->base + PRESCALE_OFFSET); > + writel(value, kp->base + PWM_CONTROL_OFFSET); > + } > > - kona_pwmc_apply_settings(kp, chan); > + kona_pwmc_apply_settings(kp, chan); > + } else if (cstate.enabled) { > + kona_pwmc_disable(chip, pwm); If I do: pwm_apply_state(pwm, { .polarity = PWM_POLARITY_NORMAL, .enabled = true, ... }); pwm_apply_state(pwm, { .polarity = PWM_POLARITY_INVERSED, .enabled = false, ... }); the output is constant low, which is wrong. Best regards Uwe -- Pengutronix e.K. | Uwe Kleine-K?nig | Industrial Linux Solutions | http://www.pengutronix.de/ |