Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6552976imu; Mon, 21 Jan 2019 10:55:25 -0800 (PST) X-Google-Smtp-Source: ALg8bN6pA8oKkue8q+4yDif+nd9cVh9uoYBebTGkv2k+mylx3DgG4thnTRnBSrwQ5xG8c43qmJdJ X-Received: by 2002:a63:d655:: with SMTP id d21mr29161067pgj.280.1548096925508; Mon, 21 Jan 2019 10:55:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548096925; cv=none; d=google.com; s=arc-20160816; b=PkgBWONM2Q/yrfcPuj+mAh1KBQXAIqY67CyrLtLLqdsxRgb2JEH9OQ62Rzd4qBRemu sPOqU8ateXJY4dYOHsMctRLrQzuqhnGhuYfm+DZir9jDxE5bEVXlfVc1VnoQBbVsEDRH T+BVq+QVYGsP04fEKZIt6tgww0N+yE6kV5j2qaC8TCYviV2NtAhZNYzdnYOcym9XAN+V HfLmTIq4p4Ms19qXJCZnh0d4vIRx0or7+kKBCUMrn2EInfyIUuN1sQux48Z6vfQHfqsD CgiRNEV1NcJdOwiZjwfqLw04BmCxJDDRXA2F3ZsRVFENG/l0gDjBx5tKRX64X0izZ5vQ Xk+Q== 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=K1Zy3wQK8nWArPG5QICEEz2xu4QrWQ7ZC3WFfMwWL+s=; b=UIXS2Yd45m42oTdQNOvfVZQ8GkvWYzcqjEYTwByAa1E4ge6KW5Csr9nAeyPopEWALr sGa5MpqDpBLp11viap/Bu9v0vcb4ky6S9sXWOLFNDuZ5tjNEwoxfWxrNDrRpA5GT9OIk o9YL05qDclJqDHp1kK5AAlld6M2iyg0LLPtAbRanTycTKxLReAPe2t0YPOcyx1tZ3Byl zUwvtHyggljm5oXa+3zMiHSrLJZe/ax3pMpmfHPDoRrmlrYe4K0IVPOzDnffr5TkHCOZ OP1Z9EXpflahp1PFx4E7ijqEJruZAXBpIiuifvbB6V2/EchnyV/LDRWbyUhad/JVLKih LwVA== 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 g21si12448840pgl.114.2019.01.21.10.55.09; Mon, 21 Jan 2019 10:55:25 -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 S1728030AbfAUSyB (ORCPT + 99 others); Mon, 21 Jan 2019 13:54:01 -0500 Received: from metis.ext.pengutronix.de ([85.220.165.71]:37599 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726575AbfAUSyB (ORCPT ); Mon, 21 Jan 2019 13:54:01 -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 1glehm-0003x1-Pl; Mon, 21 Jan 2019 19:53:54 +0100 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1glehm-0004Xa-1J; Mon, 21 Jan 2019 19:53:54 +0100 Date: Mon, 21 Jan 2019 19:53:53 +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, Praveen Kumar B Subject: Re: [PATCH V2 3/4] drivers: pwm: pwm-bcm-kona: Add cygnus-pwm support Message-ID: <20190121185353.fam72wk5eeyz3kwy@pengutronix.de> References: <1547669716-20070-1-git-send-email-sheetal.tigadoli@broadcom.com> <1547669716-20070-4-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-4-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:15AM +0530, Sheetal Tigadoli wrote: > From: Praveen Kumar B > > Add support for new version of pwm-cygnus. > Add support to make PWM changes configured and stable. > > Signed-off-by: Praveen Kumar B > Reviewed-by: Ray Jui > Reviewed-by: Scott Branden > Signed-off-by: Sheetal Tigadoli > --- > drivers/pwm/pwm-bcm-kona.c | 95 ++++++++++++++++++++++++++++++++++++---------- > 1 file changed, 76 insertions(+), 19 deletions(-) > > diff --git a/drivers/pwm/pwm-bcm-kona.c b/drivers/pwm/pwm-bcm-kona.c > index fe63289..143843f 100644 > --- a/drivers/pwm/pwm-bcm-kona.c > +++ b/drivers/pwm/pwm-bcm-kona.c > @@ -15,6 +15,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -65,10 +66,19 @@ > #define DUTY_CYCLE_HIGH_MIN (0x00000000) > #define DUTY_CYCLE_HIGH_MAX (0x00ffffff) > > +#define PWM_MONITOR_OFFSET 0xb0 > +#define PWM_MONITOR_TIMEOUT_US 5 > + > +enum kona_pwmc_ver { > + KONA_PWM = 1, > + CYGNUS_PWM > +}; > + > struct kona_pwmc { > struct pwm_chip chip; > void __iomem *base; > struct clk *clk; > + enum kona_pwmc_ver version; > }; > > static inline struct kona_pwmc *to_kona_pwmc(struct pwm_chip *_chip) > @@ -76,10 +86,36 @@ static inline struct kona_pwmc *to_kona_pwmc(struct pwm_chip *_chip) > return container_of(_chip, struct kona_pwmc, chip); > } > > +static int kona_pwmc_wait_stable(struct pwm_chip *chip, unsigned int chan, > + unsigned int kona_ver) > +{ > + struct kona_pwmc *kp = to_kona_pwmc(chip); > + unsigned int value; > + > + switch (kona_ver) { > + case KONA_PWM: > + /* > + * There must be a min 400ns delay between clearing trigger and > + * settingit. Failing to do this may result in no PWM signal. > + */ > + ndelay(400); > + return 0; > + > + case CYGNUS_PWM: > + return readl_poll_timeout(kp->base + PWM_MONITOR_OFFSET, value, > + !(value & (BIT(chan))), 0, > + PWM_MONITOR_TIMEOUT_US); > + > + default: > + return -ENODEV; > + > + } > +} This function is the only difference between these two otherwise similar implementations. If you do the following instead: static int kona_pwmc_wait_stable(struct pwm_chip *chip, unsigned int chan) { ndelay(400); return 0; } static int cygnus_pwmc_wait_stable(struct pwm_chip *chip, unsigned int chan) { return readl_poll_timeout(...); } and then maintain a int (*wait_stable)(struct pwm_chip *, unsigned int); in struct kona_pwmc that is initialized in .probe you save quite some checks for the version. > + > /* > * Clear trigger bit but set smooth bit to maintain old output. > */ > -static void kona_pwmc_prepare_for_settings(struct kona_pwmc *kp, > +static int kona_pwmc_prepare_for_settings(struct kona_pwmc *kp, > unsigned int chan) > { > unsigned int value = readl(kp->base + PWM_CONTROL_OFFSET); > @@ -88,14 +124,10 @@ static void kona_pwmc_prepare_for_settings(struct kona_pwmc *kp, > value &= ~(1 << PWM_CONTROL_TRIGGER_SHIFT(chan)); > writel(value, kp->base + PWM_CONTROL_OFFSET); > > - /* > - * There must be a min 400ns delay between clearing trigger and setting > - * it. Failing to do this may result in no PWM signal. > - */ > - ndelay(400); > + return kona_pwmc_wait_stable(&kp->chip, chan, kp->version); > } > > -static void kona_pwmc_apply_settings(struct kona_pwmc *kp, unsigned int chan) > +static int kona_pwmc_apply_settings(struct kona_pwmc *kp, unsigned int chan) > { > unsigned int value = readl(kp->base + PWM_CONTROL_OFFSET); > > @@ -104,8 +136,7 @@ static void kona_pwmc_apply_settings(struct kona_pwmc *kp, unsigned int chan) > value |= 1 << PWM_CONTROL_TRIGGER_SHIFT(chan); > writel(value, kp->base + PWM_CONTROL_OFFSET); > > - /* Trigger bit must be held high for at least 400 ns. */ > - ndelay(400); > + return kona_pwmc_wait_stable(&kp->chip, chan, kp->version); > } > > static void kona_pwmc_disable(struct pwm_chip *chip, struct pwm_device *pwm) > @@ -113,8 +144,13 @@ 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; > > - kona_pwmc_prepare_for_settings(kp, chan); > + ret = kona_pwmc_prepare_for_settings(kp, chan); > + if (ret < 0) { > + dev_err(chip->dev, "failed to prepare pwm settings: %d\n", ret); > + return; > + } > > /* Simulate a disable by configuring for zero duty */ > writel(0, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan)); > @@ -125,7 +161,11 @@ static void kona_pwmc_disable(struct pwm_chip *chip, struct pwm_device *pwm) > value &= ~PRESCALE_MASK(chan); > writel(value, kp->base + PRESCALE_OFFSET); > > - kona_pwmc_apply_settings(kp, chan); > + ret = kona_pwmc_apply_settings(kp, chan); > + if (ret < 0) { > + dev_err(chip->dev, "failed to apply pwm settings: %d\n", ret); > + return; I think it would make sense to change kona_pwmc_disable to return an error code and then use that in the callers. > + } > > clk_disable_unprepare(kp->clk); > } > @@ -188,7 +228,12 @@ static int kona_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm, > } > } > > - kona_pwmc_prepare_for_settings(kp, chan); > + ret = kona_pwmc_prepare_for_settings(kp, chan); > + if (ret < 0) { > + dev_err(chip->dev, > + "failed to prepare pwm settings: %d\n", ret); > + return ret; > + } > > value = readl(kp->base + PRESCALE_OFFSET); > value &= ~PRESCALE_MASK(chan); > @@ -208,7 +253,12 @@ static int kona_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm, > writel(value, kp->base + PWM_CONTROL_OFFSET); > } > > - kona_pwmc_apply_settings(kp, chan); > + ret = kona_pwmc_apply_settings(kp, chan); > + if (ret < 0) { > + dev_err(chip->dev, > + "failed to prepare pwm settings: %d\n", ret); > + return ret; > + } > } else if (cstate.enabled) { > kona_pwmc_disable(chip, pwm); > } > @@ -221,14 +271,26 @@ static int kona_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm, > .owner = THIS_MODULE, > }; > > +static const struct of_device_id bcm_kona_pwmc_dt[] = { > + { .compatible = "brcm,kona-pwm", .data = (void *)KONA_PWM}, > + { .compatible = "brcm,cygnus-pwm", .data = (void *)CYGNUS_PWM}, > + { }, > +}; > +MODULE_DEVICE_TABLE(of, bcm_kona_pwmc_dt); > + > static int kona_pwmc_probe(struct platform_device *pdev) > { > struct kona_pwmc *kp; > struct resource *res; > + const struct of_device_id *of_id; > unsigned int chan; > unsigned int value = 0; > int ret = 0; > > + of_id = of_match_node(bcm_kona_pwmc_dt, pdev->dev.of_node); > + if (!of_id) > + return -ENODEV; > + > kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL); > if (kp == NULL) > return -ENOMEM; > @@ -241,6 +303,7 @@ static int kona_pwmc_probe(struct platform_device *pdev) > kp->chip.npwm = 6; > kp->chip.of_xlate = of_pwm_xlate_with_flags; > kp->chip.of_pwm_n_cells = 3; > + kp->version = (enum kona_pwmc_ver)of_id->data; > > res = platform_get_resource(pdev, IORESOURCE_MEM, 0); > kp->base = devm_ioremap_resource(&pdev->dev, res); > @@ -287,12 +350,6 @@ static int kona_pwmc_remove(struct platform_device *pdev) > return pwmchip_remove(&kp->chip); > } > > -static const struct of_device_id bcm_kona_pwmc_dt[] = { > - { .compatible = "brcm,kona-pwm" }, > - { }, > -}; > -MODULE_DEVICE_TABLE(of, bcm_kona_pwmc_dt); > - > static struct platform_driver kona_pwmc_driver = { > .driver = { > .name = "bcm-kona-pwm", Best regards Uwe -- Pengutronix e.K. | Uwe Kleine-K?nig | Industrial Linux Solutions | http://www.pengutronix.de/ |