pwm: sprd: Modification of UNISOC Platform PWM Driver
Subsystems (3)
cover pwm: sprd: Modification of UNISOC Platform PWM Driver
Due to new usage scenarios, some upgrades are made to unisoc's pwm driver. Patch 1 supports the change of channel offset on UMS9620.
Patch 2 supports more brightness levels (duty cycle) for backlight control. Patch 3 optimizes the calculation method of duty.
Patch 4 converts dt-binding file from txt to yaml
Patch 5-6 update pwm-sprd.yaml according to patch 1 and patch 2.
Wenhua Lin (6):
pwm: sprd: Add support for UMS9620
pwm: sprd: Improve the pwm backlight control function
pwm: sprd: Optimize the calculation method of duty
dt-bindings: pwm: sprd: Convert to YAML
pwm: sprd: Add sprd,ums9620-pwm compatible
dt-bindings: pwm: sprd: Add sprd,mod attribute
.../devicetree/bindings/pwm/pwm-sprd.txt | 40 ------- .../devicetree/bindings/pwm/pwm-sprd.yaml | 106 ++++++++++++++++++ drivers/pwm/pwm-sprd.c | 71 ++++++++++-- 3 files changed, 165 insertions(+), 52 deletions(-) delete mode 100644 Documentation/devicetree/bindings/pwm/pwm-sprd.txt create mode 100644 Documentation/devicetree/bindings/pwm/pwm-sprd.yaml
Signature · 1 line
2.17.1
PATCH 1/6 pwm: sprd: Add support for UMS9620
The PMW unit on the current Unisoc's SoCs has 4 channels but has different address offsets. On UMS512, they are 0x0, 0x20, 0x40, 0x60 respectively, while are 0x0, 0x4000, 0x8000, 0xC000 on UMS9620.
--- drivers/pwm/pwm-sprd.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/pwm/pwm-sprd.c +24 -4
@@ -9,6 +9,7 @@#include <linux/math64.h> #include <linux/mod_devicetable.h> #include <linux/module.h> +#include <linux/of.h> #include <linux/platform_device.h> #include <linux/pwm.h>
@@ -23,7 +24,6 @@#define SPRD_PWM_ENABLE_BIT BIT(0)#define SPRD_PWM_CHN_NUM 4 -#define SPRD_PWM_REGS_SHIFT 5#define SPRD_PWM_CHN_CLKS_NUM 2#define SPRD_PWM_CHN_OUTPUT_CLK 1
@@ -32,14 +32,27 @@ struct sprd_pwm_chn {u32 clk_rate; }; +struct sprd_pwm_data { +int reg_shift; +}; +struct sprd_pwm_chip {void __iomem *base;struct device *dev;struct pwm_chip chip; +const struct sprd_pwm_data *pdata;int num_pwms;struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM ]; }; +static const struct sprd_pwm_data ums512_data = { + .reg_shift =5 , +}; + +static const struct sprd_pwm_data ums9620_data = { + .reg_shift =14 , +}; +static inline struct sprd_pwm_chip *sprd_pwm_from_chip (struct pwm_chip *chip) { return container_of(chip, struct sprd_pwm_chip, chip);
@@ -58,7 +71,7 @@ static const char * const sprd_pwm_clks[] = {static u32 sprd_pwm_read (struct sprd_pwm_chip *spc,u32 hwid,u32 reg) { -u32 offset = reg + (hwid <<SPRD_PWM_REGS_SHIFT ); +u32 offset = reg + (hwid << spc->pdata->reg_shift);return readl_relaxed (spc->base + offset); }
@@ -66,7 +79,7 @@ static u32 sprd_pwm_read(struct sprd_pwm_chip *spc, u32 hwid, u32 reg)static void sprd_pwm_write (struct sprd_pwm_chip *spc,u32 hwid,u32 reg,u32 val) { -u32 offset = reg + (hwid <<SPRD_PWM_REGS_SHIFT ); +u32 offset = reg + (hwid << spc->pdata->reg_shift);writel_relaxed (val, spc->base + offset); }
@@ -253,6 +266,7 @@ static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc)static int sprd_pwm_probe (struct platform_device *pdev) {struct sprd_pwm_chip *spc; +const void *priv;int ret; spc =devm_kzalloc (&pdev->dev,sizeof (*spc),GFP_KERNEL );
@@ -263,6 +277,11 @@ static int sprd_pwm_probe(struct platform_device *pdev)if (IS_ERR (spc->base))return PTR_ERR (spc->base); + priv =of_device_get_match_data (&pdev->dev); +if (!priv) +return dev_err_probe (&pdev->dev, -EINVAL ,"get regs shift failed!\n" ); + spc->pdata = priv; + spc->dev = &pdev->dev; ret =sprd_pwm_clk_init (spc);
@@ -281,7 +300,8 @@ static int sprd_pwm_probe(struct platform_device *pdev) }static const struct of_device_id sprd_pwm_of_match[] = { - { .compatible ="sprd,ums512-pwm" , }, + { .compatible ="sprd,ums512-pwm" , .data = (void *)&ums512_data}, + { .compatible ="sprd,ums9620-pwm" , .data = (void *)&ums9620_data}, { }, };MODULE_DEVICE_TABLE (of, sprd_pwm_of_match);
Signature · 1 line
2.17.1
Re: [PATCH 1/6] pwm: sprd: Add support for UMS9620
Quoted 76 lines · Wenhua Lin
The PMW unit on the current Unisoc's SoCs has 4 channels but has different address offsets. On UMS512, they are
0x0,0x20,0x40,0x60respectively, while are0x0,0x4000,0x8000,0xC000on UMS9620.Signed-off-by Wenhua Lin <[email protected]>--- drivers/pwm/pwm-sprd.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)drivers/pwm/pwm-sprd.c +16
-3@@ -9,6 +9,7 @@#include <linux/math64.h> #include <linux/mod_devicetable.h> #include <linux/module.h> +#include <linux/of.h> #include <linux/platform_device.h> #include <linux/pwm.h> @@ -23,7 +24,6 @@#define SPRD_PWM_ENABLE_BIT BIT(0)#define SPRD_PWM_CHN_NUM 4 -#define SPRD_PWM_REGS_SHIFT 5#define SPRD_PWM_CHN_CLKS_NUM 2#define SPRD_PWM_CHN_OUTPUT_CLK 1@@ -32,14 +32,27 @@ struct sprd_pwm_chn {u32 clk_rate; }; +struct sprd_pwm_data { +int reg_shift; +}; +struct sprd_pwm_chip {void __iomem *base;struct device *dev;struct pwm_chip chip; +const struct sprd_pwm_data *pdata;int num_pwms;struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM ]; }; +static const struct sprd_pwm_data ums512_data = { + .reg_shift =5 , +}; + +static const struct sprd_pwm_data ums9620_data = { + .reg_shift =14 , +}; +static inline struct sprd_pwm_chip *sprd_pwm_from_chip (struct pwm_chip *chip) { return container_of(chip, struct sprd_pwm_chip, chip);@@ -58,7 +71,7 @@ static const char * const sprd_pwm_clks[] = {static u32 sprd_pwm_read (struct sprd_pwm_chip *spc,u32 hwid,u32 reg) { -u32 offset = reg + (hwid <<SPRD_PWM_REGS_SHIFT ); +u32 offset = reg + (hwid << spc->pdata->reg_shift);return readl_relaxed (spc->base + offset); }@@ -66,7 +79,7 @@ static u32 sprd_pwm_read(struct sprd_pwm_chip *spc, u32 hwid, u32 reg)static void sprd_pwm_write (struct sprd_pwm_chip *spc,u32 hwid,u32 reg,u32 val) { -u32 offset = reg + (hwid <<SPRD_PWM_REGS_SHIFT ); +u32 offset = reg + (hwid << spc->pdata->reg_shift);writel_relaxed (val, spc->base + offset); }
drivers/pwm/pwm-sprd.c static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc) · line 269
Quoted 4 lines
drivers/pwm/pwm-sprd.c +1
-0@@ -253,6 +266,7 @@ static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc)static int sprd_pwm_probe (struct platform_device *pdev) {struct sprd_pwm_chip *spc; +const void *priv;
This can better be of type struct sprd_pwm_data *. Also pdata would be a better name.
Quoted 16 lines
drivers/pwm/pwm-sprd.c +5
-0int ret; spc =devm_kzalloc (&pdev->dev,sizeof (*spc),GFP_KERNEL );@@ -263,6 +277,11 @@ static int sprd_pwm_probe(struct platform_device *pdev)if (IS_ERR (spc->base))return PTR_ERR (spc->base); + priv =of_device_get_match_data (&pdev->dev); +if (!priv) +return dev_err_probe (&pdev->dev, -EINVAL ,"get regs shift failed!\n" ); + spc->pdata = priv; + spc->dev = &pdev->dev; ret =sprd_pwm_clk_init (spc);
drivers/pwm/pwm-sprd.c static int sprd_pwm_probe(struct platform_device *pdev) · line 304
Quoted 6 lines
drivers/pwm/pwm-sprd.c +2
-1@@ -281,7 +300,8 @@ static int sprd_pwm_probe(struct platform_device *pdev) }static const struct of_device_id sprd_pwm_of_match[] = { - { .compatible ="sprd,ums512-pwm" , }, + { .compatible ="sprd,ums512-pwm" , .data = (void *)&ums512_data}, + { .compatible ="sprd,ums9620-pwm" , .data = (void *)&ums9620_data},
Please use one line per assignment. Do you really need the cast to void *?
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH 2/6 pwm: sprd: Improve the pwm backlight control function
In reply to Wenhua LinThe pwm-sprd driver support only 8-bit linear control of backlight. Now, new requests of supporting 9-bit, 10-bit, 11-bit and 12-bit linear control of backlight are proposed. Besides, different channels of pwm could be configured into different linear control of backlight. Thus, sprd,mod attribute is introduced into dts for every channel of pwm device. This attribute would determine the value of MOD and eventually realize the new requirements.
--- drivers/pwm/pwm-sprd.c | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-)
drivers/pwm/pwm-sprd.c +34 -8
@@ -18,7 +18,8 @@#define SPRD_PWM_DUTY 0x8#define SPRD_PWM_ENABLE 0x18 -#define SPRD_PWM_MOD_MAX GENMASK(7, 0) +#define SPRD_PWM_MOD_MAX GENMASK(15, 0) +#define SPRD_PWM_MOD_DEFAULT GENMASK(9, 0)#define SPRD_PWM_DUTY_MSK GENMASK(15, 0)#define SPRD_PWM_PRESCALE_MSK GENMASK(7, 0)#define SPRD_PWM_ENABLE_BIT BIT(0)
@@ -43,6 +44,7 @@ struct sprd_pwm_chip {const struct sprd_pwm_data *pdata;int num_pwms;struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM ]; +u32 mod[SPRD_PWM_CHN_NUM ]; };static const struct sprd_pwm_data ums512_data = {
@@ -120,7 +122,7 @@ static int sprd_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,*/ val =sprd_pwm_read (spc, pwm->hwpwm,SPRD_PWM_PRESCALE ); prescale = val &SPRD_PWM_PRESCALE_MSK ; - tmp = (prescale +1 ) *NSEC_PER_SEC *SPRD_PWM_MOD_MAX ; + tmp = (prescale +1 ) *NSEC_PER_SEC * spc->mod[pwm->hwpwm]; state->period =DIV_ROUND_CLOSEST_ULL (tmp, chn->clk_rate); val =sprd_pwm_read (spc, pwm->hwpwm,SPRD_PWM_DUTY );
@@ -140,7 +142,7 @@ static int sprd_pwm_config(struct sprd_pwm_chip *spc, struct pwm_device *pwm, int duty_ns, int period_ns) {struct sprd_pwm_chn *chn = &spc->chn[pwm->hwpwm]; -u32 prescale, duty; +u32 prescale, duty, mod;u64 tmp;/*
@@ -148,16 +150,21 @@ static int sprd_pwm_config(struct sprd_pwm_chip *spc, struct pwm_device *pwm,* The period length is (PRESCALE + 1) * MOD counter steps. * The duty cycle length is (PRESCALE + 1) * DUTY counter steps. * -* To keep the maths simple we're always using MOD = SPRD_PWM_MOD_MAX. +* The value for MOD is obtained from dts. * The value for PRESCALE is selected such that the resulting period * gets the maximal length not bigger than the requested one with the -* given settings (MOD = SPRD_PWM_MOD_MAX and input clock). +* given settings (MOD and input clock). */ - duty = duty_ns *SPRD_PWM_MOD_MAX / period_ns; + mod = spc->mod[pwm->hwpwm]; + duty = duty_ns * mod / period_ns; tmp = (u64 )chn->clk_rate * period_ns;do_div (tmp,NSEC_PER_SEC ); - prescale =DIV_ROUND_CLOSEST_ULL (tmp,SPRD_PWM_MOD_MAX ) -1 ; + prescale =DIV_ROUND_CLOSEST_ULL (tmp, mod); +if (prescale <1 ) + prescale =1 ; + prescale--; +if (prescale >SPRD_PWM_PRESCALE_MSK ) prescale =SPRD_PWM_PRESCALE_MSK ;
@@ -170,7 +177,7 @@ static int sprd_pwm_config(struct sprd_pwm_chip *spc, struct pwm_device *pwm,* before changing a new configuration to avoid mixed settings. */ sprd_pwm_write (spc, pwm->hwpwm,SPRD_PWM_PRESCALE , prescale); -sprd_pwm_write (spc, pwm->hwpwm,SPRD_PWM_MOD ,SPRD_PWM_MOD_MAX ); +sprd_pwm_write (spc, pwm->hwpwm,SPRD_PWM_MOD , mod);sprd_pwm_write (spc, pwm->hwpwm,SPRD_PWM_DUTY , duty);return 0 ;
@@ -263,6 +270,21 @@ static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc)return 0 ; } +static int sprd_pwm_get_mod (struct platform_device *pdev) +{ +int i, ret; +struct sprd_pwm_chip *spc =platform_get_drvdata (pdev); + + ret =of_property_read_u32_array (pdev->dev.of_node, +"sprd,mod" , spc->mod, spc->num_pwms); +if (ret) { +for (i =0 ; i < spc->num_pwms; i++) + spc->mod[i] =SPRD_PWM_MOD_DEFAULT ; + } + +return ret; +} +static int sprd_pwm_probe (struct platform_device *pdev) {struct sprd_pwm_chip *spc;
@@ -288,6 +310,10 @@ static int sprd_pwm_probe(struct platform_device *pdev)if (ret)return ret; + ret =sprd_pwm_get_mod (pdev); +if (ret) +dev_err (&pdev->dev,"get pwm mod failed! Use default setting\n" ); + spc->chip.dev = &pdev->dev; spc->chip.ops = &sprd_pwm_ops; spc->chip.npwm = spc->num_pwms;
Signature · 1 line
2.17.1
Re: [PATCH 2/6] pwm: sprd: Improve the pwm backlight control function
drivers/pwm/pwm-sprd.c static int sprd_pwm_probe(struct platform_device *pdev) · line 315
Quoted 11 lines · Wenhua Lin
drivers/pwm/pwm-sprd.c +4
-0+static int sprd_pwm_probe (struct platform_device *pdev) {struct sprd_pwm_chip *spc;@@ -288,6 +310,10 @@ static int sprd_pwm_probe(struct platform_device *pdev)if (ret)return ret; + ret =sprd_pwm_get_mod (pdev); +if (ret) +dev_err (&pdev->dev,"get pwm mod failed! Use default setting\n" );
That's not an error. dev_info. Assuming that it will even stay (once you send proper version for proper review).
Best regards,
Krzysztof
PATCH 3/6 pwm: sprd: Optimize the calculation method of duty
In reply to Wenhua LinUse DIV_ROUND_CLOSEST_ULL to avoid overflow and improve accuracy when calculating duty.
--- drivers/pwm/pwm-sprd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pwm/pwm-sprd.c +2 -1
@@ -156,7 +156,8 @@ static int sprd_pwm_config(struct sprd_pwm_chip *spc, struct pwm_device *pwm,* given settings (MOD and input clock). */ mod = spc->mod[pwm->hwpwm]; - duty = duty_ns * mod / period_ns; + tmp = (u64 )duty_ns * mod; + duty =DIV_ROUND_CLOSEST_ULL (tmp, period_ns); tmp = (u64 )chn->clk_rate * period_ns;do_div (tmp,NSEC_PER_SEC );
Signature · 1 line
2.17.1
PATCH 4/6 dt-bindings: pwm: sprd: Convert to YAML
In reply to Wenhua LinConvert Spreadtrum PWM controller bindings to DT schema.
--- .../devicetree/bindings/pwm/pwm-sprd.txt | 40 -------- .../devicetree/bindings/pwm/pwm-sprd.yaml | 93 +++++++++++++++++++ 2 files changed, 93 insertions(+), 40 deletions(-) delete mode 100644 Documentation/devicetree/bindings/pwm/pwm-sprd.txt create mode 100644 Documentation/devicetree/bindings/pwm/pwm-sprd.yaml
Documentation/devicetree/bindings/pwm/pwm-sprd.txt deleted +0 -40
@@ -1,40 +0,0 @@ -Spreadtrum PWM controller - -Spreadtrum SoCs PWM controller provides 4 PWM channels. - -Required properties: -- compatible : Should be "sprd,ums512-pwm". -- reg: Physical base address and length of the controller's registers. -- clocks: The phandle and specifier referencing the controller's clocks. -- clock-names: Should contain following entries: - "pwmn": used to derive the functional clock for PWM channel n (n range: 0 ~ 3). - "enablen": for PWM channel n enable clock (n range: 0 ~ 3). -- #pwm-cells: Should be 2. See pwm.yaml in this directory for a description of - the cells format. - -Optional properties: -- assigned-clocks: Reference to the PWM clock entries. -- assigned-clock-parents: The phandle of the parent clock of PWM clock. - -Example: - pwms: pwm@32260000 { - compatible = "sprd,ums512-pwm"; - reg = <0 0x32260000 0 0x10000>; - clock-names = "pwm0", "enable0", - "pwm1", "enable1", - "pwm2", "enable2", - "pwm3", "enable3"; - clocks = <&aon_clk CLK_PWM0>, <&aonapb_gate CLK_PWM0_EB>, - <&aon_clk CLK_PWM1>, <&aonapb_gate CLK_PWM1_EB>, - <&aon_clk CLK_PWM2>, <&aonapb_gate CLK_PWM2_EB>, - <&aon_clk CLK_PWM3>, <&aonapb_gate CLK_PWM3_EB>; - assigned-clocks = <&aon_clk CLK_PWM0>, - <&aon_clk CLK_PWM1>, - <&aon_clk CLK_PWM2>, - <&aon_clk CLK_PWM3>; - assigned-clock-parents = <&ext_26m>, - <&ext_26m>, - <&ext_26m>, - <&ext_26m>; - #pwm-cells = <2>; - };
Documentation/devicetree/bindings/pwm/pwm-sprd.yaml new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 4/6] dt-bindings: pwm: sprd: Convert to YAML
Quoted 3 lines · Wenhua Lin
Convert Spreadtrum PWM controller bindings to DT schema.
Signed-off-by Wenhua Lin <[email protected]>
Please use scripts/get_maintainers.pl to get a list of necessary people and lists to CC. It might happen, that command when run on an older kernel, gives you outdated entries. Therefore please be sure you base your patches on recent Linux kernel.
Tools like b4 or scripts_getmaintainer.pl provide you proper list of people, so fix your workflow. Tools might also fail if you work on some ancient tree (don't, use mainline), work on fork of kernel (don't, use mainline) or you ignore some maintainers (really don't). Just use b4 and all the problems go away.
You missed at least devicetree list (maybe more), so this won't be tested by automated tooling. Performing review on untested code might be a waste of time, thus I will skip this patch entirely till you follow the process allowing the patch to be tested.
Please kindly resend and include all necessary To/Cc entries.
Best regards,
Krzysztof
PATCH 5/6 pwm: sprd: Add sprd,ums9620-pwm compatible
In reply to Wenhua LinAdd sprd,ums9620-pwm compatible string to binding document.
--- Documentation/devicetree/bindings/pwm/pwm-sprd.yaml | 1 + 1 file changed, 1 insertion(+)
Documentation/devicetree/bindings/pwm/pwm-sprd.yaml +1 -0
@@ -23,6 +23,7 @@ properties: items: - enum: - sprd,ums512-pwm + - sprd,ums9620-pwm reg: maxItems: 1
Signature · 1 line
2.17.1
Re: [PATCH 5/6] pwm: sprd: Add sprd,ums9620-pwm compatible
Quoted 3 lines · Wenhua Lin
Add sprd,ums9620-pwm compatible string to binding document.
Signed-off-by Wenhua Lin <[email protected]>
Please use scripts/get_maintainers.pl to get a list of necessary people and lists to CC. It might happen, that command when run on an older kernel, gives you outdated entries. Therefore please be sure you base your patches on recent Linux kernel.
Tools like b4 or scripts_getmaintainer.pl provide you proper list of people, so fix your workflow. Tools might also fail if you work on some ancient tree (don't, use mainline), work on fork of kernel (don't, use mainline) or you ignore some maintainers (really don't). Just use b4 and all the problems go away.
You missed at least devicetree list (maybe more), so this won't be tested by automated tooling. Performing review on untested code might be a waste of time, thus I will skip this patch entirely till you follow the process allowing the patch to be tested.
Please kindly resend and include all necessary To/Cc entries.
Best regards,
Krzysztof
PATCH 6/6 dt-bindings: pwm: sprd: Add sprd,mod attribute
In reply to Wenhua LinAdd sprd,mod attribute, which set the number of different duty cycles that PWM's waveform could output, to dts.
--- Documentation/devicetree/bindings/pwm/pwm-sprd.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+)
Documentation/devicetree/bindings/pwm/pwm-sprd.yaml +12 -0
@@ -55,6 +55,16 @@ properties: minItems: 4 maxItems: 4 + sprd,mod: + $ref: /schemas/types.yaml#/definitions/uint32-array + minItems: 4 + maxItems: 4 + items: + minimum: 0xFF + maximum: 0xFFF + description: | + The number of different duty cycles that could be set for PWM's waveform output. + "#pwm-cells": const: 2
@@ -63,6 +73,7 @@ required: - reg - clocks - clock-names + - sprd,mod additionalProperties: false
@@ -88,6 +99,7 @@ examples: <&ext_26m>, <&ext_26m>, <&ext_26m>; + sprd,mod = <0xFF 0x1FF 0x3FF 0xFFF>; #pwm-cells = <2>; };
Signature · 1 line
2.17.1
Re: [PATCH 6/6] dt-bindings: pwm: sprd: Add sprd,mod attribute
Quoted 3 lines · Wenhua Lin
Add sprd,mod attribute, which set the number of different duty cycles that PWM's waveform could output, to dts.
Please use scripts/get_maintainers.pl to get a list of necessary people and lists to CC. It might happen, that command when run on an older kernel, gives you outdated entries. Therefore please be sure you base your patches on recent Linux kernel.
Tools like b4 or scripts_getmaintainer.pl provide you proper list of people, so fix your workflow. Tools might also fail if you work on some ancient tree (don't, use mainline), work on fork of kernel (don't, use mainline) or you ignore some maintainers (really don't). Just use b4 and all the problems go away.
You missed at least devicetree list (maybe more), so this won't be tested by automated tooling. Performing review on untested code might be a waste of time, thus I will skip this patch entirely till you follow the process allowing the patch to be tested.
Please kindly resend and include all necessary To/Cc entries.
Best regards,
Krzysztof