pwm: sti: Several improvements
cover pwm: sti: Several improvements
Hello,
while cleaning up my mailbox I stumbled over an old patch touching the sti pwm driver that didn't make it into the mainline. While forward porting it to todays kernel I noticed a few more patch opportunities. This series is the result.
Best regards
Uwe
Uwe Kleine-König (6):
pwm: sti: Simplify probe function using devm functions
pwm: sti: Improve error reporting using dev_err_probe()
pwm: sti: Drop member from driver data that only carries a constant
pwm: sti: Maintain all per-chip driver data in a single struct
pwm: sti: Use devm_kcalloc() instead of calculating the size for
devm_kzalloc()
pwm: sti: Prefer local variable over pointer dereference
drivers/pwm/pwm-sti.c | 161 ++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 107 deletions(-)
base-commit: 8ffc8b1bbd505e27e2c8439d326b6059c906c9dd
Signature · 1 line
2.43.0
PATCH 1/6 pwm: sti: Simplify probe function using devm functions
Instead of of_clk_get_by_name() use devm_clk_get_prepared() which has several advantages:
- Combines getting the clock and a call to clk_prepare(). The latter can be dropped from sti_pwm_probe() accordingly.
- Cares for calling clk_put() which is missing in both probe's error path and the remove function.
- Cares for calling clk_unprepare() which can be dropped from the error paths and the remove function. (Note that not all error path got this right.)
With additionally using devm_pwmchip_add() instead of pwmchip_add() the remove callback can be dropped completely. With it the last user of platform_get_drvdata() goes away and so platform_set_drvdata() can be dropped from the probe function, too.
--- drivers/pwm/pwm-sti.c | 39 +++------------------------------------ 1 file changed, 3 insertions(+), 36 deletions(-)
drivers/pwm/pwm-sti.c +3 -36
@@ -624,32 +624,20 @@ static int sti_pwm_probe(struct platform_device *pdev)return ret;if (cdata->pwm_num_devs) { - pc->pwm_clk =of_clk_get_by_name (dev->of_node,"pwm" ); + pc->pwm_clk =devm_clk_get_prepared (dev,"pwm" );if (IS_ERR (pc->pwm_clk)) {dev_err (dev,"failed to get PWM clock\n" );return PTR_ERR (pc->pwm_clk); } - - ret =clk_prepare (pc->pwm_clk); -if (ret) { -dev_err (dev,"failed to prepare clock\n" ); -return ret; - } }if (cdata->cpt_num_devs) { - pc->cpt_clk =of_clk_get_by_name (dev->of_node,"capture" ); + pc->cpt_clk =devm_clk_get_prepared (dev,"capture" );if (IS_ERR (pc->cpt_clk)) {dev_err (dev,"failed to get PWM capture clock\n" );return PTR_ERR (pc->cpt_clk); } - ret =clk_prepare (pc->cpt_clk); -if (ret) { -dev_err (dev,"failed to prepare clock\n" ); -return ret; - } - cdata->ddata =devm_kzalloc (dev, cdata->cpt_num_devs *sizeof (*cdata->ddata),GFP_KERNEL );if (!cdata->ddata)return -ENOMEM ;
@@ -664,27 +652,7 @@ static int sti_pwm_probe(struct platform_device *pdev)mutex_init (&ddata->lock); } - ret =pwmchip_add (chip); -if (ret <0 ) { -clk_unprepare (pc->pwm_clk); -clk_unprepare (pc->cpt_clk); -return ret; - } - -platform_set_drvdata (pdev, chip); - -return 0 ; -} - -static void sti_pwm_remove (struct platform_device *pdev) -{ -struct pwm_chip *chip =platform_get_drvdata (pdev); -struct sti_pwm_chip *pc =to_sti_pwmchip (chip); - -pwmchip_remove (chip); - -clk_unprepare (pc->pwm_clk); -clk_unprepare (pc->cpt_clk); +return devm_pwmchip_add (dev, chip); }static const struct of_device_id sti_pwm_of_match[] = {
@@ -699,7 +667,6 @@ static struct platform_driver sti_pwm_driver = { .of_match_table = sti_pwm_of_match, }, .probe = sti_pwm_probe, - .remove_new = sti_pwm_remove, };module_platform_driver (sti_pwm_driver);
Signature · 1 line
2.43.0
PATCH 2/6 pwm: sti: Improve error reporting using dev_err_probe()
In reply to Uwe Kleine-KönigThis has the advantage of handling EPROBE_DEFER correctly and being more compact.
This change also introduces an error message for a few error paths that lacked an error indicator before. Also sti_pwm_probe_dt() is renamed to sti_pwm_probe_regmap() to better fit what it actually does.
--- drivers/pwm/pwm-sti.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-)
drivers/pwm/pwm-sti.c +20 -21
@@ -502,7 +502,7 @@ static irqreturn_t sti_pwm_interrupt(int irq, void *data)return ret; } -static int sti_pwm_probe_dt (struct sti_pwm_chip *pc) +static int sti_pwm_probe_regmap (struct sti_pwm_chip *pc) {struct device *dev = pc->dev;const struct reg_field *reg_fields;
@@ -570,10 +570,8 @@ static int sti_pwm_probe(struct platform_device *pdev)if (!ret) cpt_num_devs = num_devs; -if (!pwm_num_devs && !cpt_num_devs) { -dev_err (dev,"No channels configured\n" ); -return -EINVAL ; - } +if (!pwm_num_devs && !cpt_num_devs) +return dev_err_probe (dev, -EINVAL ,"No channels configured\n" ); chip =devm_pwmchip_alloc (dev,max (pwm_num_devs, cpt_num_devs),sizeof (*pc));if (IS_ERR (chip))
@@ -591,7 +589,8 @@ static int sti_pwm_probe(struct platform_device *pdev) pc->regmap =devm_regmap_init_mmio (dev, pc->mmio, &sti_pwm_regmap_config);if (IS_ERR (pc->regmap)) -return PTR_ERR (pc->regmap); +return dev_err_probe (dev,PTR_ERR (pc->regmap), +"Failed to initialize regmap\n" ); irq =platform_get_irq (pdev,0 );if (irq <0 )
@@ -599,10 +598,8 @@ static int sti_pwm_probe(struct platform_device *pdev) ret =devm_request_irq (&pdev->dev, irq, sti_pwm_interrupt,0 , pdev->name, pc); -if (ret <0 ) { -dev_err (&pdev->dev,"Failed to request IRQ\n" ); -return ret; - } +if (ret <0 ) +dev_err_probe (&pdev->dev, ret,"Failed to request IRQ\n" );/* * Setup PWM data with default values: some values could be replaced
@@ -619,24 +616,22 @@ static int sti_pwm_probe(struct platform_device *pdev) pc->en_count =0 ;mutex_init (&pc->sti_pwm_lock); - ret =sti_pwm_probe_dt (pc); + ret =sti_pwm_probe_regmap (pc);if (ret) -return ret; +return dev_err_probe (dev, ret,"Failed to initialize regmap fields\n" );if (cdata->pwm_num_devs) { pc->pwm_clk =devm_clk_get_prepared (dev,"pwm" ); -if (IS_ERR (pc->pwm_clk)) { -dev_err (dev,"failed to get PWM clock\n" ); -return PTR_ERR (pc->pwm_clk); - } +if (IS_ERR (pc->pwm_clk)) +return dev_err_probe (dev,PTR_ERR (pc->pwm_clk), +"failed to get PWM clock\n" ); }if (cdata->cpt_num_devs) { pc->cpt_clk =devm_clk_get_prepared (dev,"capture" ); -if (IS_ERR (pc->cpt_clk)) { -dev_err (dev,"failed to get PWM capture clock\n" ); -return PTR_ERR (pc->cpt_clk); - } +if (IS_ERR (pc->cpt_clk)) +return dev_err_probe (dev,PTR_ERR (pc->cpt_clk), +"failed to get PWM capture clock\n" ); cdata->ddata =devm_kzalloc (dev, cdata->cpt_num_devs *sizeof (*cdata->ddata),GFP_KERNEL );if (!cdata->ddata)
@@ -652,7 +647,11 @@ static int sti_pwm_probe(struct platform_device *pdev)mutex_init (&ddata->lock); } -return devm_pwmchip_add (dev, chip); + ret =devm_pwmchip_add (dev, chip); +if (ret) +return dev_err_probe (dev, ret,"Failed to register pwm chip\n" ); + +return 0 ; }static const struct of_device_id sti_pwm_of_match[] = {
Signature · 1 line
2.43.0
PATCH 3/6 pwm: sti: Drop member from driver data that only carries a constant
In reply to Uwe Kleine-KönigThe .regfield member of struct sti_pwm_compat_data only holds a pointer to the global array sti_pwm_regfields. Replace the few usages by directly using this array and drop the member, saving a bit of memory and a few pointer dereferences.
--- drivers/pwm/pwm-sti.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/pwm/pwm-sti.c +6 -12
@@ -74,7 +74,6 @@ struct sti_cpt_ddata { };struct sti_pwm_compat_data { -const struct reg_field *reg_fields;unsigned int pwm_num_devs;unsigned int cpt_num_devs;unsigned int max_pwm_cnt;
@@ -505,38 +504,34 @@ static irqreturn_t sti_pwm_interrupt(int irq, void *data)static int sti_pwm_probe_regmap (struct sti_pwm_chip *pc) {struct device *dev = pc->dev; -const struct reg_field *reg_fields; -struct sti_pwm_compat_data *cdata = pc->cdata; - - reg_fields = cdata->reg_fields; pc->prescale_low =devm_regmap_field_alloc (dev, pc->regmap, - reg_fields[PWMCLK_PRESCALE_LOW ]); + sti_pwm_regfields[PWMCLK_PRESCALE_LOW ]);if (IS_ERR (pc->prescale_low))return PTR_ERR (pc->prescale_low); pc->prescale_high =devm_regmap_field_alloc (dev, pc->regmap, - reg_fields[PWMCLK_PRESCALE_HIGH ]); + sti_pwm_regfields[PWMCLK_PRESCALE_HIGH ]);if (IS_ERR (pc->prescale_high))return PTR_ERR (pc->prescale_high); pc->pwm_out_en =devm_regmap_field_alloc (dev, pc->regmap, - reg_fields[PWM_OUT_EN ]); + sti_pwm_regfields[PWM_OUT_EN ]);if (IS_ERR (pc->pwm_out_en))return PTR_ERR (pc->pwm_out_en); pc->pwm_cpt_en =devm_regmap_field_alloc (dev, pc->regmap, - reg_fields[PWM_CPT_EN ]); + sti_pwm_regfields[PWM_CPT_EN ]);if (IS_ERR (pc->pwm_cpt_en))return PTR_ERR (pc->pwm_cpt_en); pc->pwm_cpt_int_en =devm_regmap_field_alloc (dev, pc->regmap, - reg_fields[PWM_CPT_INT_EN ]); + sti_pwm_regfields[PWM_CPT_INT_EN ]);if (IS_ERR (pc->pwm_cpt_int_en))return PTR_ERR (pc->pwm_cpt_int_en); pc->pwm_cpt_int_stat =devm_regmap_field_alloc (dev, pc->regmap, - reg_fields[PWM_CPT_INT_STAT ]); + sti_pwm_regfields[PWM_CPT_INT_STAT ]);if (PTR_ERR_OR_ZERO (pc->pwm_cpt_int_stat))return PTR_ERR (pc->pwm_cpt_int_stat);
@@ -605,7 +600,6 @@ static int sti_pwm_probe(struct platform_device *pdev)* Setup PWM data with default values: some values could be replaced * with specific ones provided from Device Tree. */ - cdata->reg_fields = sti_pwm_regfields; cdata->max_prescale =0xff ; cdata->max_pwm_cnt =255 ; cdata->pwm_num_devs = pwm_num_devs;
Signature · 1 line
2.43.0
PATCH 4/6 pwm: sti: Maintain all per-chip driver data in a single struct
In reply to Uwe Kleine-KönigInstead of (arbitrarily?) spliting the per-chip driver data into two structures, put everything into struct sti_pwm_chip. This reduces memory management overhead and a few pointer indirections.
--- drivers/pwm/pwm-sti.c | 58 ++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 36 deletions(-)
drivers/pwm/pwm-sti.c +22 -36
@@ -73,20 +73,16 @@ struct sti_cpt_ddata {wait_queue_head_t wait; }; -struct sti_pwm_compat_data { -unsigned int pwm_num_devs; -unsigned int cpt_num_devs; -unsigned int max_pwm_cnt; -unsigned int max_prescale; -struct sti_cpt_ddata *ddata; -}; -struct sti_pwm_chip {struct device *dev;struct clk *pwm_clk;struct clk *cpt_clk;struct regmap *regmap; -struct sti_pwm_compat_data *cdata; +unsigned int pwm_num_devs; +unsigned int cpt_num_devs; +unsigned int max_pwm_cnt; +unsigned int max_prescale; +struct sti_cpt_ddata *ddata;struct regmap_field *prescale_low;struct regmap_field *prescale_high;struct regmap_field *pwm_out_en;
@@ -121,7 +117,6 @@ static inline struct sti_pwm_chip *to_sti_pwmchip(struct pwm_chip *chip)static int sti_pwm_get_prescale (struct sti_pwm_chip *pc,unsigned long period,unsigned int *prescale) { -struct sti_pwm_compat_data *cdata = pc->cdata;unsigned long clk_rate;unsigned long value;unsigned int ps;
@@ -136,13 +131,13 @@ static int sti_pwm_get_prescale(struct sti_pwm_chip *pc, unsigned long period,* prescale = ((period_ns * clk_rate) / (10^9 * (max_pwm_cnt + 1)) - 1 */ value =NSEC_PER_SEC / clk_rate; - value *= cdata->max_pwm_cnt +1 ; + value *= pc->max_pwm_cnt +1 ;if (period % value)return -EINVAL ; ps = period / value -1 ; -if (ps > cdata->max_prescale) +if (ps > pc->max_prescale)return -EINVAL ; *prescale = ps;
@@ -163,7 +158,6 @@ static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, int duty_ns, int period_ns) {struct sti_pwm_chip *pc =to_sti_pwmchip (chip); -struct sti_pwm_compat_data *cdata = pc->cdata;unsigned int ncfg, value, prescale =0 ;struct pwm_device *cur = pc->cur;struct device *dev = pc->dev;
@@ -223,7 +217,7 @@ static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,* PWM pulse = (max_pwm_count + 1) local cycles, * that is continuous pulse: signal never goes low. */ - value = cdata->max_pwm_cnt * duty_ns / period_ns; + value = pc->max_pwm_cnt * duty_ns / period_ns; ret =regmap_write (pc->regmap,PWM_OUT_VAL (pwm->hwpwm), value);if (ret)
@@ -312,14 +306,13 @@ static int sti_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_capture *result, unsigned long timeout) {struct sti_pwm_chip *pc =to_sti_pwmchip (chip); -struct sti_pwm_compat_data *cdata = pc->cdata; -struct sti_cpt_ddata *ddata = &cdata->ddata[pwm->hwpwm]; +struct sti_cpt_ddata *ddata = &pc->ddata[pwm->hwpwm];struct device *dev = pc->dev;unsigned int effective_ticks;unsigned long long high, low;int ret; -if (pwm->hwpwm >= cdata->cpt_num_devs) { +if (pwm->hwpwm >= pc->cpt_num_devs) {dev_err (dev,"device %u is not valid\n" , pwm->hwpwm);return -EINVAL ; }
@@ -394,11 +387,10 @@ static int sti_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state) {struct sti_pwm_chip *pc =to_sti_pwmchip (chip); -struct sti_pwm_compat_data *cdata = pc->cdata;struct device *dev = pc->dev;int err; -if (pwm->hwpwm >= cdata->pwm_num_devs) { +if (pwm->hwpwm >= pc->pwm_num_devs) {dev_err (dev,"device %u is not valid for pwm mode\n" , pwm->hwpwm);return -EINVAL ;
@@ -447,7 +439,7 @@ static irqreturn_t sti_pwm_interrupt(int irq, void *data)while (cpt_int_stat) { devicenum =ffs (cpt_int_stat) -1 ; - ddata = &pc->cdata->ddata[devicenum]; + ddata = &pc->ddata[devicenum];/* * Capture input:
@@ -551,7 +543,6 @@ static int sti_pwm_probe(struct platform_device *pdev)u32 num_devs;unsigned int pwm_num_devs =0 ;unsigned int cpt_num_devs =0 ; -struct sti_pwm_compat_data *cdata;struct pwm_chip *chip;struct sti_pwm_chip *pc;unsigned int i;
@@ -573,10 +564,6 @@ static int sti_pwm_probe(struct platform_device *pdev)return PTR_ERR (chip); pc =to_sti_pwmchip (chip); - cdata =devm_kzalloc (dev,sizeof (*cdata),GFP_KERNEL ); -if (!cdata) -return -ENOMEM ; - pc->mmio =devm_platform_ioremap_resource (pdev,0 );if (IS_ERR (pc->mmio))return PTR_ERR (pc->mmio);
@@ -600,12 +587,11 @@ static int sti_pwm_probe(struct platform_device *pdev)* Setup PWM data with default values: some values could be replaced * with specific ones provided from Device Tree. */ - cdata->max_prescale =0xff ; - cdata->max_pwm_cnt =255 ; - cdata->pwm_num_devs = pwm_num_devs; - cdata->cpt_num_devs = cpt_num_devs; + pc->max_prescale =0xff ; + pc->max_pwm_cnt =255 ; + pc->pwm_num_devs = pwm_num_devs; + pc->cpt_num_devs = cpt_num_devs; - pc->cdata = cdata; pc->dev = dev; pc->en_count =0 ;mutex_init (&pc->sti_pwm_lock);
@@ -614,28 +600,28 @@ static int sti_pwm_probe(struct platform_device *pdev)if (ret)return dev_err_probe (dev, ret,"Failed to initialize regmap fields\n" ); -if (cdata->pwm_num_devs) { +if (pc->pwm_num_devs) { pc->pwm_clk =devm_clk_get_prepared (dev,"pwm" );if (IS_ERR (pc->pwm_clk))return dev_err_probe (dev,PTR_ERR (pc->pwm_clk),"failed to get PWM clock\n" ); } -if (cdata->cpt_num_devs) { +if (pc->cpt_num_devs) { pc->cpt_clk =devm_clk_get_prepared (dev,"capture" );if (IS_ERR (pc->cpt_clk))return dev_err_probe (dev,PTR_ERR (pc->cpt_clk),"failed to get PWM capture clock\n" ); - cdata->ddata =devm_kzalloc (dev, cdata->cpt_num_devs *sizeof (*cdata->ddata),GFP_KERNEL ); -if (!cdata->ddata) + pc->ddata =devm_kzalloc (dev, pc->cpt_num_devs *sizeof (*pc->ddata),GFP_KERNEL ); +if (!pc->ddata)return -ENOMEM ; } chip->ops = &sti_pwm_ops; -for (i =0 ; i < cdata->cpt_num_devs; i++) { -struct sti_cpt_ddata *ddata = &cdata->ddata[i]; +for (i =0 ; i < pc->cpt_num_devs; i++) { +struct sti_cpt_ddata *ddata = &pc->ddata[i];init_waitqueue_head (&ddata->wait);mutex_init (&ddata->lock);
Signature · 1 line
2.43.0
PATCH 5/6 pwm: sti: Use devm_kcalloc() instead of calculating the size for devm_kzalloc()
In reply to Uwe Kleine-KönigUsing a multiplication to calculate the size of an allocation isn't recommended in case the mulitplication overflows. While the chance this happens is low, preventing such an error is easy enough; so do that.
--- drivers/pwm/pwm-sti.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pwm/pwm-sti.c +2 -1
@@ -613,7 +613,8 @@ static int sti_pwm_probe(struct platform_device *pdev)return dev_err_probe (dev,PTR_ERR (pc->cpt_clk),"failed to get PWM capture clock\n" ); - pc->ddata =devm_kzalloc (dev, pc->cpt_num_devs *sizeof (*pc->ddata),GFP_KERNEL ); + pc->ddata =devm_kcalloc (dev, pc->cpt_num_devs, +sizeof (*pc->ddata),GFP_KERNEL );if (!pc->ddata)return -ENOMEM ; }
Signature · 1 line
2.43.0
PATCH 6/6 pwm: sti: Prefer local variable over pointer dereference
In reply to Uwe Kleine-KönigWhile the compiler probably optimizes out the pointer dereference, using the local variable holding the same value also benefits the human reader. So simplify accordingly. Also move a loop over all capture lines into the capture if block to group the operations related to capturing in .probe().
--- drivers/pwm/pwm-sti.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/pwm/pwm-sti.c +10 -10
@@ -600,34 +600,34 @@ static int sti_pwm_probe(struct platform_device *pdev)if (ret)return dev_err_probe (dev, ret,"Failed to initialize regmap fields\n" ); -if (pc->pwm_num_devs) { +if (pwm_num_devs) { pc->pwm_clk =devm_clk_get_prepared (dev,"pwm" );if (IS_ERR (pc->pwm_clk))return dev_err_probe (dev,PTR_ERR (pc->pwm_clk),"failed to get PWM clock\n" ); } -if (pc->cpt_num_devs) { +if (cpt_num_devs) { pc->cpt_clk =devm_clk_get_prepared (dev,"capture" );if (IS_ERR (pc->cpt_clk))return dev_err_probe (dev,PTR_ERR (pc->cpt_clk),"failed to get PWM capture clock\n" ); - pc->ddata =devm_kcalloc (dev, pc->cpt_num_devs, + pc->ddata =devm_kcalloc (dev, cpt_num_devs,sizeof (*pc->ddata),GFP_KERNEL );if (!pc->ddata)return -ENOMEM ; + +for (i =0 ; i < cpt_num_devs; i++) { +struct sti_cpt_ddata *ddata = &pc->ddata[i]; + +init_waitqueue_head (&ddata->wait); +mutex_init (&ddata->lock); + } } chip->ops = &sti_pwm_ops; -for (i =0 ; i < pc->cpt_num_devs; i++) { -struct sti_cpt_ddata *ddata = &pc->ddata[i]; - -init_waitqueue_head (&ddata->wait); -mutex_init (&ddata->lock); - } - ret =devm_pwmchip_add (dev, chip);if (ret)return dev_err_probe (dev, ret,"Failed to register pwm chip\n" );
Signature · 1 line
2.43.0
Hello,
Quoted 11 lines · Uwe Kleine-König
Uwe Kleine-König (6):
pwm: sti: Simplify probe function using devm functions
pwm: sti: Improve error reporting usingdev_err_probe()
pwm: sti: Drop member from driver data that only carries a constant
pwm: sti: Maintain all per-chip driver data in a single struct
pwm: sti: Usedevm_kcalloc()instead of calculating the size for
devm_kzalloc()
pwm: sti: Prefer local variable over pointer dereferencedrivers/pwm/pwm-sti.c | 161 ++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 107 deletions(-)
I applied this to my for-nexxt branch. Don't rely on the commit sha, the branch will see a rebase on (at least) v6.9-rc1. I'll also continue to address feedback (be it a request for changes or review tags).
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |