pwm: Introduce pwm driver for the Loongson family chips
Subsystems (4)
Binbin Zhou
maintainerv4 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.
-------
V4:
patch (2/2):
- Rebase on pwm/for-next;
- Addressed Uwe's review comments, thanks.
- 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();
- Put the parameters used by PM into a separate
structure(pwm_loongson_suspend_store);
- 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 | 295 ++++++++++++++++++ 5 files changed, 381 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.0
Binbin Zhou
maintainerPATCH v4 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
@@ -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
@@ -12992,6 +12992,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.0
Binbin Zhou
maintainerPATCH v4 2/2 pwm: Add Loongson PWM controller support
In reply to Binbin ZhouThis 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 | 295 +++++++++++++++++++++++++++++++++++++ 4 files changed, 309 insertions(+) create mode 100644 drivers/pwm/pwm-loongson.c
MAINTAINERS +1 -0
@@ -12997,6 +12997,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
@@ -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
@@ -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 +295 -0
295 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v4 2/2] pwm: Add Loongson PWM controller support
Hello,
Quoted 16 lines · Binbin Zhou
drivers/pwm/pwm-loongson.c new file +16
-0+static int pwm_loongson_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +int ret; +u64 period, duty_cycle; +bool enabled = pwm->state.enabled; + + period =min (state->period,NANOHZ_PER_HZ ); + duty_cycle =min (state->duty_cycle,NANOHZ_PER_HZ ); + +if (state->polarity != pwm->state.polarity) { +if (enabled) { +pwm_loongson_disable (chip, pwm); + enabled = false; + } +
drivers/pwm/pwm-loongson.c line 159
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+ ret =pwm_loongson_set_polarity (chip, pwm, state->polarity); +if (ret) +return ret; + } + +if (!state->enabled) { +if (enabled) +pwm_loongson_disable (chip, pwm); +return 0 ; + }
Given that the configured polarity isn't relevant for a disabled PWM, I suggest to swap these two if blocks. However then you have to be a bit more careful for the polarity check because otherwise the following series of commands yields wrong results:
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_NORMAL, .enabled = true});
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = false});
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = true});Quoted 20 lines
drivers/pwm/pwm-loongson.c new file +20
-0+ ret =pwm_loongson_config (chip, pwm, duty_cycle, period); +if (ret) +return ret; + +if (!enabled) + ret =pwm_loongson_enable (chip, pwm); + +return ret; +} + +static int pwm_loongson_get_state (struct pwm_chip *chip,struct pwm_device *pwm, +struct pwm_state *state) +{ +u32 duty, period, ctrl; +struct pwm_loongson_ddata *ddata =to_pwm_loongson_ddata (chip); + +/* duty_cycle = ddata->duty * NSEC_PER_SEC / ddata->clk_rate */ + duty =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_DUTY ); + state->duty_cycle =mul_u64_u64_div_u64 (duty,NSEC_PER_SEC , ddata->clk_rate); +
drivers/pwm/pwm-loongson.c line 190
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+/* period = ddata->period * NSEC_PER_SEC / ddata->clk_rate */ + period =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_PERIOD ); + state->period =mul_u64_u64_div_u64 (period,NSEC_PER_SEC , ddata->clk_rate); + + ctrl =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_CTRL ); + state->polarity = (ctrl &LOONGSON_PWM_CTRL_INVERT ) ?PWM_POLARITY_INVERSED : +PWM_POLARITY_NORMAL ; + state->enabled = (ctrl &LOONGSON_PWM_CTRL_EN ) ? true : false; + +return 0 ;
You didn't test extensively with PWM_DEBUG enabled, right? You need to round up the divisions here otherwise you get strange rounding results:
Consider ddata->clk_rate = 1594323. When a state with .period = 20000 is applied, LOONGSON_PWM_REG_PERIOD is assigned 31.
Calling .get_state() in this situation gives .period = 19443. Reapplying .period = 19443 results in LOONGSON_PWM_REG_PERIOD := 30. Iterating this further yields:
- .period = 18816
- LOONGSON_PWM_REG_PERIOD := 29
- .period = 18189
- LOONGSON_PWM_REG_PERIOD := 28
- ...
Quoted 29 lines
drivers/pwm/pwm-loongson.c new file +29
-0+} + +static const struct pwm_ops pwm_loongson_ops = { + .apply = pwm_loongson_apply, + .get_state = pwm_loongson_get_state, +}; + +static int pwm_loongson_probe (struct platform_device *pdev) +{ +int ret; +struct pwm_chip *chip; +struct pwm_loongson_ddata *ddata; +struct device *dev = &pdev->dev; + + chip =devm_pwmchip_alloc (dev,1 ,sizeof (*ddata)); +if (IS_ERR (chip)) +return PTR_ERR (chip); + ddata =to_pwm_loongson_ddata (chip); + + ddata->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (ddata->base)) +return PTR_ERR (ddata->base); + +if (!has_acpi_companion (dev)) { + ddata->clk =devm_clk_get_enabled (dev,NULL ); +if (IS_ERR (ddata->clk)) +return dev_err_probe (dev,PTR_ERR (ddata->clk), +"failed to get pwm clock\n" ); + ddata->clk_rate =clk_get_rate (ddata->clk);
drivers/pwm/pwm-loongson.c line 229
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+ }else { + ddata->clk_rate =LOONGSON_PWM_FREQ_STD ; + } + + chip->ops = &pwm_loongson_ops; +dev_set_drvdata (dev, chip); + + ret =devm_pwmchip_add (dev, chip); +if (ret <0 ) { +clk_disable_unprepare (ddata->clk);
This is wrong. You aquired the clk using devm_clk_get_enabled(), so you don't need (and must not) care for disable.
drivers/pwm/pwm-loongson.c line 234
Quoted 5 lines
drivers/pwm/pwm-loongson.c new file +5
-0+return dev_err_probe (dev, ret,"failed to add PWM chip\n" ); + } + +return 0 ; +}
Best regards
Uwe
Re: [PATCH v4 2/2] pwm: Add Loongson PWM controller support
Hi Uwe:
Thanks for your reply.
Quoted 36 lines · Uwe Kleine-König
Hello,
Quoted 16 lines · Binbin Zhou
drivers/pwm/pwm-loongson.c new file +16
-0+static int pwm_loongson_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +int ret; +u64 period, duty_cycle; +bool enabled = pwm->state.enabled; + + period =min (state->period,NANOHZ_PER_HZ ); + duty_cycle =min (state->duty_cycle,NANOHZ_PER_HZ ); + +if (state->polarity != pwm->state.polarity) { +if (enabled) { +pwm_loongson_disable (chip, pwm); + enabled = false; + } +drivers/pwm/pwm-loongson.c line 159
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+ ret =pwm_loongson_set_polarity (chip, pwm, state->polarity); +if (ret) +return ret; + } + +if (!state->enabled) { +if (enabled) +pwm_loongson_disable (chip, pwm); +return 0 ; + }Given that the configured polarity isn't relevant for a disabled PWM, I suggest to swap these two if blocks. However then you have to be a bit more careful for the polarity check because otherwise the following series of commands yields wrong results:
Quoted 4 lines
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_NORMAL, .enabled = true});
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = false});
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = true});Yes, we'd better make sure pwm is enabled first.
I will swap the two if blocks.
Quoted 40 lines
Quoted 20 lines
drivers/pwm/pwm-loongson.c new file +20
-0+ ret =pwm_loongson_config (chip, pwm, duty_cycle, period); +if (ret) +return ret; + +if (!enabled) + ret =pwm_loongson_enable (chip, pwm); + +return ret; +} + +static int pwm_loongson_get_state (struct pwm_chip *chip,struct pwm_device *pwm, +struct pwm_state *state) +{ +u32 duty, period, ctrl; +struct pwm_loongson_ddata *ddata =to_pwm_loongson_ddata (chip); + +/* duty_cycle = ddata->duty * NSEC_PER_SEC / ddata->clk_rate */ + duty =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_DUTY ); + state->duty_cycle =mul_u64_u64_div_u64 (duty,NSEC_PER_SEC , ddata->clk_rate); +drivers/pwm/pwm-loongson.c line 190
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+/* period = ddata->period * NSEC_PER_SEC / ddata->clk_rate */ + period =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_PERIOD ); + state->period =mul_u64_u64_div_u64 (period,NSEC_PER_SEC , ddata->clk_rate); + + ctrl =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_CTRL ); + state->polarity = (ctrl &LOONGSON_PWM_CTRL_INVERT ) ?PWM_POLARITY_INVERSED : +PWM_POLARITY_NORMAL ; + state->enabled = (ctrl &LOONGSON_PWM_CTRL_EN ) ? true : false; + +return 0 ;You didn't test extensively with
PWM_DEBUGenabled, right? You need to round up the divisions here otherwise you get strange rounding results:Consider
ddata->clk_rate= 1594323. When a state with .period = 20000 is applied,LOONGSON_PWM_REG_PERIODis assigned 31.
Calling .get_state() in this situation gives .period = 19443. Reapplying .period = 19443 results inLOONGSON_PWM_REG_PERIOD:= 30. Iterating this further yields:
Quoted 6 lines
- .period = 18816
-LOONGSON_PWM_REG_PERIOD:= 29
- .period = 18189
-LOONGSON_PWM_REG_PERIOD:= 28
- ...
Yes, I'm very sorry I didn't do extensive testing with PWM_DEBUG enabled. Here I do need to use DIV64_U64_ROUND_UP().
Below:
/* duty_cycle = ddata->duty * NSEC_PER_SEC / ddata->clk_rate */
duty = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_DUTY);
state->duty_cycle = DIV64_U64_ROUND_UP((u64)duty *NSEC_PER_SEC, ddata->clk_rate);
/* period = ddata->period * NSEC_PER_SEC / ddata->clk_rate */
period = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_PERIOD);
state->period = DIV64_U64_ROUND_UP((u64)period * NSEC_PER_SEC,ddata->clk_rate);
I'd also like to ask which tests I still need to do to make sure the driver is more complete?
Quoted 34 lines
Quoted 34 lines
drivers/pwm/pwm-loongson.c new file +34
-0+} + +static const struct pwm_ops pwm_loongson_ops = { + .apply = pwm_loongson_apply, + .get_state = pwm_loongson_get_state, +}; + +static int pwm_loongson_probe (struct platform_device *pdev) +{ +int ret; +struct pwm_chip *chip; +struct pwm_loongson_ddata *ddata; +struct device *dev = &pdev->dev; + + chip =devm_pwmchip_alloc (dev,1 ,sizeof (*ddata)); +if (IS_ERR (chip)) +return PTR_ERR (chip); + ddata =to_pwm_loongson_ddata (chip); + + ddata->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (ddata->base)) +return PTR_ERR (ddata->base); + +if (!has_acpi_companion (dev)) { + ddata->clk =devm_clk_get_enabled (dev,NULL ); +if (IS_ERR (ddata->clk)) +return dev_err_probe (dev,PTR_ERR (ddata->clk), +"failed to get pwm clock\n" ); + ddata->clk_rate =clk_get_rate (ddata->clk); + }else { + ddata->clk_rate =LOONGSON_PWM_FREQ_STD ; + } + + chip->ops = &pwm_loongson_ops;
Quoted 9 lines
drivers/pwm/pwm-loongson.c line 229
Quoted 5 lines
drivers/pwm/pwm-loongson.c new file +5
-0+dev_set_drvdata (dev, chip); + + ret =devm_pwmchip_add (dev, chip); +if (ret <0 ) { +clk_disable_unprepare (ddata->clk);This is wrong. You aquired the clk using
devm_clk_get_enabled(), so you don't need (and must not) care for disable.
Sorry, I had some misunderstanding about devm_clk_get_enabled(), I will fix it.
Thanks.
Binbin
Quoted 8 lines
drivers/pwm/pwm-loongson.c line 234
Quoted 5 lines
drivers/pwm/pwm-loongson.c new file +5
-0+return dev_err_probe (dev, ret,"failed to add PWM chip\n" ); + } + +return 0 ; +}Best regards
Uwe
Re: [PATCH v4 2/2] pwm: Add Loongson PWM controller support
Hello Binbin,
Quoted 106 lines · Binbin Zhou
Hi Uwe:
Thanks for your reply.
Quoted 36 lines · Uwe Kleine-König
Hello,
Quoted 16 lines · Binbin Zhou
drivers/pwm/pwm-loongson.c new file +16
-0+static int pwm_loongson_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +int ret; +u64 period, duty_cycle; +bool enabled = pwm->state.enabled; + + period =min (state->period,NANOHZ_PER_HZ ); + duty_cycle =min (state->duty_cycle,NANOHZ_PER_HZ ); + +if (state->polarity != pwm->state.polarity) { +if (enabled) { +pwm_loongson_disable (chip, pwm); + enabled = false; + } +drivers/pwm/pwm-loongson.c line 159
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+ ret =pwm_loongson_set_polarity (chip, pwm, state->polarity); +if (ret) +return ret; + } + +if (!state->enabled) { +if (enabled) +pwm_loongson_disable (chip, pwm); +return 0 ; + }Given that the configured polarity isn't relevant for a disabled PWM, I suggest to swap these two if blocks. However then you have to be a bit more careful for the polarity check because otherwise the following series of commands yields wrong results:
Quoted 4 lines
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_NORMAL, .enabled = true}); pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = false}); pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = true});Yes, we'd better make sure pwm is enabled first.
I will swap the two if blocks.Quoted 40 lines
Quoted 20 lines
drivers/pwm/pwm-loongson.c new file +20
-0+ ret =pwm_loongson_config (chip, pwm, duty_cycle, period); +if (ret) +return ret; + +if (!enabled) + ret =pwm_loongson_enable (chip, pwm); + +return ret; +} + +static int pwm_loongson_get_state (struct pwm_chip *chip,struct pwm_device *pwm, +struct pwm_state *state) +{ +u32 duty, period, ctrl; +struct pwm_loongson_ddata *ddata =to_pwm_loongson_ddata (chip); + +/* duty_cycle = ddata->duty * NSEC_PER_SEC / ddata->clk_rate */ + duty =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_DUTY ); + state->duty_cycle =mul_u64_u64_div_u64 (duty,NSEC_PER_SEC , ddata->clk_rate); +drivers/pwm/pwm-loongson.c line 190
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+/* period = ddata->period * NSEC_PER_SEC / ddata->clk_rate */ + period =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_PERIOD ); + state->period =mul_u64_u64_div_u64 (period,NSEC_PER_SEC , ddata->clk_rate); + + ctrl =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_CTRL ); + state->polarity = (ctrl &LOONGSON_PWM_CTRL_INVERT ) ?PWM_POLARITY_INVERSED : +PWM_POLARITY_NORMAL ; + state->enabled = (ctrl &LOONGSON_PWM_CTRL_EN ) ? true : false; + +return 0 ;You didn't test extensively with
PWM_DEBUGenabled, right? You need to round up the divisions here otherwise you get strange rounding results:Consider
ddata->clk_rate= 1594323. When a state with .period = 20000 is applied,LOONGSON_PWM_REG_PERIODis assigned 31.
Calling .get_state() in this situation gives .period = 19443. Reapplying .period = 19443 results inLOONGSON_PWM_REG_PERIOD:= 30. Iterating this further yields:Quoted 6 lines
- .period = 18816
-LOONGSON_PWM_REG_PERIOD:= 29
- .period = 18189
-LOONGSON_PWM_REG_PERIOD:= 28
- ...Yes, I'm very sorry I didn't do extensive testing with
PWM_DEBUGenabled. Here I do need to useDIV64_U64_ROUND_UP().Below:
/* duty_cycle = ddata->duty * NSEC_PER_SEC / ddata->clk_rate */ duty = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_DUTY); state->duty_cycle = DIV64_U64_ROUND_UP((u64)duty *
NSEC_PER_SEC,ddata->clk_rate);
Quoted 8 lines
/* period = ddata->period * NSEC_PER_SEC / ddata->clk_rate */ period = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_PERIOD); state->period = DIV64_U64_ROUND_UP((u64)period * NSEC_PER_SEC,
ddata->clk_rate);I'd also like to ask which tests I still need to do to make sure the driver is more complete?
There is no official lower limit on tests. But the goal is that your driver behaves correctly and some of the typical errors can be catched by enabling PWM_DEBUG and then doing the following tests for sensible values of A and B:
# Sequence of increasing periods
for period in A ... B:
configure 0/period
# Sequence of decreasing periods
for period in A ... B:
configure 0/(B + A - period)
for period in some set:
# Sequence of increasing duty length for duty_cycle in 0 ... period:
configure duty_cycle/period
# Sequence of decreasing duty length for duty_cycle in 0 ... period:
configure (period - duty_cycle)/period
That should give you a good coverage.
The idea of extensive testing on your side is also: Review capacities are a scarce resource and you suffer from that because it takes some patience between you sending a patch and a maintainer coming around to review it. If your code is well tested, there is less for the maintainers to find and so I save time because there are less revisions and they mature quicker. The direct consequence for you and the others waiting for my attention should be obvious. (This also means: If you look into other's patch submissions and point out the things you already learned to them, you also save maintainer time and so might get their attention earlier yourself.)
I'm working on a bigger change for the pwm subsystem. One of the results will be a new userspace API. I intend to create a test program that does the above tests, so in the foreseeable future testing should get easier. (In return writing a lowlevel driver will become a bit harder, as the requirements for precision increase.)
Best regards
Uwe
Re: [PATCH v4 2/2] pwm: Add Loongson PWM controller support
Hi Uwe:
Quoted 137 lines · Uwe Kleine-König
Hello Binbin,
Quoted 106 lines · Binbin Zhou
Hi Uwe:
Thanks for your reply.
Quoted 36 lines · Uwe Kleine-König
Hello,
Quoted 16 lines · Binbin Zhou
drivers/pwm/pwm-loongson.c new file +16
-0+static int pwm_loongson_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +int ret; +u64 period, duty_cycle; +bool enabled = pwm->state.enabled; + + period =min (state->period,NANOHZ_PER_HZ ); + duty_cycle =min (state->duty_cycle,NANOHZ_PER_HZ ); + +if (state->polarity != pwm->state.polarity) { +if (enabled) { +pwm_loongson_disable (chip, pwm); + enabled = false; + } +drivers/pwm/pwm-loongson.c line 159
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+ ret =pwm_loongson_set_polarity (chip, pwm, state->polarity); +if (ret) +return ret; + } + +if (!state->enabled) { +if (enabled) +pwm_loongson_disable (chip, pwm); +return 0 ; + }Given that the configured polarity isn't relevant for a disabled PWM, I suggest to swap these two if blocks. However then you have to be a bit more careful for the polarity check because otherwise the following series of commands yields wrong results:
Quoted 4 lines
pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_NORMAL, .enabled = true}); pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = false}); pwm_apply_might_sleep(pwm, {.duty_cycle = D, .period = P, .polarity = PWM_POLARITY_INVERSED, .enabled = true});Yes, we'd better make sure pwm is enabled first.
I will swap the two if blocks.Quoted 40 lines
Quoted 20 lines
drivers/pwm/pwm-loongson.c new file +20
-0+ ret =pwm_loongson_config (chip, pwm, duty_cycle, period); +if (ret) +return ret; + +if (!enabled) + ret =pwm_loongson_enable (chip, pwm); + +return ret; +} + +static int pwm_loongson_get_state (struct pwm_chip *chip,struct pwm_device *pwm, +struct pwm_state *state) +{ +u32 duty, period, ctrl; +struct pwm_loongson_ddata *ddata =to_pwm_loongson_ddata (chip); + +/* duty_cycle = ddata->duty * NSEC_PER_SEC / ddata->clk_rate */ + duty =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_DUTY ); + state->duty_cycle =mul_u64_u64_div_u64 (duty,NSEC_PER_SEC , ddata->clk_rate); +drivers/pwm/pwm-loongson.c line 190
Quoted 10 lines
drivers/pwm/pwm-loongson.c new file +10
-0+/* period = ddata->period * NSEC_PER_SEC / ddata->clk_rate */ + period =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_PERIOD ); + state->period =mul_u64_u64_div_u64 (period,NSEC_PER_SEC , ddata->clk_rate); + + ctrl =pwm_loongson_readl (ddata,LOONGSON_PWM_REG_CTRL ); + state->polarity = (ctrl &LOONGSON_PWM_CTRL_INVERT ) ?PWM_POLARITY_INVERSED : +PWM_POLARITY_NORMAL ; + state->enabled = (ctrl &LOONGSON_PWM_CTRL_EN ) ? true : false; + +return 0 ;You didn't test extensively with
PWM_DEBUGenabled, right? You need to round up the divisions here otherwise you get strange rounding results:Consider
ddata->clk_rate= 1594323. When a state with .period = 20000 is applied,LOONGSON_PWM_REG_PERIODis assigned 31.
Calling .get_state() in this situation gives .period = 19443. Reapplying .period = 19443 results inLOONGSON_PWM_REG_PERIOD:= 30. Iterating this further yields:Quoted 6 lines
- .period = 18816
-LOONGSON_PWM_REG_PERIOD:= 29
- .period = 18189
-LOONGSON_PWM_REG_PERIOD:= 28
- ...Yes, I'm very sorry I didn't do extensive testing with
PWM_DEBUGenabled. Here I do need to useDIV64_U64_ROUND_UP().Below:
/* duty_cycle = ddata->duty * NSEC_PER_SEC / ddata->clk_rate */ duty = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_DUTY); state->duty_cycle = DIV64_U64_ROUND_UP((u64)duty *
NSEC_PER_SEC,ddata->clk_rate);Quoted 8 lines
/* period = ddata->period * NSEC_PER_SEC / ddata->clk_rate */ period = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_PERIOD); state->period = DIV64_U64_ROUND_UP((u64)period * NSEC_PER_SEC,
ddata->clk_rate);I'd also like to ask which tests I still need to do to make sure the driver is more complete?
There is no official lower limit on tests. But the goal is that your driver behaves correctly and some of the typical errors can be catched by enabling
PWM_DEBUGand then doing the following tests for sensible values of A and B:# Sequence of increasing periods
for period in A ... B:
configure 0/period# Sequence of decreasing periods
for period in A ... B:
configure 0/(B + A - period)for period in some set:
# Sequence of increasing duty length forduty_cyclein 0 ... period:
configure duty_cycle/period
Quoted 5 lines
# Sequence of decreasing duty length for
duty_cyclein 0 ... period:
configure (period - duty_cycle)/periodThat should give you a good coverage.
Thanks for your advice.
I would use this approach to test extensively, trying to make sure there are no typical errors.
Thanks.
Binbin
Quoted 20 lines
The idea of extensive testing on your side is also: Review capacities are a scarce resource and you suffer from that because it takes some patience between you sending a patch and a maintainer coming around to review it. If your code is well tested, there is less for the maintainers to find and so I save time because there are less revisions and they mature quicker. The direct consequence for you and the others waiting for my attention should be obvious. (This also means: If you look into other's patch submissions and point out the things you already learned to them, you also save maintainer time and so might get their attention earlier yourself.)
I'm working on a bigger change for the pwm subsystem. One of the results will be a new userspace API. I intend to create a test program that does the above tests, so in the foreseeable future testing should get easier. (In return writing a lowlevel driver will become a bit harder, as the requirements for precision increase.)
Best regards
Uwe
Quoted 22 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.
-------
V4:
patch (2/2):
- Rebase on pwm/for-next;
- Addressed Uwe's review comments, thanks.
- Make use ofdevm_pwmchip_alloc()function;
- Add Limitations description;
- AddLOONGSON_prefix for Loongson pwm register defines;
- Keep regs written only once;
- Rewrite duty/period calculation;
- Adddev_err_probe()in .probe();
- Put the parameters used by PM into a separate
Quoted 5 lines
structure(pwm_loongson_suspend_store);- Fix some code style.
Link to V3:
https://lore.kernel.org/linux-pwm/[email protected]/
Hi Uwe:
Gentle ping.
I seem to have addressed your review comments for the v3 patchset, please review it when you have time.
Thanks.
Binbin
Quoted 39 lines
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, forpwm.yamlmakes 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 | 295 ++++++++++++++++++ 5 files changed, 381 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml create mode 100644 drivers/pwm/pwm-loongson.c--
2.43.0
For the whole series,
Quoted 67 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.
-------
V4:
patch (2/2):
- Rebase on pwm/for-next;
- Addressed Uwe's review comments, thanks.
- Make use ofdevm_pwmchip_alloc()function;
- Add Limitations description;
- AddLOONGSON_prefix for Loongson pwm register defines;
- Keep regs written only once;
- Rewrite duty/period calculation;
- Adddev_err_probe()in .probe();
- Put the parameters used by PM into a separatestructure(pwm_loongson_suspend_store);- 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, forpwm.yamlmakes 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 | 295 ++++++++++++++++++ 5 files changed, 381 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/loongson,ls7a-pwm.yaml create mode 100644 drivers/pwm/pwm-loongson.c--
2.43.0