devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
To: linus.walleij@linaro.org, geert+renesas@glider.be,
	thierry.reding@gmail.com, robh+dt@kernel.org,
	mark.rutland@arm.com
Cc: linux-gpio@vger.kernel.org, linux-pwm@vger.kernel.org,
	devicetree@vger.kernel.org, linux-renesas-soc@vger.kernel.org,
	Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
Subject: [PATCH RFC 6/7] pwm: rcar: Add gpio support to output duty zero
Date: Mon,  8 Jul 2019 18:07:47 +0900	[thread overview]
Message-ID: <1562576868-8124-7-git-send-email-yoshihiro.shimoda.uh@renesas.com> (raw)
In-Reply-To: <1562576868-8124-1-git-send-email-yoshihiro.shimoda.uh@renesas.com>

The R-Car SoCs PWM Timer cannot output duty zero. So, this patch
adds gpio support to output it.

Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
---
 drivers/pwm/pwm-rcar.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/pwm-rcar.c b/drivers/pwm/pwm-rcar.c
index c8cd43f..1c19a8b 100644
--- a/drivers/pwm/pwm-rcar.c
+++ b/drivers/pwm/pwm-rcar.c
@@ -7,6 +7,7 @@
 
 #include <linux/clk.h>
 #include <linux/err.h>
+#include <linux/gpio/consumer.h>
 #include <linux/io.h>
 #include <linux/log2.h>
 #include <linux/math64.h>
@@ -38,6 +39,7 @@ struct rcar_pwm_chip {
 	struct pwm_chip chip;
 	void __iomem *base;
 	struct clk *clk;
+	struct gpio_desc *gpio;
 };
 
 static inline struct rcar_pwm_chip *to_rcar_pwm_chip(struct pwm_chip *chip)
@@ -119,8 +121,11 @@ static int rcar_pwm_set_counter(struct rcar_pwm_chip *rp, int div, int duty_ns,
 	ph = tmp & RCAR_PWMCNT_PH0_MASK;
 
 	/* Avoid prohibited setting */
-	if (cyc == 0 || ph == 0)
+	if (cyc == 0)
 		return -EINVAL;
+	/* Try to use GPIO to output duty zero */
+	if (ph == 0)
+		return -EAGAIN;
 
 	rcar_pwm_write(rp, cyc | ph, RCAR_PWMCNT);
 
@@ -157,6 +162,28 @@ static void rcar_pwm_disable(struct rcar_pwm_chip *rp)
 	rcar_pwm_update(rp, RCAR_PWMCR_EN0, 0, RCAR_PWMCR);
 }
 
+static int rcar_pwm_gpiod_get(struct rcar_pwm_chip *rp)
+{
+	if (rp->gpio)
+		return 0;
+
+	rp->gpio = gpiod_get(rp->chip.dev, "renesas,duty-zero", GPIOD_OUT_LOW);
+	if (!IS_ERR(rp->gpio))
+		return 0;
+
+	rp->gpio = NULL;
+	return -EINVAL;
+}
+
+static void rcar_pwm_gpiod_put(struct rcar_pwm_chip *rp)
+{
+	if (!rp->gpio)
+		return;
+
+	gpiod_put(rp->gpio);
+	rp->gpio = NULL;
+}
+
 static int rcar_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			  struct pwm_state *state)
 {
@@ -171,6 +198,7 @@ static int rcar_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	if (!state->enabled) {
 		rcar_pwm_disable(rp);
+		rcar_pwm_gpiod_put(rp);
 		return 0;
 	}
 
@@ -187,8 +215,12 @@ static int rcar_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	/* The SYNC should be set to 0 even if rcar_pwm_set_counter failed */
 	rcar_pwm_update(rp, RCAR_PWMCR_SYNC, 0, RCAR_PWMCR);
 
-	if (!ret)
+	if (!ret) {
 		ret = rcar_pwm_enable(rp);
+		rcar_pwm_gpiod_put(rp);
+	} else if (ret == -EAGAIN) {
+		ret = rcar_pwm_gpiod_get(rp);
+	}
 
 	return ret;
 }
-- 
2.7.4

  parent reply	other threads:[~2019-07-08  9:07 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-08  9:07 [PATCH RFC 0/7] treewide: modify sh-pfc and add support pwm duty zero Yoshihiro Shimoda
2019-07-08  9:07 ` [PATCH RFC 1/7] pinctrl: sh-pfc: add new flags into struct sh_pfc_pin_config Yoshihiro Shimoda
2019-07-08 15:51   ` Sergei Shtylyov
2019-08-06  8:46   ` Geert Uytterhoeven
2019-07-08  9:07 ` [PATCH RFC 2/7] pinctrl: sh-pfc: remove incomplete flag "cfg->type" Yoshihiro Shimoda
2019-07-28 23:02   ` Linus Walleij
2019-07-29  5:16     ` Yoshihiro Shimoda
2019-08-06  8:49       ` Geert Uytterhoeven
2019-08-06  9:23   ` Geert Uytterhoeven
2019-08-06 11:48     ` Yoshihiro Shimoda
2019-07-08  9:07 ` [PATCH RFC 3/7] pinctrl: sh-pfc: Rollback to mux if requires when the gpio is freed Yoshihiro Shimoda
2019-08-06  9:02   ` Geert Uytterhoeven
2019-08-06 11:38     ` Yoshihiro Shimoda
2019-08-06 12:01       ` Geert Uytterhoeven
2019-07-08  9:07 ` [PATCH RFC 4/7] dt-bindings: pwm: rcar: Add specific gpios property to output duty zero Yoshihiro Shimoda
2019-08-06  9:21   ` Geert Uytterhoeven
2019-07-08  9:07 ` [PATCH RFC 5/7] pwm: rcar: remove a redundant condition in rcar_pwm_apply() Yoshihiro Shimoda
2019-08-06  9:05   ` Geert Uytterhoeven
2019-08-06 11:39     ` Yoshihiro Shimoda
2019-08-06 16:00     ` Uwe Kleine-König
2019-08-07  2:56       ` Yoshihiro Shimoda
2019-08-07  6:33   ` Uwe Kleine-König
2019-07-08  9:07 ` Yoshihiro Shimoda [this message]
2019-08-07  7:03   ` [PATCH RFC 6/7] pwm: rcar: Add gpio support to output duty zero Uwe Kleine-König
2019-08-08  3:52     ` Yoshihiro Shimoda
2019-08-08  6:49       ` Geert Uytterhoeven
2019-08-08  7:02         ` Yoshihiro Shimoda
2019-08-08  7:31       ` Uwe Kleine-König
2019-07-08  9:07 ` [PATCH RFC 7/7] arm64: dts: renesas: salvator-common: add gpio property into pwm1 Yoshihiro Shimoda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1562576868-8124-7-git-send-email-yoshihiro.shimoda.uh@renesas.com \
    --to=yoshihiro.shimoda.uh@renesas.com \
    --cc=devicetree@vger.kernel.org \
    --cc=geert+renesas@glider.be \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=thierry.reding@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).