Skip to contentSkip to conversation

Change PWM-controlled LED pin active mode and algorithm

PATCH Superseded Nylon Chen 3 participants 7 messages Jan 26, 2024 – Feb 10, 2024

Subsystems (5)

v8 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Nylon Chen

author
#0 lore raw

v8 cover Change PWM-controlled LED pin active mode and algorithm

According to the circuit diagram of User LEDs - RGB described in the manual hifive-unleashed-a00.pdf[0] and hifive-unmatched-schematics-v3.pdf[1].

The behavior of PWM is acitve-high.

According to the descriptionof PWM for pwmcmp in SiFive FU740-C000 Manual[2].

The pwm algorithm is (PW) pulse active time = (D) duty * (T) period. The frac variable is pulse "inactive" time so we need to invert it.

So this patchset removes active-low in DTS and adds reverse logic to the driver.

Updated patches: 3
New patches: 0
Unchanged patches: 0

Links:
- [0]: https://sifive.cdn.prismic.io/sifive/c52a8e32-05ce-4aaf-95c8-7bf8453f8698_hifive-unleashed-a00-schematics-1.pdf
- [1]: https://sifive.cdn.prismic.io/sifive/6a06d6c0-6e66-49b5-8e9e-e68ce76f4192_hifive-unmatched-schematics-v3.pdf
- [2]: https://sifive.cdn.prismic.io/sifive/1a82e600-1f93-4f41-b2d8-86ed8b16acba_fu740-c000-manual-v1p6.pdf

Changed in v8:
- Fix Signed-off-by and Co-developed-by typo.

Changed in v7:
- Remove active-low strings from hifive-unleashed-a00.dts file.

Changed in v6:
- Separate the idempotent test bug fixes into a new patch.
- Move the reversing the duty before the line checking state->enabled.
- Fix the algorithm and change it to take the minimum value first and then reverse it.

Changed in v5:
- Add the updates to the PWM algorithm based on version 2 back in.
- Replace div64_ul with DIV_ROUND_UP_ULL to correct the error in the period value of the idempotent test in pwm_apply_state_debug.

Changed in v4:
- Remove previous updates to the PWM algorithm.

Changed in v3:
- Convert the reference link to standard link.
- Move the inverted function before taking the minimum value.
- Change polarity check condition(high and low).
- Pick the biggest period length possible that is not bigger than the requested period.

Changed in v2:
- Convert the reference link to standard link.
- Fix typo: s/sifive unmatched:/sifive: unmatched:/.
- Remove active-low from hifive-unleashed-a00.dts.
- Include this reference link in the dts and pwm commit messages.

Nylon Chen (3):
riscv: dts: sifive: unleashed/unmatched: Remove PWM controlled LED's
active-low properties
pwm: sifive: change the PWM controlled LED algorithm
pwm: sifive: Fix the error in the idempotent test within the
pwm_apply_state_debug function

 arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts | 12 ++++--------
 arch/riscv/boot/dts/sifive/hifive-unmatched-a00.dts | 12 ++++--------
 drivers/pwm/pwm-sifive.c                            |  9 +++++----
 3 files changed, 13 insertions(+), 20 deletions(-)
Signature · 1 line
2.42.0
author
#1 lore raw

PATCH v8 1/3 riscv: dts: sifive: unleashed/unmatched: Remove PWM controlled LED's active-low properties

This removes the active-low properties of the PWM-controlled LEDs in the HiFive Unmatched device tree.

The reference is hifive-unleashed-a00.pdf[0] and hifive-unmatched-schematics-v3.pdf[1].

---
 arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts | 12 ++++--------
 arch/riscv/boot/dts/sifive/hifive-unmatched-a00.dts | 12 ++++--------
 2 files changed, 8 insertions(+), 16 deletions(-)
arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts +4 -8
index 900a50526d77..06731b8c7bc3 100644
@@ -49,32 +49,28 @@ led-controller {
 		compatible = "pwm-leds";
 
 		led-d1 {
-			pwms = <&pwm0 0 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 0 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d1";
 		};
 
 		led-d2 {
-			pwms = <&pwm0 1 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 1 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d2";
 		};
 
 		led-d3 {
-			pwms = <&pwm0 2 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 2 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d3";
 		};
 
 		led-d4 {
-			pwms = <&pwm0 3 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 3 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d4";
arch/riscv/boot/dts/sifive/hifive-unmatched-a00.dts +4 -8
index 07387f9c135c..b328ee80693f 100644
@@ -51,8 +51,7 @@ led-controller-1 {
 		compatible = "pwm-leds";
 
 		led-d12 {
-			pwms = <&pwm0 0 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 0 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d12";
@@ -68,20 +67,17 @@ multi-led {
 			label = "d2";
 
 			led-red {
-				pwms = <&pwm0 2 7812500 PWM_POLARITY_INVERTED>;
-				active-low;
+				pwms = <&pwm0 2 7812500 0>;
 				color = <LED_COLOR_ID_RED>;
 			};
 
 			led-green {
-				pwms = <&pwm0 1 7812500 PWM_POLARITY_INVERTED>;
-				active-low;
+				pwms = <&pwm0 1 7812500 0>;
 				color = <LED_COLOR_ID_GREEN>;
 			};
 
 			led-blue {
-				pwms = <&pwm0 3 7812500 PWM_POLARITY_INVERTED>;
-				active-low;
+				pwms = <&pwm0 3 7812500 0>;
 				color = <LED_COLOR_ID_BLUE>;
 			};
 		};
Signature · 1 line
2.42.0
maintainer Reviewed-by Acked-by on v8 1/3
#2 lore raw

Re: [PATCH v8 1/3] riscv: dts: sifive: unleashed/unmatched: Remove PWM controlled LED's active-low properties

Quoted 5 lines · Nylon Chen

This removes the active-low properties of the PWM-controlled LEDs in the HiFive Unmatched device tree.

The reference is hifive-unleashed-a00.pdf[0] and hifive-unmatched-schematics-v3.pdf[1].

Quoted 8 lines

Uwe, take this with the driver, if that is ready to be taken.

Thanks,
Conor.

Quoted 87 lines
---
 arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts | 12 ++++--------
 arch/riscv/boot/dts/sifive/hifive-unmatched-a00.dts | 12 ++++--------
 2 files changed, 8 insertions(+), 16 deletions(-)
arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts +4 -8
index 900a50526d77..06731b8c7bc3 100644
@@ -49,32 +49,28 @@ led-controller {
 		compatible = "pwm-leds";
 
 		led-d1 {
-			pwms = <&pwm0 0 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 0 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d1";
 		};
 
 		led-d2 {
-			pwms = <&pwm0 1 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 1 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d2";
 		};
 
 		led-d3 {
-			pwms = <&pwm0 2 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 2 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d3";
 		};
 
 		led-d4 {
-			pwms = <&pwm0 3 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 3 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d4";
arch/riscv/boot/dts/sifive/hifive-unmatched-a00.dts +4 -8
index 07387f9c135c..b328ee80693f 100644
@@ -51,8 +51,7 @@ led-controller-1 {
 		compatible = "pwm-leds";
 
 		led-d12 {
-			pwms = <&pwm0 0 7812500 PWM_POLARITY_INVERTED>;
-			active-low;
+			pwms = <&pwm0 0 7812500 0>;
 			color = <LED_COLOR_ID_GREEN>;
 			max-brightness = <255>;
 			label = "d12";
@@ -68,20 +67,17 @@ multi-led {
 			label = "d2";
 
 			led-red {
-				pwms = <&pwm0 2 7812500 PWM_POLARITY_INVERTED>;
-				active-low;
+				pwms = <&pwm0 2 7812500 0>;
 				color = <LED_COLOR_ID_RED>;
 			};
 
 			led-green {
-				pwms = <&pwm0 1 7812500 PWM_POLARITY_INVERTED>;
-				active-low;
+				pwms = <&pwm0 1 7812500 0>;
 				color = <LED_COLOR_ID_GREEN>;
 			};
 
 			led-blue {
-				pwms = <&pwm0 3 7812500 PWM_POLARITY_INVERTED>;
-				active-low;
+				pwms = <&pwm0 3 7812500 0>;
 				color = <LED_COLOR_ID_BLUE>;
 			};
 		};
Signature · 1 line
2.42.0
author
#3 lore raw

PATCH v8 2/3 pwm: sifive: change the PWM controlled LED algorithm

In reply to Nylon Chen

The frac variable represents the pulse inactive time, and the result of this algorithm is the pulse active time. Therefore, we must reverse the result.

The reference is SiFive FU740-C000 Manual[0]

---
 drivers/pwm/pwm-sifive.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/pwm/pwm-sifive.c +4 -3
index eabddb7c7820..b07c8598bb21 100644
@@ -113,6 +113,7 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	u32 duty, val;
 
 	duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));
+	duty = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - duty;
 
 	state->enabled = duty > 0;
 
@@ -123,11 +124,10 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	state->period = ddata->real_period;
 	state->duty_cycle =
 		(u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH;
-	state->polarity = PWM_POLARITY_INVERSED;
+	state->polarity = PWM_POLARITY_NORMAL;
 
 	return 0;
 }
-
 static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			    const struct pwm_state *state)
 {
@@ -139,7 +139,7 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	int ret = 0;
 	u32 frac;
 
-	if (state->polarity != PWM_POLARITY_INVERSED)
+	if (state->polarity != PWM_POLARITY_NORMAL)
 		return -EINVAL;
 
 	cur_state = pwm->state;
@@ -159,6 +159,7 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	frac = DIV64_U64_ROUND_CLOSEST(num, state->period);
 	/* The hardware cannot generate a 100% duty cycle */
 	frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1);
+	frac = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - frac;
 
 	mutex_lock(&ddata->lock);
 	if (state->period != ddata->approx_period) {
Signature · 1 line
2.42.0
#4 lore raw

Re: [PATCH v8 2/3] pwm: sifive: change the PWM controlled LED algorithm

Hello,

Regarding the Subject: The patch has nothing to do with an LED, has it?

Quoted 2 lines · Nylon Chen

The frac variable represents the pulse inactive time, and the result of this algorithm is the pulse active time. Therefore, we must reverse the result.

Please break lines at 75 columns in the commit log.

Quoted 3 lines

The reference is SiFive FU740-C000 Manual[0]

I looked at Figure 29 in this document (version v1p6, pdf page 148). Not sure I understand that correctly, but I expect that the output of the ">=?" node below pwmcmp0 to become 1 if pwms has reached pwmcmp0, is that right? In that case this output is zero when pwmcount is zero and then pwmcmp0ip is zero, too. So a period starts with the inactive part and so it's inversed polarity.

What made you think that the current driver implementation is wrong?

Quoted 14 lines
---
 drivers/pwm/pwm-sifive.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/pwm/pwm-sifive.c +0 -0
index eabddb7c7820..b07c8598bb21 100644

drivers/pwm/pwm-sifive.c static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 116

Quoted 4 lines
drivers/pwm/pwm-sifive.c +1 -0
@@ -113,6 +113,7 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	u32 duty, val;
 
 	duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));
+	duty = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - duty;

I find it irritating that both values are assigned to duty. I'd spend another variable and make this:

	inactive = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));
	duty = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - inactive;

drivers/pwm/pwm-sifive.c static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 130

Quoted 13 lines
drivers/pwm/pwm-sifive.c +1 -2
 	state->enabled = duty > 0;
 
@@ -123,11 +124,10 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	state->period = ddata->real_period;
 	state->duty_cycle =
 		(u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH;
-	state->polarity = PWM_POLARITY_INVERSED;
+	state->polarity = PWM_POLARITY_NORMAL;
 
 	return 0;
 }
-

Please keep this empty line between functions.

drivers/pwm/pwm-sifive.c static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 133

Quoted 3 lines
drivers/pwm/pwm-sifive.c +0 -0
 static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			    const struct pwm_state *state)
 {

Best regards
Uwe

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

Re: [PATCH v8 2/3] pwm: sifive: change the PWM controlled LED algorithm

Uwe Kleine-König <[email protected]> 於 2024年2月6日 週二 上午2:07寫道:

Quoted 2 lines

Hello,

Hi Uwe, thanks for your feedback.

Quoted 2 lines

Regarding the Subject: The patch has nothing to do with an LED, has it?

I will correct this.

Quoted 6 lines · Nylon Chen
Quoted 2 lines

The frac variable represents the pulse inactive time, and the result of this algorithm is the pulse active time. Therefore, we must reverse the result.

Please break lines at 75 columns in the commit log.

got it.

Quoted 5 lines
Quoted 4 lines

The reference is SiFive FU740-C000 Manual[0]

Quoted 8 lines

I looked at Figure 29 in this document (version v1p6, pdf page 148). Not sure I understand that correctly, but I expect that the output of the ">=?" node below pwmcmp0 to become 1 if pwms has reached pwmcmp0, is that right? In that case this output is zero when pwmcount is zero and then pwmcmp0ip is zero, too. So a period starts with the inactive part and so it's inversed polarity.

What made you think that the current driver implementation is wrong?

This is the process of my speculation.

This is a HiFive Unmatched/Unleashed LED-PWM layout

            VDD
               |
               |
           _____
           \        /   LED
            \     /
              ---
               |
               |
               |
         ______
        |              |
        -             |
        ^    -->    |------ PWM
        |___|___|
               |
               |
              __
               -
            GND

- the waveform
e.g. duty=30s, period=100s, actvie-high = 30%, active-low = 70%

V
^

|
| ----------|
|             |
|             |
|______ |__________ > t

When VCC is high, the LED will be illuminated, which is an active-high logic. This is why I want to remove "active-low".

For HW, we just focus on pwmcount/pwmcmp[0-3]
- pwmcount default is zero, that counter 0->1->0xffff
- Follow the origin algorithm the frac=0x0(on) / 0xffff(off) and when the smaller the value of frac, the brighter the light.
-- E.g. pwmcmp = 0x2, pwmcount 0x0->0x1->...->0xffff

       --- 0->0x2=low & 0x3->0xffff=high => 98%
   -- E.g. pwmcmp = 0xffff, pwmcount 0x0->0x1->...->0xffff
       --- 0->0xffff=low => 0%

- For SW, we reference the algorithm. (D=PW/T*100% D=duty_cycle, T=period, PW=pulse width (pulse active time))
-- when we consider HW behavior
--- Direct writing SW frac into HW's pwmcmp is active low, so when
we want to get an active-high behavior that use a invert function.

If my understanding or deduction process is incorrect, please let me know. Thank you.

Quoted 17 lines
Quoted 17 lines
---
 drivers/pwm/pwm-sifive.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/pwm/pwm-sifive.c +0 -0
index eabddb7c7820..b07c8598bb21 100644
@@ -113,6 +113,7 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
      u32 duty, val;
 
Quoted 8 lines

drivers/pwm/pwm-sifive.c static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 116

Quoted 2 lines
drivers/pwm/pwm-sifive.c +1 -0
      duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));
