From: Sam Shih <sam.shih@mediatek.com>
To: "Thierry Reding" <thierry.reding@gmail.com>,
"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
"Matthias Brugger" <matthias.bgg@gmail.com>
Cc: John Crispin <john@phrozen.org>, <linux-pwm@vger.kernel.org>,
<linux-kernel@vger.kernel.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-mediatek@lists.infradead.org>,
Sam Shih <sam.shih@mediatek.com>
Subject: [PATCH v2 1/1] pwm: mediatek: add longer period support
Date: Tue, 3 Mar 2020 18:19:15 +0800 [thread overview]
Message-ID: <1583230755-25986-2-git-send-email-sam.shih@mediatek.com> (raw)
In-Reply-To: <1583230755-25986-1-git-send-email-sam.shih@mediatek.com>
The pwm clock source could be divided by 1625 with PWM_CON
BIT(3) setting in mediatek hardware.
This patch add support for longer pwm period configuration,
which allowing blinking LEDs via pwm interface.
Signed-off-by: Sam Shih <sam.shih@mediatek.com>
---
drivers/pwm/pwm-mediatek.c | 34 ++++++++++++++++++++++++++++++----
1 file changed, 30 insertions(+), 4 deletions(-)
diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
index b94e0d09c300..c64ecff6c550 100644
--- a/drivers/pwm/pwm-mediatek.c
+++ b/drivers/pwm/pwm-mediatek.c
@@ -121,8 +121,11 @@ static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns)
{
struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
- u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH,
- reg_thres = PWMTHRES;
+ /* The source clock is divided by 2^clkdiv or iff the clksel bit
+ * is set by (2^clkdiv*1625)
+ */
+ u32 clkdiv = 0, clksel = 0, cnt_period, cnt_duty,
+ reg_width = PWMDWIDTH, reg_thres = PWMTHRES;
u64 resolution;
int ret;
@@ -133,12 +136,30 @@ static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
/* Using resolution in picosecond gets accuracy higher */
resolution = (u64)NSEC_PER_SEC * 1000;
+ /* Calculate resolution based on current clock frequency */
do_div(resolution, clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
-
+ /* Using resolution to calculate cnt_period which represents
+ * the effective range of the PWM period counter
+ */
cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
while (cnt_period > 8191) {
+ /* Using clkdiv to reduce clock frequency and calculate
+ * new resolution based on new clock speed
+ */
resolution *= 2;
clkdiv++;
+ if (clkdiv > PWM_CLK_DIV_MAX && !clksel) {
+ /* Using clksel to divide the pwm source clock by
+ * an additional 1625, and recalculate new clkdiv
+ * and resolution
+ */
+ clksel = 1;
+ clkdiv = 0;
+ resolution = (u64)NSEC_PER_SEC * 1000 * 1625;
+ do_div(resolution,
+ clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
+ }
+ /* Calculate cnt_period based on resolution */
cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
resolution);
}
@@ -158,8 +179,13 @@ static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
reg_thres = PWM45THRES_FIXUP;
}
+ /* Calculate cnt_duty based on resolution */
cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
- pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
+ if (clksel)
+ pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | BIT(3) |
+ clkdiv);
+ else
+ pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
pwm_mediatek_writel(pc, pwm->hwpwm, reg_width, cnt_period);
pwm_mediatek_writel(pc, pwm->hwpwm, reg_thres, cnt_duty);
--
2.17.1
next prev parent reply other threads:[~2020-03-03 10:19 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-03 10:19 [PATCH v2 0/1] pwm: mediatek: add longer period support Sam Shih
2020-03-03 10:19 ` Sam Shih [this message]
2020-03-06 7:41 ` [PATCH v2 1/1] " Uwe Kleine-König
2020-03-07 21:28 ` Matthias Brugger
2020-03-08 20:18 ` Uwe Kleine-König
2020-03-09 3:35 ` Sam Shih
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=1583230755-25986-2-git-send-email-sam.shih@mediatek.com \
--to=sam.shih@mediatek.com \
--cc=john@phrozen.org \
--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=thierry.reding@gmail.com \
--cc=u.kleine-koenig@pengutronix.de \
/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).