Skip to contentSkip to conversation

pwm: mediatek: add longer period support

PATCH Superseded Sam Shih 2 participants 7 messages Feb 27, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sam Shih

author
#0 lore raw

cover pwm: mediatek: add longer period support

This patch add support for longer pwm period configuration, which allowing blinking LEDs via pwm interface.

Sam Shih (1):
pwm: mediatek: add longer period support

 drivers/pwm/pwm-mediatek.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 1/1 pwm: mediatek: add longer period support

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.

---
 drivers/pwm/pwm-mediatek.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/pwm/pwm-mediatek.c +17 -4
index b94e0d09c300..9af309bea01a 100644
@@ -121,8 +121,8 @@ 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;
+	u32 clkdiv = 0, clksel = 0, cnt_period, cnt_duty,
+	    reg_width = PWMDWIDTH, reg_thres = PWMTHRES;
 	u64 resolution;
 	int ret;
 
@@ -141,9 +141,18 @@ static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
 		clkdiv++;
 		cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
 						   resolution);
+		if (clkdiv > PWM_CLK_DIV_MAX && !clksel) {
+			clksel = 1;
+			clkdiv = 0;
+			resolution = (u64)NSEC_PER_SEC * 1000 * 1625;
+			do_div(resolution,
+				clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
+			cnt_period = DIV_ROUND_CLOSEST_ULL(
+					(u64)period_ns * 1000, resolution);
+		}
 	}
 
-	if (clkdiv > PWM_CLK_DIV_MAX) {
+	if (clkdiv > PWM_CLK_DIV_MAX && clksel) {
 		pwm_mediatek_clk_disable(chip, pwm);
 		dev_err(chip->dev, "period %d not supported\n", period_ns);
 		return -EINVAL;
@@ -159,7 +168,11 @@ static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	}
 
 	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);
 
Signature · 1 line
2.17.1
#2 lore raw
Quoted 16 lines · Sam Shih

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.

---
 drivers/pwm/pwm-mediatek.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/pwm/pwm-mediatek.c +0 -0
index b94e0d09c300..9af309bea01a 100644

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 128

Quoted 10 lines
drivers/pwm/pwm-mediatek.c +2 -2
@@ -121,8 +121,8 @@ 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;
+	u32 clkdiv = 0, clksel = 0, cnt_period, cnt_duty,
+	    reg_width = PWMDWIDTH, reg_thres = PWMTHRES;
 	u64 resolution;
 	int ret;
 

Adding some more context:

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +0 -0
@@ -139,11 +139,20 @@ static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	while (cnt_period > 8191) {
 		resolution *= 2;
 		clkdiv++;

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 151

Quoted 10 lines
drivers/pwm/pwm-mediatek.c +8 -0
 		cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
 						   resolution);
+		if (clkdiv > PWM_CLK_DIV_MAX && !clksel) {
+			clksel = 1;
+			clkdiv = 0;
+			resolution = (u64)NSEC_PER_SEC * 1000 * 1625;
+			do_div(resolution,
+				clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
+			cnt_period = DIV_ROUND_CLOSEST_ULL(
+					(u64)period_ns * 1000, resolution);

The assignment is a repetition from just above the if. Maybe just put it once after this if block?

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 155

Quoted 5 lines
drivers/pwm/pwm-mediatek.c +2 -1
+		}
 	}
 
-	if (clkdiv > PWM_CLK_DIV_MAX) {
+	if (clkdiv > PWM_CLK_DIV_MAX && clksel) {

Is this change actually relevant? If the while loop that starts at line 139 is never run (because cnt_period is <= 8191) clkdiv is zero and so the condition is false with and without "&& clksel". If however the while loop is entered and clkdiv becomes bigger than PWM_CLK_DIV_MAX clksel is 1 and the "&& clksel" doesn't make a difference, too.

The code is hard to follow, I wonder if this could be cleaned up with some comments added that explain the hardware details enough to be able to actually understand the code without having the hardware reference manual handy.

Quoted 18 lines
drivers/pwm/pwm-mediatek.c +5 -1
 		pwm_mediatek_clk_disable(chip, pwm);
 		dev_err(chip->dev, "period %d not supported\n", period_ns);
 		return -EINVAL;
@@ -159,7 +168,11 @@ static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	}
 
 	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);
 
Signature · 1 line
2.17.1

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#3 lore raw

On Thu, 2020-02-27 at 09:04 +0100, Uwe Kleine-König wrote:

Quoted 11 lines · Sam Shih
Quoted 11 lines

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.

---
 drivers/pwm/pwm-mediatek.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
Quoted 5 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip

Quoted 5 lines
drivers/pwm/pwm-mediatek.c +0 -0
index b94e0d09c300..9af309bea01a 100644
@@ -121,8 +121,8 @@ static int pwm_mediatek_config(struct pwm_chip

*chip, struct pwm_device *pwm,

Quoted 12 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 128

Quoted 10 lines
drivers/pwm/pwm-mediatek.c +2 -2
 			       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;
+	u32 clkdiv = 0, clksel = 0, cnt_period, cnt_duty,
+	    reg_width = PWMDWIDTH, reg_thres = PWMTHRES;
 	u64 resolution;
 	int ret;
 

Adding some more context:

+ /* The pwm source clock can be divided by 2^clkdiv. When the clksel +
* bit is set to 1, The final clock output needs to be divided by an + * extra 1625.
+ */

Is this ok ?

Quoted 1 line

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip

Quoted 1 line
drivers/pwm/pwm-mediatek.c +0 -0
@@ -139,11 +139,20 @@ static int pwm_mediatek_config(struct pwm_chip

*chip, struct pwm_device *pwm,

Quoted 7 lines
Quoted 7 lines
drivers/pwm/pwm-mediatek.c +2 -0
 	while (cnt_period > 8191) {
 		resolution *= 2;
 		clkdiv++;
 		cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
 						   resolution);
+		if (clkdiv > PWM_CLK_DIV_MAX && !clksel) {
+			clksel = 1;
Quoted 8 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 151

Quoted 6 lines
drivers/pwm/pwm-mediatek.c +6 -0
+			clkdiv = 0;
+			resolution = (u64)NSEC_PER_SEC * 1000 * 1625;
+			do_div(resolution,
+				clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
+			cnt_period = DIV_ROUND_CLOSEST_ULL(
+					(u64)period_ns * 1000, resolution);

The assignment is a repetition from just above the if. Maybe just put

it

Quoted 1 line

once after this if block?

The cnt_period represents the effective range of the PWM period counter, when we need changing the pwm output period to a longer value at the same clock frequency, we can setting a larger cnt_period, but the width of the cnt_peroid register is 12 bits,
When the request period is too long, we need to divide the clock source and then recalculate cnt_period outputs the correct waveform. As mentioned above, when changing clkdiv, we need to recalculate cnt_period immediately.

If the request period is very long (for example, LED blinking), clkdiv may be insufficient.
In this case, we will use clksel to divide the pwm source clock by an additional 1625, and recalculate clkdiv and cnt_period.

I don't think we can just place assignments after the if block.

Quoted 8 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 155

Quoted 6 lines
drivers/pwm/pwm-mediatek.c +2 -1
+		}
 	}
 
-	if (clkdiv > PWM_CLK_DIV_MAX) {
+	if (clkdiv > PWM_CLK_DIV_MAX && clksel) {

Is this change actually relevant? If the while loop that starts at

line

Quoted 5 lines

139 is never run (because cnt_period is <= 8191) clkdiv is zero and so the condition is false with and without "&& clksel". If however the while loop is entered and clkdiv becomes bigger than PWM_CLK_DIV_MAX clksel is 1 and the "&& clksel" doesn't make a difference, too.

You are right, I will remove this.

Quoted 2 lines

The code is hard to follow, I wonder if this could be cleaned up with some comments added that explain the hardware details enough to be

able

Quoted 3 lines

to actually understand the code without having the hardware reference manual handy.

Is it sufficient to add some context into comment like the response of the second question?

Quoted 4 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +0 -0
 		pwm_mediatek_clk_disable(chip, pwm);
 		dev_err(chip->dev, "period %d not supported\n", period_ns);
 		return -EINVAL;
@@ -159,7 +168,11 @@ static int pwm_mediatek_config(struct pwm_chip

*chip, struct pwm_device *pwm,

Quoted 12 lines
Quoted 12 lines
drivers/pwm/pwm-mediatek.c +5 -1
 	}
 
 	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);
 
Quoted 7 lines
Quoted 2 lines
Signature · 1 line
2.17.1

Best regards
Uwe

Best Regards,
Sam

#4 lore raw

Hello,

Quoted 32 lines · Sam Shih

On Thu, 2020-02-27 at 09:04 +0100, Uwe Kleine-König wrote:

Quoted 11 lines · Sam Shih
Quoted 11 lines

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.

---
 drivers/pwm/pwm-mediatek.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
Quoted 5 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip

Quoted 5 lines
drivers/pwm/pwm-mediatek.c +0 -0
index b94e0d09c300..9af309bea01a 100644
@@ -121,8 +121,8 @@ static int pwm_mediatek_config(struct pwm_chip

*chip, struct pwm_device *pwm,

Quoted 12 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 128

Quoted 10 lines
drivers/pwm/pwm-mediatek.c +2 -2
 			       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;
+	u32 clkdiv = 0, clksel = 0, cnt_period, cnt_duty,
+	    reg_width = PWMDWIDTH, reg_thres = PWMTHRES;
 	u64 resolution;
 	int ret;
 

Adding some more context:

Quoted 4 lines

+ /* The pwm source clock can be divided by 2^clkdiv. When the clksel +
* bit is set to 1, The final clock output needs to be divided by an + * extra 1625.
+ */

I'd write:

The source clock is divided by 2^clkdiv or iff the clksel bit is set by 2^clkdiv + 1625.

Quoted 33 lines

Is this ok ?

Quoted 1 line

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip

Quoted 1 line
drivers/pwm/pwm-mediatek.c +0 -0
@@ -139,11 +139,20 @@ static int pwm_mediatek_config(struct pwm_chip

*chip, struct pwm_device *pwm,

Quoted 7 lines
Quoted 7 lines
drivers/pwm/pwm-mediatek.c +2 -0
 	while (cnt_period > 8191) {
 		resolution *= 2;
 		clkdiv++;
 		cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
 						   resolution);
+		if (clkdiv > PWM_CLK_DIV_MAX && !clksel) {
+			clksel = 1;
Quoted 8 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 151

Quoted 6 lines
drivers/pwm/pwm-mediatek.c +6 -0
+			clkdiv = 0;
+			resolution = (u64)NSEC_PER_SEC * 1000 * 1625;
+			do_div(resolution,
+				clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
+			cnt_period = DIV_ROUND_CLOSEST_ULL(
+					(u64)period_ns * 1000, resolution);

The assignment is a repetition from just above the if. Maybe just put

it

Quoted 1 line

once after this if block?

The cnt_period represents the effective range of the PWM period counter, when we need changing the pwm output period to a longer value at the same clock frequency, we can setting a larger cnt_period, but the width of the cnt_peroid register is 12 bits,
When the request period is too long, we need to divide the clock source and then recalculate cnt_period outputs the correct waveform. As mentioned above, when changing clkdiv, we need to recalculate cnt_period immediately.

Quoted 6 lines

If the request period is very long (for example, LED blinking), clkdiv may be insufficient.
In this case, we will use clksel to divide the pwm source clock by an additional 1625, and recalculate clkdiv and cnt_period.

I don't think we can just place assignments after the if block.

I didn't care enough to read your reasoning and retry to convince you with mine:

With your patch you have:

	cnt_period = someexpression;

	if (somecondition) {
		...
		cnt_period = someexpression;
	}

As somecondition doesn't make use of cnt_period this is equivalent to:

	if (somecondition) {
		...
	}
	cnt_period = someexpression;

isn't it?

Quoted 7 lines
Quoted 4 lines

The code is hard to follow, I wonder if this could be cleaned up with some comments added that explain the hardware details enough to be able to actually understand the code without having the hardware reference manual handy.

Is it sufficient to add some context into comment like the response of the second question?

I didn't check but I wouldn't be surprised if the code is more complicated than necessary. If you don't see something to simplify, go for adding an explanation as suggested and I will take a look in a quiet moment.

Not sure I already pointed out that having a link to a publicly available reference manual in the driver's header is useful. If there is such a manual, please add a link there. Your benefit is that you simplify others to improve your driver.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#5 lore raw

Hello,

Quoted 10 lines · Sam Shih
Quoted 8 lines · Uwe Kleine-König
Quoted 6 lines · Sam Shih
Quoted 6 lines

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.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Review

Quoted 6 lines
---
 drivers/pwm/pwm-mediatek.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/pwm/pwm-mediatek.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct

Quoted 4 lines

index b94e0d09c300..9af309bea01a 100644

drivers/pwm/pwm-mediatek.c +0 -0
@@ -121,8 +121,8 @@ static int pwm_mediatek_config(struct

pwm_chip

Quoted 14 lines
Quoted 14 lines

*chip, struct pwm_device *pwm,

Quoted 12 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 128

Quoted 10 lines
drivers/pwm/pwm-mediatek.c +2 -2
 			       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;
+	u32 clkdiv = 0, clksel = 0, cnt_period, cnt_duty,
+	    reg_width = PWMDWIDTH, reg_thres = PWMTHRES;
 	u64 resolution;
 	int ret;
 

Adding some more context:

Quoted 1 line
Quoted 1 line

+ /* The pwm source clock can be divided by 2^clkdiv. When the

clksel +

Quoted 1 line
Quoted 1 line

* bit is set to 1, The final clock output needs to be divided by an

+ *

Quoted 6 lines
Quoted 2 lines

extra 1625.
+ */

I'd write:

The source clock is divided by 2^clkdiv or iff the clksel bit is set

by

Quoted 2 lines

2^clkdiv + 1625.

Great, the comment is short and clear.
But maybe change “2^clkdiv + 1625” to “the product of 2^clkdiv and 1625” is clearer ?

Quoted 5 lines
Quoted 5 lines

Is this ok ?

Quoted 1 line

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct

Quoted 1 line
drivers/pwm/pwm-mediatek.c +0 -0
@@ -139,11 +139,20 @@ static int pwm_mediatek_config(struct

pwm_chip

Quoted 16 lines
Quoted 16 lines

*chip, struct pwm_device *pwm,

Quoted 15 lines

drivers/pwm/pwm-mediatek.c static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 151

Quoted 13 lines
drivers/pwm/pwm-mediatek.c +8 -0
 	while (cnt_period > 8191) {
 		resolution *= 2;
 		clkdiv++;
 		cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
 						   resolution);
+		if (clkdiv > PWM_CLK_DIV_MAX && !clksel) {
+			clksel = 1;
+			clkdiv = 0;
+			resolution = (u64)NSEC_PER_SEC * 1000 * 1625;
+			do_div(resolution,
+				clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
+			cnt_period = DIV_ROUND_CLOSEST_ULL(
+					(u64)period_ns * 1000, resolution);

The assignment is a repetition from just above the if. Maybe just

put

Quoted 4 lines
Quoted 4 lines

it

Quoted 1 line

once after this if block?

The cnt_period represents the effective range of the PWM period

counter,

Quoted 2 lines
Quoted 2 lines

when we need changing the pwm output period to a longer value at the same clock frequency, we can setting a larger cnt_period, but the

width

Quoted 2 lines
Quoted 2 lines

of the cnt_peroid register is 12 bits,
When the request period is too long, we need to divide the clock

source

Quoted 5 lines
Quoted 5 lines

and then recalculate cnt_period outputs the correct waveform. As mentioned above, when changing clkdiv, we need to recalculate cnt_period immediately.

If the request period is very long (for example, LED blinking),

clkdiv

Quoted 2 lines
Quoted 2 lines

may be insufficient.
In this case, we will use clksel to divide the pwm source clock by

an

Quoted 16 lines
Quoted 3 lines

additional 1625, and recalculate clkdiv and cnt_period.

I don't think we can just place assignments after the if block.

I didn't care enough to read your reasoning and retry to convince you with mine:

With your patch you have:

	cnt_period = someexpression;

	if (somecondition) {
		...
		cnt_period = someexpression;
	}
Quoted 9 lines

As somecondition doesn't make use of cnt_period this is equivalent to:

	if (somecondition) {
		...
	}
	cnt_period = someexpression;

isn't it?

Yes, you're right, I misunderstood.
Your code clearly reminded me.

I just want to explain that the re-calculation of the cnt_period is important.
However, after reading your code, I think the program logic will not be break and the duplicates can be removed.

Quoted 1 line
Quoted 1 line
Quoted 1 line

The code is hard to follow, I wonder if this could be cleaned up

with

Quoted 1 line
Quoted 1 line
Quoted 1 line

some comments added that explain the hardware details enough to be

able

Quoted 1 line
Quoted 1 line
Quoted 1 line

to actually understand the code without having the hardware

reference

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

manual handy.

Is it sufficient to add some context into comment like the response

of

Quoted 5 lines
Quoted 1 line

the second question?

I didn't check but I wouldn't be surprised if the code is more complicated than necessary. If you don't see something to simplify, go for adding an explanation as suggested and I will take a look in a

quiet

Quoted 2 lines

moment.

I will send v2 patch to remove the repetition of “cnt_period = someexpression”, and add some comment to clksel and cnt_period.

Quoted 2 lines

Not sure I already pointed out that having a link to a publicly available reference manual in the driver's header is useful. If there

is

Quoted 8 lines

such a manual, please add a link there. Your benefit is that you simplify others to improve your driver.

Best regards
Uwe

Best Regards.
Sam

#6 lore raw

Hello Sam,

Quoted 8 lines · Sam Shih
Quoted 8 lines
Quoted 5 lines

+ /* The pwm source clock can be divided by 2^clkdiv. When the clksel +
* bit is set to 1, The final clock output needs to be divided by an + * extra 1625.
+ */

I'd write:

Quoted 7 lines
Quoted 3 lines

The source clock is divided by 2^clkdiv or iff the clksel bit is set by 2^clkdiv + 1625.

Great, the comment is short and clear.
But maybe change “2^clkdiv + 1625” to “the product of 2^clkdiv and 1625” is clearer ?

Writing a formula in words isn't helpful. If my formula was wrong use the right one. I wrote

	2^clkdiv + 1625

(which implicitly means (2^clkdiv) + 1625), if this is wrong write

	2^clkdiv * 1625

or whatever is the right one then. And use parenthesis if you doubt clearness.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |