Skip to contentSkip to conversation

pwm: Introduce pwm driver for the Loongson family chips

PATCH Superseded Binbin Zhou 3 participants 6 messages Jul 10, 2024 – Sep 13, 2024

Subsystems (4)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Binbin Zhou

author maintainer
#0 lore raw

v5 cover pwm: Introduce pwm driver for the Loongson family chips

Hi all:

This patchset introduce a generic PWM framework driver for Loongson family. Each PWM has one pulse width output signal and one pulse input signal to be measured.

It can be found on Loongson-2K series cpus and Loongson LS7A bridge chips.

Thanks.

-------
V5:
patch (2/2):
- Rebase on pwm/for-next;
- Test with PWM_DEBUG enabled;
- Rewrote pwm_loongson_apply() and adjusted the order of pwm status and polarity to avoid test failure;
- Added DIV64_U64_ROUND_UP in pwm_loongson_get_state() to avoid precision loss and to avoid test failures.

Link to V4:
https://lore.kernel.org/all/[email protected]/

V4:
patch (2/2):
- Rebase on pwm/for-next;
- Addressed Uwe's review comments:
- Make use of devm_pwmchip_alloc() function;
- Add Limitations description;
- Add LOONGSON_ prefix for Loongson pwm register defines;
- Keep regs written only once;
- Rewrite duty/period calculation;
- Add dev_err_probe() in .probe();
- Fix some code style.

Link to V3:
https://lore.kernel.org/linux-pwm/[email protected]/

V3:
patch (1/2):
- Add Reviewed-by tag from Krzysztof, thanks.
patch (2/2):
- Several code stlye adjustments, such as line breaks.

Link to V2:
https://lore.kernel.org/all/[email protected]/

v2:
- Remove the dts-related patches and update dts at once after all relevant drivers are complete.
patch (1/2):
- The dt-binding filename should match compatible, rename it as loongson,ls7a-pwm.yaml;
- Update binding description;
- Add description for each pwm cell;
- Drop '#pwm-cells' from required, for pwm.yaml makes it required already.

Link to v1:
https://lore.kernel.org/linux-pwm/[email protected]/

Binbin Zhou (2):
dt-bindings: pwm: Add Loongson PWM controller
pwm: Add Loongson PWM controller support

 .../bindings/pwm/loongson,ls7a-pwm.yaml       |  66 ++++
 MAINTAINERS                                   |   7 +
 drivers/pwm/Kconfig                           |  12 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-loongson.c                    | 285 ++++++++++++++++++
 5 files changed, 371 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml
 create mode 100644 drivers/pwm/pwm-loongson.c
Signature · 1 line
2.43.5
author maintainer
#1 lore raw

PATCH v5 1/2 dt-bindings: pwm: Add Loongson PWM controller

Add Loongson PWM controller binding with DT schema format using json-schema.

---
 .../bindings/pwm/loongson,ls7a-pwm.yaml       | 66 +++++++++++++++++++
 MAINTAINERS                                   |  6 ++
 2 files changed, 72 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml
Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml new file +66 -0
new file mode 100644
index 000000000000..46814773e0cc
@@ -0,0 +1,66 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/loongson,ls7a-pwm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Loongson PWM Controller
+
+maintainers:
+  - Binbin Zhou <[email protected]>
+
+description:
+  The Loongson PWM has one pulse width output signal and one pulse input
+  signal to be measured.
+  It can be found on Loongson-2K series cpus and Loongson LS7A bridge chips.
+
+allOf:
+  - $ref: pwm.yaml#
+
+properties:
+  compatible:
+    oneOf:
+      - const: loongson,ls7a-pwm
+      - items:
+          - enum:
+              - loongson,ls2k0500-pwm
+              - loongson,ls2k1000-pwm
+              - loongson,ls2k2000-pwm
+          - const: loongson,ls7a-pwm
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  '#pwm-cells':
+    description:
+      The first cell must have a value of 0, which specifies the PWM output signal;
+      The second cell is the period in nanoseconds;
+      The third cell flag supported by this binding is PWM_POLARITY_INVERTED.
+    const: 3
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/clock/loongson,ls2k-clk.h>
+    pwm@1fe22000 {
+        compatible = "loongson,ls2k1000-pwm", "loongson,ls7a-pwm";
+        reg = <0x1fe22000 0x10>;
+        interrupt-parent = <&liointc0>;
+        interrupts = <24 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clk LOONGSON2_APB_CLK>;
+        #pwm-cells = <3>;
+    };
MAINTAINERS +6 -0
index efa9e7b653a6..973d60113105 100644
@@ -12989,6 +12989,12 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml
 F:	drivers/i2c/busses/i2c-ls2x.c
 
+LOONGSON PWM DRIVER
+M:	Binbin Zhou <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml
+
 LOONGSON-2 SOC SERIES CLOCK DRIVER
 M:	Yinbo Zhu <[email protected]>
 L:	[email protected]
Signature · 1 line
2.43.5
author maintainer
#2 lore raw

PATCH v5 2/2 pwm: Add Loongson PWM controller support

In reply to Binbin Zhou

This commit adds a generic PWM framework driver for the PWM controller found on Loongson family chips.

---
 MAINTAINERS                |   1 +
 drivers/pwm/Kconfig        |  12 ++
 drivers/pwm/Makefile       |   1 +
 drivers/pwm/pwm-loongson.c | 285 +++++++++++++++++++++++++++++++++++++
 4 files changed, 299 insertions(+)
 create mode 100644 drivers/pwm/pwm-loongson.c
MAINTAINERS +1 -0
index 973d60113105..2222d036057a 100644
@@ -12994,6 +12994,7 @@ M:	Binbin Zhou <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml
+F:	drivers/pwm/pwm-loongson.c
 
 LOONGSON-2 SOC SERIES CLOCK DRIVER
 M:	Yinbo Zhu <[email protected]>
drivers/pwm/Kconfig +12 -0
index 1dd7921194f5..839a63ccda0e 100644
@@ -320,6 +320,18 @@ config PWM_KEEMBAY
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-keembay.
 
+config PWM_LOONGSON
+	tristate "Loongson PWM support"
+	depends on MACH_LOONGSON64 || COMPILE_TEST
+	depends on COMMON_CLK
+	help
+	  Generic PWM framework driver for Loongson family.
+	  It can be found on Loongson-2K series cpus and Loongson LS7A
+	  bridge chips.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-loongson.
+
 config PWM_LP3943
 	tristate "TI/National Semiconductor LP3943 PWM support"
 	depends on MFD_LP3943
drivers/pwm/Makefile +1 -0
index 90913519f11a..032d73327509 100644
@@ -27,6 +27,7 @@ obj-$(CONFIG_PWM_INTEL_LGM)	+= pwm-intel-lgm.o
 obj-$(CONFIG_PWM_IQS620A)	+= pwm-iqs620a.o
 obj-$(CONFIG_PWM_JZ4740)	+= pwm-jz4740.o
 obj-$(CONFIG_PWM_KEEMBAY)	+= pwm-keembay.o
+obj-$(CONFIG_PWM_LOONGSON)	+= pwm-loongson.o
 obj-$(CONFIG_PWM_LP3943)	+= pwm-lp3943.o
 obj-$(CONFIG_PWM_LPC18XX_SCT)	+= pwm-lpc18xx-sct.o
 obj-$(CONFIG_PWM_LPC32XX)	+= pwm-lpc32xx.o
drivers/pwm/pwm-loongson.c new file +285 -0

285 lines not on this page · raw message

Signature · 1 line
2.43.5
#5 lore raw
In reply to Binbin Zhou
Quoted 20 lines · Binbin Zhou

Hi all:

This patchset introduce a generic PWM framework driver for Loongson family. Each PWM has one pulse width output signal and one pulse input signal to be measured.

It can be found on Loongson-2K series cpus and Loongson LS7A bridge chips.

Thanks.

-------
V5:
patch (2/2):
- Rebase on pwm/for-next;
- Test with PWM_DEBUG enabled;
- Rewrote pwm_loongson_apply() and adjusted the order of pwm status and polarity to avoid test failure;
- Added DIV64_U64_ROUND_UP in pwm_loongson_get_state() to avoid precision loss and to avoid test failures.

Quoted 2 lines

Link to V4:
https://lore.kernel.org/all/[email protected]/

Hi Uwe:

Sorry to interrupt.
I have tested the patchset on the pwm-next branch(861a4272660ac0ff51aa4e2dbfbc3276c06b35eb) with PWM_DEBUG enabled.
Please review it for me at your leisure.

Thanks.
Binbin

Quoted 54 lines

V4:
patch (2/2):
- Rebase on pwm/for-next;
- Addressed Uwe's review comments:
- Make use of devm_pwmchip_alloc() function;
- Add Limitations description;
- Add LOONGSON_ prefix for Loongson pwm register defines;
- Keep regs written only once;
- Rewrite duty/period calculation;
- Add dev_err_probe() in .probe();
- Fix some code style.

