Skip to content

Commit 0a86940

Browse files
committed
ACPI: battery: Convert the driver to a platform one
While binding drivers directly to struct acpi_device objects allows basic functionality to be provided, at least in the majority of cases, there are some problems with it, related to general consistency, sysfs layout, power management operation ordering, and code cleanliness. Overall, it is better to bind drivers to platform devices than to their ACPI companions, so convert the ACPI battery driver to a platform one. While this is not expected to alter functionality, it changes sysfs layout and so it will be visible to user space. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Link: https://patch.msgid.link/3187448.CbtlEUcBR6@rafael.j.wysocki
1 parent 0d17aaf commit 0a86940

1 file changed

Lines changed: 28 additions & 33 deletions

File tree

drivers/acpi/battery.c

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/list.h>
1818
#include <linux/module.h>
1919
#include <linux/mutex.h>
20+
#include <linux/platform_device.h>
2021
#include <linux/slab.h>
2122
#include <linux/suspend.h>
2223
#include <linux/types.h>
@@ -1208,26 +1209,26 @@ static void sysfs_battery_cleanup(struct acpi_battery *battery)
12081209
sysfs_remove_battery(battery);
12091210
}
12101211

1211-
static int acpi_battery_add(struct acpi_device *device)
1212+
static int acpi_battery_probe(struct platform_device *pdev)
12121213
{
1213-
int result = 0;
1214+
struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
12141215
struct acpi_battery *battery;
1215-
1216-
if (!device)
1217-
return -EINVAL;
1216+
int result;
12181217

12191218
if (device->dep_unmet)
12201219
return -EPROBE_DEFER;
12211220

1222-
battery = devm_kzalloc(&device->dev, sizeof(*battery), GFP_KERNEL);
1221+
battery = devm_kzalloc(&pdev->dev, sizeof(*battery), GFP_KERNEL);
12231222
if (!battery)
12241223
return -ENOMEM;
1224+
1225+
platform_set_drvdata(pdev, battery);
1226+
12251227
battery->device = device;
12261228
strscpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
12271229
strscpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1228-
device->driver_data = battery;
12291230

1230-
result = devm_mutex_init(&device->dev, &battery->update_lock);
1231+
result = devm_mutex_init(&pdev->dev, &battery->update_lock);
12311232
if (result)
12321233
return result;
12331234

@@ -1246,7 +1247,7 @@ static int acpi_battery_add(struct acpi_device *device)
12461247
if (result)
12471248
goto fail;
12481249

1249-
device_init_wakeup(&device->dev, 1);
1250+
device_init_wakeup(&pdev->dev, true);
12501251

12511252
result = acpi_dev_install_notify_handler(device, ACPI_ALL_NOTIFY,
12521253
acpi_battery_notify, battery);
@@ -1256,27 +1257,26 @@ static int acpi_battery_add(struct acpi_device *device)
12561257
return 0;
12571258

12581259
fail_pm:
1259-
device_init_wakeup(&device->dev, 0);
1260+
device_init_wakeup(&pdev->dev, false);
12601261
unregister_pm_notifier(&battery->pm_nb);
12611262
fail:
12621263
sysfs_battery_cleanup(battery);
12631264

12641265
return result;
12651266
}
12661267

1267-
static void acpi_battery_remove(struct acpi_device *device)
1268+
static void acpi_battery_remove(struct platform_device *pdev)
12681269
{
1269-
struct acpi_battery *battery;
1270+
struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
1271+
struct acpi_battery *battery = platform_get_drvdata(pdev);
12701272

1271-
if (!device || !acpi_driver_data(device))
1273+
if (!device || !battery)
12721274
return;
12731275

1274-
battery = acpi_driver_data(device);
1275-
12761276
acpi_dev_remove_notify_handler(device, ACPI_ALL_NOTIFY,
12771277
acpi_battery_notify);
12781278

1279-
device_init_wakeup(&device->dev, 0);
1279+
device_init_wakeup(&pdev->dev, false);
12801280
unregister_pm_notifier(&battery->pm_nb);
12811281

12821282
sysfs_battery_cleanup(battery);
@@ -1285,12 +1285,8 @@ static void acpi_battery_remove(struct acpi_device *device)
12851285
/* this is needed to learn about changes made in suspended state */
12861286
static int acpi_battery_resume(struct device *dev)
12871287
{
1288-
struct acpi_battery *battery;
1289-
1290-
if (!dev)
1291-
return -EINVAL;
1288+
struct acpi_battery *battery = dev_get_drvdata(dev);
12921289

1293-
battery = acpi_driver_data(to_acpi_device(dev));
12941290
if (!battery)
12951291
return -EINVAL;
12961292

@@ -1304,16 +1300,15 @@ static int acpi_battery_resume(struct device *dev)
13041300

13051301
static DEFINE_SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
13061302

1307-
static struct acpi_driver acpi_battery_driver = {
1308-
.name = "battery",
1309-
.class = ACPI_BATTERY_CLASS,
1310-
.ids = battery_device_ids,
1311-
.ops = {
1312-
.add = acpi_battery_add,
1313-
.remove = acpi_battery_remove,
1314-
},
1315-
.drv.pm = pm_sleep_ptr(&acpi_battery_pm),
1316-
.drv.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1303+
static struct platform_driver acpi_battery_driver = {
1304+
.probe = acpi_battery_probe,
1305+
.remove = acpi_battery_remove,
1306+
.driver = {
1307+
.name = "acpi-battery",
1308+
.acpi_match_table = battery_device_ids,
1309+
.pm = pm_sleep_ptr(&acpi_battery_pm),
1310+
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1311+
},
13171312
};
13181313

13191314
static int __init acpi_battery_init(void)
@@ -1323,12 +1318,12 @@ static int __init acpi_battery_init(void)
13231318

13241319
dmi_check_system(bat_dmi_table);
13251320

1326-
return acpi_bus_register_driver(&acpi_battery_driver);
1321+
return platform_driver_register(&acpi_battery_driver);
13271322
}
13281323

13291324
static void __exit acpi_battery_exit(void)
13301325
{
1331-
acpi_bus_unregister_driver(&acpi_battery_driver);
1326+
platform_driver_unregister(&acpi_battery_driver);
13321327
battery_hook_exit();
13331328
}
13341329

0 commit comments

Comments
 (0)