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

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

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

Fixes: ad29937e206f ("counter: Add STM32 Timer quadrature encoder")
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-21-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
e1717d2e 5998ea62

+18 -12
+18 -12
drivers/counter/stm32-timer-cnt.c
··· 29 29 }; 30 30 31 31 struct stm32_timer_cnt { 32 - struct counter_device counter; 33 32 struct regmap *regmap; 34 33 struct clk *clk; 35 34 u32 max_arr; ··· 316 317 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); 317 318 struct device *dev = &pdev->dev; 318 319 struct stm32_timer_cnt *priv; 320 + struct counter_device *counter; 321 + int ret; 319 322 320 323 if (IS_ERR_OR_NULL(ddata)) 321 324 return -EINVAL; 322 325 323 - priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 324 - if (!priv) 326 + counter = devm_counter_alloc(dev, sizeof(*priv)); 327 + if (!counter) 325 328 return -ENOMEM; 329 + 330 + priv = counter_priv(counter); 326 331 327 332 priv->regmap = ddata->regmap; 328 333 priv->clk = ddata->clk; 329 334 priv->max_arr = ddata->max_arr; 330 335 331 - priv->counter.name = dev_name(dev); 332 - priv->counter.parent = dev; 333 - priv->counter.ops = &stm32_timer_cnt_ops; 334 - priv->counter.counts = &stm32_counts; 335 - priv->counter.num_counts = 1; 336 - priv->counter.signals = stm32_signals; 337 - priv->counter.num_signals = ARRAY_SIZE(stm32_signals); 338 - priv->counter.priv = priv; 336 + counter->name = dev_name(dev); 337 + counter->parent = dev; 338 + counter->ops = &stm32_timer_cnt_ops; 339 + counter->counts = &stm32_counts; 340 + counter->num_counts = 1; 341 + counter->signals = stm32_signals; 342 + counter->num_signals = ARRAY_SIZE(stm32_signals); 339 343 340 344 platform_set_drvdata(pdev, priv); 341 345 342 346 /* Register Counter device */ 343 - return devm_counter_register(dev, &priv->counter); 347 + ret = devm_counter_add(dev, counter); 348 + if (ret < 0) 349 + dev_err_probe(dev, ret, "Failed to add counter\n"); 350 + 351 + return ret; 344 352 } 345 353 346 354 static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)