Link to V3:
https://lore.kernel.org/linux-pwm/[email protected]/

V3:
patch (1/2):
- Add Reviewed-by tag from Krzysztof, thanks.
patch (2/2):
- Several code stlye adjustments, such as line breaks.

Link to V2:
https://lore.kernel.org/all/[email protected]/

v2:
- Remove the dts-related patches and update dts at once after all relevant drivers are complete.
patch (1/2):
- The dt-binding filename should match compatible, rename it as loongson,ls7a-pwm.yaml;
- Update binding description;
- Add description for each pwm cell;
- Drop '#pwm-cells' from required, for pwm.yaml makes it required already.

Link to v1:
https://lore.kernel.org/linux-pwm/[email protected]/

Binbin Zhou (2):
dt-bindings: pwm: Add Loongson PWM controller
pwm: Add Loongson PWM controller support

 .../bindings/pwm/loongson,ls7a-pwm.yaml       |  66 ++++
 MAINTAINERS                                   |   7 +
 drivers/pwm/Kconfig                           |  12 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-loongson.c                    | 285 ++++++++++++++++++
 5 files changed, 371 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml
 create mode 100644 drivers/pwm/pwm-loongson.c

--
2.43.5

#3 lore raw

Re: [PATCH v5 2/2] pwm: Add Loongson PWM controller support

In reply to Binbin Zhou

Hello,

Quoted 10 lines · Binbin Zhou
drivers/pwm/pwm-loongson.c new file +4 -0
new file mode 100644
index 000000000000..17ab2a2f48ad
@@ -0,0 +1,285 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Loongson PWM driver
+ *

drivers/pwm/pwm-loongson.c line 14

Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10 -0
+ * Author: Juxin Gao <[email protected]>
+ * Further cleanup and restructuring by:
+ *         Binbin Zhou <[email protected]>
+ *
+ * Copyright (C) 2017-2024 Loongson Technology Corporation Limited.
+ *
+ * Limitations:
+ * - The buffer register value should be written before the CTRL register.
+ * - When disabled the output is driven to 0 independent of the configured
+ *   polarity.

An info about possible glitches and if a period is completed on reconfiguration or when the PWM is disabled would be great.

Also if there is a publically available manual, please add a link here.

Quoted 4 lines
drivers/pwm/pwm-loongson.c new file +3 -0
+ */
+
[...]
+static int pwm_loongson_apply(struct pwm_chip *chip, struct pwm_device *pwm,

drivers/pwm/pwm-loongson.c line 144

Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10 -0
+			      const struct pwm_state *state)
+{
+	int ret;
+	u64 period, duty_cycle;
+	bool enabled = pwm->state.enabled;
+
+	if (enabled && !state->enabled) {
+		pwm_loongson_disable(chip, pwm);
+		return 0;
+	}

You can also shortcut if !pwm->state.enabled. Something like:

	if (!state->enabled) {
		if (enabled)
			pwm_loongson_disable(chip, pwm);
		return 0;
	}

drivers/pwm/pwm-loongson.c line 150

Quoted 5 lines
drivers/pwm/pwm-loongson.c new file +5 -0
+	if (state->polarity != pwm->state.polarity) {
+		ret = pwm_loongson_set_polarity(chip, pwm, state->polarity);
+		if (ret)
+			return ret;
+	}

Together with the shortcut above this is buggy. Consider:

pwm_apply_might_sleep(mypwm, &(struct pwm_state){
.period = A,
.duty_cycle = B, .polarity = PWM_POLARITY_NORMAL, .enabled = true});
pwm_apply_might_sleep(mypwm, &(struct pwm_state){
.period = A,
.duty_cycle = B, .polarity = PWM_POLARITY_INVERSED, .enabled = false});
pwm_apply_might_sleep(mypwm, &(struct pwm_state){
.period = A,
.duty_cycle = B, .polarity = PWM_POLARITY_INVERSED, .enabled = true});

After the 2nd call you left pwm_loongson_apply() early without writing the inversed polarity to the register space. In the 3rd call you have state->polarity == pwm->state.polarity and so skip configuring the polarity again.

I suggest to just do pwm_loongson_set_polarity() unconditionally if state->enabled = true.

drivers/pwm/pwm-loongson.c line 163

Quoted 12 lines
drivers/pwm/pwm-loongson.c new file +12 -0
+	period = min(state->period, NANOHZ_PER_HZ);
+	duty_cycle = min(state->duty_cycle, NANOHZ_PER_HZ);
+
+	ret = pwm_loongson_config(chip, pwm, duty_cycle, period);
+	if (ret)
+		return ret;
+
+	if (!enabled && state->enabled)
+		ret = pwm_loongson_enable(chip, pwm);
+
+	return ret;
+}

