From: 20190311120055 created <payne.lin@mediatek.com>
To: "Uwe Kleine-König" <ukleinek@kernel.org>,
"Matthias Brugger" <matthias.bgg@gmail.com>,
"AngeloGioacchino Del Regno"
<angelogioacchino.delregno@collabora.com>
Cc: <linux-pwm@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-mediatek@lists.infradead.org>,
<Project_Global_Chrome_Upstream_Group@mediatek.com>,
<sirius.wang@mediatek.com>, <vince-wl.liu@mediatek.com>,
<jh.hsu@mediatek.com>, Payne Lin <payne.lin@mediatek.com>
Subject: [PATCH] pwm: mediatek: Add error handling for zero rate in PWM state
Date: Mon, 15 Dec 2025 18:40:32 +0800 [thread overview]
Message-ID: <20251215104037.3100485-1-payne.lin@mediatek.com> (raw)
From: Payne Lin <payne.lin@mediatek.com>
Added a check to handle cases where the rate is zero in the
mtk_disp_pwm_get_state function. This prevents division by zero errors
when calculating the period.
- Added error message for zero rate scenario
Signed-off-by: Payne Lin <payne.lin@mediatek.com>
---
drivers/pwm/pwm-mtk-disp.c | 31 +++++++++++++++++++------------
1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/drivers/pwm/pwm-mtk-disp.c b/drivers/pwm/pwm-mtk-disp.c
index bafd6b6195f6..dd0ae041af70 100644
--- a/drivers/pwm/pwm-mtk-disp.c
+++ b/drivers/pwm/pwm-mtk-disp.c
@@ -176,19 +176,18 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip,
struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
u64 rate, period, high_width;
u32 clk_div, pwm_en, con0, con1;
- int err;
+ int ret;
- err = clk_prepare_enable(mdp->clk_main);
- if (err < 0) {
- dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_main: %pe\n", ERR_PTR(err));
- return err;
+ ret = clk_prepare_enable(mdp->clk_main);
+ if (ret < 0) {
+ dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_main: %pe\n", ERR_PTR(ret));
+ goto err_handle;
}
- err = clk_prepare_enable(mdp->clk_mm);
- if (err < 0) {
- dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_mm: %pe\n", ERR_PTR(err));
- clk_disable_unprepare(mdp->clk_main);
- return err;
+ ret = clk_prepare_enable(mdp->clk_mm);
+ if (ret < 0) {
+ dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_mm: %pe\n", ERR_PTR(ret));
+ goto err_disable_clk_main;
}
/*
@@ -212,15 +211,23 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip,
* period has 12 bits, clk_div 11 and NSEC_PER_SEC has 30,
* so period * (clk_div + 1) * NSEC_PER_SEC doesn't overflow.
*/
+ if (rate == 0) {
+ dev_err(pwmchip_parent(chip), "rate is zero, cannot calculate period\n");
+ ret = -EINVAL;
+ goto err_disable_clk_mm;
+ }
state->period = DIV64_U64_ROUND_UP(period * (clk_div + 1) * NSEC_PER_SEC, rate);
high_width = FIELD_GET(PWM_HIGH_WIDTH_MASK, con1);
state->duty_cycle = DIV64_U64_ROUND_UP(high_width * (clk_div + 1) * NSEC_PER_SEC,
rate);
state->polarity = PWM_POLARITY_NORMAL;
+
+err_disable_clk_mm:
clk_disable_unprepare(mdp->clk_mm);
+err_disable_clk_main:
clk_disable_unprepare(mdp->clk_main);
-
- return 0;
+err_handle:
+ return ret;
}
static const struct pwm_ops mtk_disp_pwm_ops = {
--
2.45.2
next reply other threads:[~2025-12-15 10:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-15 10:40 20190311120055 created [this message]
2025-12-15 11:29 ` Uwe Kleine-König
-- strict thread matches above, loose matches on Subject: below --
2025-12-15 10:38 20190311120055 created
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=20251215104037.3100485-1-payne.lin@mediatek.com \
--to=payne.lin@mediatek.com \
--cc=Project_Global_Chrome_Upstream_Group@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=jh.hsu@mediatek.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-pwm@vger.kernel.org \
--cc=matthias.bgg@gmail.com \
--cc=sirius.wang@mediatek.com \
--cc=ukleinek@kernel.org \
--cc=vince-wl.liu@mediatek.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
all inboxes | Powered by JetHome®