Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

counter: stm32-lptimer-cnt: Convert to new counter registration

This fixes device lifetime issues where it was possible to free a live
struct device.

Fixes: 597f55e3f36c ("counter: stm32-lptimer: add counter device")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Link: https://lore.kernel.org/r/20211230150300.72196-22-u.kleine-koenig@pengutronix.de
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Uwe Kleine-König and committed by
Greg Kroah-Hartman
e75d678d e1717d2e

+19 -14
+19 -14
drivers/counter/stm32-lptimer-cnt.c
··· 20 20 #include <linux/types.h> 21 21 22 22 struct stm32_lptim_cnt { 23 - struct counter_device counter; 24 23 struct device *dev; 25 24 struct regmap *regmap; 26 25 struct clk *clk; ··· 410 411 static int stm32_lptim_cnt_probe(struct platform_device *pdev) 411 412 { 412 413 struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent); 414 + struct counter_device *counter; 413 415 struct stm32_lptim_cnt *priv; 416 + int ret; 414 417 415 418 if (IS_ERR_OR_NULL(ddata)) 416 419 return -EINVAL; 417 420 418 - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 419 - if (!priv) 421 + counter = devm_counter_alloc(&pdev->dev, sizeof(*priv)); 422 + if (!counter) 420 423 return -ENOMEM; 424 + priv = counter_priv(counter); 421 425 422 426 priv->dev = &pdev->dev; 423 427 priv->regmap = ddata->regmap; ··· 428 426 priv->ceiling = STM32_LPTIM_MAX_ARR; 429 427 430 428 /* Initialize Counter device */ 431 - priv->counter.name = dev_name(&pdev->dev); 432 - priv->counter.parent = &pdev->dev; 433 - priv->counter.ops = &stm32_lptim_cnt_ops; 429 + counter->name = dev_name(&pdev->dev); 430 + counter->parent = &pdev->dev; 431 + counter->ops = &stm32_lptim_cnt_ops; 434 432 if (ddata->has_encoder) { 435 - priv->counter.counts = &stm32_lptim_enc_counts; 436 - priv->counter.num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals); 433 + counter->counts = &stm32_lptim_enc_counts; 434 + counter->num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals); 437 435 } else { 438 - priv->counter.counts = &stm32_lptim_in1_counts; 439 - priv->counter.num_signals = 1; 436 + counter->counts = &stm32_lptim_in1_counts; 437 + counter->num_signals = 1; 440 438 } 441 - priv->counter.num_counts = 1; 442 - priv->counter.signals = stm32_lptim_cnt_signals; 443 - priv->counter.priv = priv; 439 + counter->num_counts = 1; 440 + counter->signals = stm32_lptim_cnt_signals; 444 441 445 442 platform_set_drvdata(pdev, priv); 446 443 447 - return devm_counter_register(&pdev->dev, &priv->counter); 444 + ret = devm_counter_add(&pdev->dev, counter); 445 + if (ret < 0) 446 + return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n"); 447 + 448 + return 0; 448 449 } 449 450 450 451 #ifdef CONFIG_PM_SLEEP