Best regards
Uwe

#4 lore raw

Re: [PATCH v5 2/2] pwm: Add Loongson PWM controller support

Hi Uwe:

Thanks for your review.

Quoted 28 lines · Uwe Kleine-König

Hello,

Quoted 10 lines · Binbin Zhou
drivers/pwm/pwm-loongson.c new file +4 -0
new file mode 100644
index 000000000000..17ab2a2f48ad
@@ -0,0 +1,285 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Loongson PWM driver
+ *

drivers/pwm/pwm-loongson.c line 14

Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10 -0
+ * Author: Juxin Gao <[email protected]>
+ * Further cleanup and restructuring by:
+ *         Binbin Zhou <[email protected]>
+ *
+ * Copyright (C) 2017-2024 Loongson Technology Corporation Limited.
+ *
+ * Limitations:
+ * - The buffer register value should be written before the CTRL register.
+ * - When disabled the output is driven to 0 independent of the configured
+ *   polarity.

An info about possible glitches and if a period is completed on reconfiguration or when the PWM is disabled would be great.

Quoted 1 line

Also if there is a publically available manual, please add a link here.

The descriptions related to our PWM controllers are in the CPU manuals, e.g. for the Loongson-2K series CPUs. but unfortunately we only have the Chinese manuals right now, so I don't have a link to them here.

Quoted 48 lines
Quoted 5 lines
drivers/pwm/pwm-loongson.c new file +3 -0
+ */
+
[...]
+static int pwm_loongson_apply(struct pwm_chip *chip, struct pwm_device *pwm,

drivers/pwm/pwm-loongson.c line 144

Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10 -0
+                           const struct pwm_state *state)
+{
+     int ret;
+     u64 period, duty_cycle;
+     bool enabled = pwm->state.enabled;
+
+     if (enabled && !state->enabled) {
+             pwm_loongson_disable(chip, pwm);
+             return 0;
+     }

You can also shortcut if !pwm->state.enabled. Something like:

        if (!state->enabled) {
                if (enabled)
                        pwm_loongson_disable(chip, pwm);
                return 0;
        }

drivers/pwm/pwm-loongson.c line 150

Quoted 5 lines
drivers/pwm/pwm-loongson.c new file +5 -0
+     if (state->polarity != pwm->state.polarity) {
+             ret = pwm_loongson_set_polarity(chip, pwm, state->polarity);
+             if (ret)
+                     return ret;
+     }

Together with the shortcut above this is buggy. Consider:

pwm_apply_might_sleep(mypwm, &(struct pwm_state){
.period = A,
.duty_cycle = B, .polarity = PWM_POLARITY_NORMAL, .enabled = true});
pwm_apply_might_sleep(mypwm, &(struct pwm_state){
.period = A,
.duty_cycle = B, .polarity = PWM_POLARITY_INVERSED, .enabled = false});
pwm_apply_might_sleep(mypwm, &(struct pwm_state){
.period = A,
.duty_cycle = B, .polarity = PWM_POLARITY_INVERSED, .enabled = true});

Quoted 7 lines

After the 2nd call you left pwm_loongson_apply() early without writing the inversed polarity to the register space. In the 3rd call you have state->polarity == pwm->state.polarity and so skip configuring the polarity again.

I suggest to just do pwm_loongson_set_polarity() unconditionally if state->enabled = true.

I check code and try to do it like:

        if (!state->enabled) {
                if (enabled)
                        pwm_loongson_disable(chip, pwm);
                return 0;
        }

        ret = pwm_loongson_set_polarity(chip, pwm, state->polarity);
        if (ret)
                return ret;

Thanks.
Binbin

Quoted 16 lines

drivers/pwm/pwm-loongson.c line 163

Quoted 13 lines
drivers/pwm/pwm-loongson.c new file +12 -0
+     period = min(state->period, NANOHZ_PER_HZ);
+     duty_cycle = min(state->duty_cycle, NANOHZ_PER_HZ);
+
+     ret = pwm_loongson_config(chip, pwm, duty_cycle, period);
+     if (ret)
+             return ret;
+
+     if (!enabled && state->enabled)
+             ret = pwm_loongson_enable(chip, pwm);
+
+     return ret;
+}

Best regards
Uwe