+     duty = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - duty;

I find it irritating that both values are assigned to duty. I'd spend another variable and make this:

        inactive = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));
        duty = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - inactive;

got it.

Quoted 12 lines
Quoted 12 lines
drivers/pwm/pwm-sifive.c +1 -1
      state->enabled = duty > 0;
 
@@ -123,11 +124,10 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
      state->period = ddata->real_period;
      state->duty_cycle =
              (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH;
-     state->polarity = PWM_POLARITY_INVERSED;
+     state->polarity = PWM_POLARITY_NORMAL;
 
Quoted 5 lines

drivers/pwm/pwm-sifive.c static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 130

Quoted 3 lines
drivers/pwm/pwm-sifive.c +0 -1
      return 0;
 }
-

Please keep this empty line between functions.

got it.

Quoted 11 lines

drivers/pwm/pwm-sifive.c static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 133

Quoted 4 lines
drivers/pwm/pwm-sifive.c +0 -0
 static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
                          const struct pwm_state *state)
 {

Best regards
Uwe

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

author
#6 lore raw

PATCH v8 3/3 pwm: sifive: Fix the error in the idempotent test within the pwm_apply_state_debug function

In reply to Nylon Chen

Round the result to the nearest whole number. This ensures that real_period is always a reasonable integer that is not lower than the actual value.

e.g.

$ echo 110 > /sys/devices/platform/led-controller-1/leds/d12/brightness
$ .apply is not idempotent (ena=1 pol=0 1739692/4032985) -> (ena=1 pol=0 1739630/4032985)
---
 drivers/pwm/pwm-sifive.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-sifive.c +1 -1
index b07c8598bb21..7cf7a76cdb44 100644
@@ -101,7 +101,7 @@ static void pwm_sifive_update_clock(struct pwm_sifive_ddata *ddata,
 
 	/* As scale <= 15 the shift operation cannot overflow. */
 	num = (unsigned long long)NSEC_PER_SEC << (PWM_SIFIVE_CMPWIDTH + scale);
-	ddata->real_period = div64_ul(num, rate);
+	ddata->real_period = DIV_ROUND_UP_ULL(num, rate);
 	dev_dbg(ddata->chip.dev,
 		"New real_period = %u ns\n", ddata->real_period);
 }
Signature · 1 line
2.42.0