mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Màxim Pedraza Padilla" <maximpedraza@gmail.com>
To: "Uwe Kleine-König" <ukleinek@kernel.org>
Cc: linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] pwm: tiecap: Implement .get_state() and keep a running PWM
Date: Mon, 16 Nov 2026 15:22:51 +0100	[thread overview]
Message-ID: <20261116142251.1296742-1-maximpedraza@gmail.com> (raw)

The driver has no .get_state(), so a PWM left running by the bootloader,
for instance for a backlight, is seen as disabled and the consumer may
turn it off before enabling it again, which makes the screen flicker.

Read the state from the hardware. While the PWM runs, .apply() writes
the shadow registers, loaded at the end of the period, so read those to
stay consistent with it, and copy the active values into them at probe
in case the bootloader didn't set them.

A running PWM also holds the runtime PM reference ecap_pwm_enable()
takes, so take it at probe. Switch to devm_pm_runtime_enable() so the
remove callback can go.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Màxim Pedraza Padilla <maximpedraza@gmail.com>
---
Tested on an AM335x board with and without CONFIG_PWM_DEBUG, with the PWM
left running by the bootloader and from a cold start, changing the
brightness and turning the backlight off and on.

On AM335x the PWMSS needs ti,no-reset-on-init in its ti-sysc node,
otherwise the module is reset before this driver probes and there is
nothing left to read back.

 drivers/pwm/pwm-tiecap.c | 71 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 9 deletions(-)

diff --git a/drivers/pwm/pwm-tiecap.c b/drivers/pwm/pwm-tiecap.c
index 3751737ead43..9a490b8d60ec 100644
--- a/drivers/pwm/pwm-tiecap.c
+++ b/drivers/pwm/pwm-tiecap.c
@@ -28,6 +28,7 @@
 #define ECCTL2_APWM_MODE	BIT(9)
 #define ECCTL2_SYNC_SEL_DISA	(BIT(7) | BIT(6))
 #define ECCTL2_TSCTR_FREERUN	BIT(4)
+#define ECCTL2_RUNNING		(ECCTL2_APWM_MODE | ECCTL2_TSCTR_FREERUN)
 
 struct ecap_context {
 	u32 cap3;
@@ -206,8 +207,42 @@ static int ecap_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	return 0;
 }
 
+static int ecap_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+			      struct pwm_state *state)
+{
+	struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
+	u32 period_cycles, duty_cycles;
+	u16 value;
+	int ret;
+
+	ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
+	if (ret)
+		return ret;
+
+	value = readw(pc->mmio_base + ECCTL2);
+	state->enabled = (value & ECCTL2_RUNNING) == ECCTL2_RUNNING;
+	/*
+	 * While running, .apply() writes the shadow registers, loaded at the
+	 * end of the period.
+	 */
+	period_cycles = readl(pc->mmio_base + (state->enabled ? CAP3 : CAP1));
+	duty_cycles = readl(pc->mmio_base + (state->enabled ? CAP4 : CAP2));
+
+	pm_runtime_put_sync(pwmchip_parent(chip));
+
+	state->polarity = value & ECCTL2_APWM_POL_LOW ? PWM_POLARITY_INVERSED :
+							PWM_POLARITY_NORMAL;
+	state->period = DIV_ROUND_UP_ULL((u64)period_cycles * NSEC_PER_SEC,
+					 pc->clk_rate);
+	state->duty_cycle = DIV_ROUND_UP_ULL((u64)duty_cycles * NSEC_PER_SEC,
+					     pc->clk_rate);
+
+	return 0;
+}
+
 static const struct pwm_ops ecap_pwm_ops = {
 	.apply = ecap_pwm_apply,
+	.get_state = ecap_pwm_get_state,
 };
 
 static const struct of_device_id ecap_of_match[] = {
@@ -223,6 +258,7 @@ static int ecap_pwm_probe(struct platform_device *pdev)
 	struct ecap_pwm_chip *pc;
 	struct pwm_chip *chip;
 	struct clk *clk;
+	bool running;
 	int ret;
 
 	chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*pc));
@@ -255,23 +291,41 @@ static int ecap_pwm_probe(struct platform_device *pdev)
 	if (IS_ERR(pc->mmio_base))
 		return PTR_ERR(pc->mmio_base);
 
+	platform_set_drvdata(pdev, chip);
+
+	ret = devm_pm_runtime_enable(&pdev->dev);
+	if (ret)
+		return ret;
+
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret)
+		return ret;
+
+	/*
+	 * A PWM left running by the bootloader holds the reference
+	 * ecap_pwm_enable() takes.
+	 */
+	running = (readw(pc->mmio_base + ECCTL2) & ECCTL2_RUNNING) ==
+		  ECCTL2_RUNNING;
+	if (running) {
+		/* The bootloader may have left the shadow registers unset */
+		writel(readl(pc->mmio_base + CAP1), pc->mmio_base + CAP3);
+		writel(readl(pc->mmio_base + CAP2), pc->mmio_base + CAP4);
+	} else {
+		pm_runtime_put_sync(&pdev->dev);
+	}
+
 	ret = devm_pwmchip_add(&pdev->dev, chip);
 	if (ret < 0) {
+		if (running)
+			pm_runtime_put_sync(&pdev->dev);
 		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
 		return ret;
 	}
 
-	platform_set_drvdata(pdev, chip);
-	pm_runtime_enable(&pdev->dev);
-
 	return 0;
 }
 
-static void ecap_pwm_remove(struct platform_device *pdev)
-{
-	pm_runtime_disable(&pdev->dev);
-}
-
 static void ecap_pwm_save_context(struct pwm_chip *chip)
 {
 	struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
@@ -328,7 +382,6 @@ static struct platform_driver ecap_pwm_driver = {
 		.pm = pm_ptr(&ecap_pwm_pm_ops),
 	},
 	.probe = ecap_pwm_probe,
-	.remove = ecap_pwm_remove,
 };
 module_platform_driver(ecap_pwm_driver);
 

base-commit: 3f9bd3ccc80af2684cfc0901757f85a78358cbab
-- 
2.39.5


                 reply	other threads:[~2026-11-16 13:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261116142251.1296742-1-maximpedraza@gmail.com \
    --to=maximpedraza@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=ukleinek@kernel.org \
    /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

all inboxes | Powered by JetHome®