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

power: supply: ds2782: Use devm_delayed_work_autocancel() helper

Use the device lifecycle managed work init function. This helps prevent
mistakes like canceling out of order in cleanup functions and
forgetting to canceling on error paths.

Note we move this to after the registering the power supply so that
the cancel is called before unregistering.

This was the last thing the .remove() function did, so remove that too.

Signed-off-by: Andrew Davis <afd@ti.com>
Link: https://lore.kernel.org/r/20241202211519.199635-5-afd@ti.com
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>

authored by

Andrew Davis and committed by
Sebastian Reichel
1c448329 8571178e

+6 -12
+6 -12
drivers/power/supply/ds2782_battery.c
··· 11 11 * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru> 12 12 */ 13 13 14 + #include <linux/devm-helpers.h> 14 15 #include <linux/kernel.h> 15 16 #include <linux/module.h> 16 17 #include <linux/types.h> ··· 311 310 battery->external_power_changed = NULL; 312 311 } 313 312 314 - static void ds278x_battery_remove(struct i2c_client *client) 315 - { 316 - struct ds278x_info *info = i2c_get_clientdata(client); 317 - 318 - cancel_delayed_work_sync(&info->bat_work); 319 - } 320 - 321 313 #ifdef CONFIG_PM_SLEEP 322 314 323 315 static int ds278x_suspend(struct device *dev) ··· 406 412 info->capacity = 100; 407 413 info->status = POWER_SUPPLY_STATUS_FULL; 408 414 409 - INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work); 410 - 411 415 info->battery = devm_power_supply_register(&client->dev, 412 416 &info->battery_desc, 413 417 &psy_cfg); 414 418 if (IS_ERR(info->battery)) { 415 419 dev_err(&client->dev, "failed to register battery\n"); 416 420 return PTR_ERR(info->battery); 417 - } else { 418 - schedule_delayed_work(&info->bat_work, DS278x_DELAY); 419 421 } 422 + 423 + ret = devm_delayed_work_autocancel(&client->dev, &info->bat_work, ds278x_bat_work); 424 + if (ret) 425 + return ret; 426 + schedule_delayed_work(&info->bat_work, DS278x_DELAY); 420 427 421 428 return 0; 422 429 } ··· 435 440 .pm = &ds278x_battery_pm_ops, 436 441 }, 437 442 .probe = ds278x_battery_probe, 438 - .remove = ds278x_battery_remove, 439 443 .id_table = ds278x_id, 440 444 }; 441 445 module_i2c_driver(ds278x_battery_driver);