Skip to content

Commit

Permalink
counter: stm32-timer-cnt: Convert to new counter registration
Browse files Browse the repository at this point in the history
This fixes device lifetime issues where it was possible to free a live
struct device.

Fixes: ad29937 ("counter: Add STM32 Timer quadrature encoder")
Reviewed-by: Jonathan Cameron <[email protected]>
Acked-by: William Breathitt Gray <[email protected]>
Signed-off-by: Uwe Kleine-König <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
Uwe Kleine-König authored and gregkh committed Dec 30, 2021
1 parent 5998ea6 commit e1717d2
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions drivers/counter/stm32-timer-cnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ struct stm32_timer_regs {
};

struct stm32_timer_cnt {
struct counter_device counter;
struct regmap *regmap;
struct clk *clk;
u32 max_arr;
Expand Down Expand Up @@ -317,31 +316,38 @@ static int stm32_timer_cnt_probe(struct platform_device *pdev)
struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
struct device *dev = &pdev->dev;
struct stm32_timer_cnt *priv;
struct counter_device *counter;
int ret;

if (IS_ERR_OR_NULL(ddata))
return -EINVAL;

priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
counter = devm_counter_alloc(dev, sizeof(*priv));
if (!counter)
return -ENOMEM;

priv = counter_priv(counter);

priv->regmap = ddata->regmap;
priv->clk = ddata->clk;
priv->max_arr = ddata->max_arr;

priv->counter.name = dev_name(dev);
priv->counter.parent = dev;
priv->counter.ops = &stm32_timer_cnt_ops;
priv->counter.counts = &stm32_counts;
priv->counter.num_counts = 1;
priv->counter.signals = stm32_signals;
priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
priv->counter.priv = priv;
counter->name = dev_name(dev);
counter->parent = dev;
counter->ops = &stm32_timer_cnt_ops;
counter->counts = &stm32_counts;
counter->num_counts = 1;
counter->signals = stm32_signals;
counter->num_signals = ARRAY_SIZE(stm32_signals);

platform_set_drvdata(pdev, priv);

/* Register Counter device */
return devm_counter_register(dev, &priv->counter);
ret = devm_counter_add(dev, counter);
if (ret < 0)
dev_err_probe(dev, ret, "Failed to add counter\n");

return ret;
}

static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
Expand Down

0 comments on commit e1717d2

Please sign in to comment.