Skip to contentSkip to conversation

fix for pwm-mxs period divider computation

PATCH In review Gaetan Hug 2 participants 4 messages Feb 18, 2015

Subsystems (2)
lore
Share & tools
lore

Gaetan Hug

author
#0 lore raw

cover fix for pwm-mxs period divider computation

Hello Thierry,

I found a bug in the pwm-mxs.c driver for the i.MX28 platform.

The driver computes from the desired period which clock divider it
should be using. This computation assumes that the link between the register value and the actual divider value is raising 2 to the power of the registry value.

div = 1 << regvalue

This is true only for the first 5 values out of 8. Next values are
64, 256 and, 1024.
This affects only the user only if he requests a period > 0.04369s.

Gaetan Hug (1):
fixing pwm-mxs period divider computation

 drivers/pwm/pwm-mxs.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

--
1.7.10.4

author
#1 lore raw

PATCH 1/1 fixing pwm-mxs period divider computation

---
 drivers/pwm/pwm-mxs.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/pwm/pwm-mxs.c +5 -2
index f75ecb0..bbb2f8a 100644
@@ -52,15 +52,18 @@ static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long rate;
 	unsigned long long c;
 
+    unsigned const int cdiv[PERIOD_CDIV_MAX] = {1, 2, 4, 8, 16, 64, 256, 1024};
+
 	rate = clk_get_rate(mxs->clk);
+
 	while (1) {
-		c = rate / (1 << div);
+		c = rate / cdiv[div];
 		c = c * period_ns;
 		do_div(c, 1000000000);
 		if (c < PERIOD_PERIOD_MAX)
 			break;
 		div++;
-		if (div > PERIOD_CDIV_MAX)
+		if (div >= PERIOD_CDIV_MAX)
 			return -EINVAL;
 	}
 
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 1/1] fixing pwm-mxs period divider computation

Quoted 3 lines · Gaetan Hug
---
 drivers/pwm/pwm-mxs.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Adding Shawn and Fabio, hence quoting the full patch.

drivers/pwm/pwm-mxs.c static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 55

Quoted 9 lines
drivers/pwm/pwm-mxs.c +1 -0
index f75ecb0..bbb2f8a 100644
@@ -52,15 +52,18 @@ static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long rate;
 	unsigned long long c;
 
+    unsigned const int cdiv[PERIOD_CDIV_MAX] = {1, 2, 4, 8, 16, 64, 256, 1024};

This could prbably be static const and could be in file scope rather than in this function.

Thierry

Quoted 19 lines
drivers/pwm/pwm-mxs.c +4 -2
+
 	rate = clk_get_rate(mxs->clk);
+
 	while (1) {
-		c = rate / (1 << div);
+		c = rate / cdiv[div];
 		c = c * period_ns;
 		do_div(c, 1000000000);
 		if (c < PERIOD_PERIOD_MAX)
 			break;
 		div++;
-		if (div > PERIOD_CDIV_MAX)
+		if (div >= PERIOD_CDIV_MAX)
 			return -EINVAL;
 	}
 
Signature · 1 line
1.7.10.4
#3 lore raw
In reply to Gaetan Hug
Quoted 9 lines · Gaetan Hug

Hello Thierry,

I found a bug in the pwm-mxs.c driver for the i.MX28 platform.

The driver computes from the desired period which clock divider it
should be using. This computation assumes that the link between the register value and the actual divider value is raising 2 to the power of the registry value.

Quoted 5 lines

div = 1 << regvalue

This is true only for the first 5 values out of 8. Next values are
64, 256 and, 1024.
This affects only the user only if he requests a period > 0.04369s.

Can you put this description into the commit message of the proper patch, please?

Also, the subject should be in the following format:

pwm: mxs: Fix period divider computation

Thanks,
Thierry