1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * acpi_ac.c - ACPI AC Adapter Driver (Revision: 27) 4 * 5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 7 */ 8 9 #define pr_fmt(fmt) "ACPI: AC: " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/slab.h> 14 #include <linux/init.h> 15 #include <linux/types.h> 16 #include <linux/dmi.h> 17 #include <linux/delay.h> 18 #include <linux/platform_device.h> 19 #include <linux/power_supply.h> 20 #include <linux/string_choices.h> 21 #include <linux/acpi.h> 22 #include <acpi/battery.h> 23 24 #define ACPI_AC_FILE_STATE "state" 25 #define ACPI_AC_NOTIFY_STATUS 0x80 26 #define ACPI_AC_STATUS_OFFLINE 0x00 27 #define ACPI_AC_STATUS_ONLINE 0x01 28 #define ACPI_AC_STATUS_UNKNOWN 0xFF 29 30 MODULE_AUTHOR("Paul Diefenbaugh"); 31 MODULE_DESCRIPTION("ACPI AC Adapter Driver"); 32 MODULE_LICENSE("GPL"); 33 34 static const struct acpi_device_id ac_device_ids[] = { 35 {"ACPI0003", 0}, 36 {"", 0}, 37 }; 38 MODULE_DEVICE_TABLE(acpi, ac_device_ids); 39 40 static int ac_sleep_before_get_state_ms; 41 static int ac_only; 42 43 struct acpi_ac { 44 struct power_supply *charger; 45 struct power_supply_desc charger_desc; 46 struct acpi_device *device; 47 unsigned long long state; 48 struct notifier_block battery_nb; 49 }; 50 51 #define to_acpi_ac(x) power_supply_get_drvdata(x) 52 53 /* AC Adapter Management */ 54 static int acpi_ac_get_state(struct acpi_ac *ac) 55 { 56 acpi_status status = AE_OK; 57 58 if (!ac) 59 return -EINVAL; 60 61 if (ac_only) { 62 ac->state = 1; 63 return 0; 64 } 65 66 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, 67 &ac->state); 68 if (ACPI_FAILURE(status)) { 69 acpi_handle_info(ac->device->handle, 70 "Error reading AC Adapter state: %s\n", 71 acpi_format_exception(status)); 72 ac->state = ACPI_AC_STATUS_UNKNOWN; 73 return -ENODEV; 74 } 75 76 return 0; 77 } 78 79 /* sysfs I/F */ 80 static int get_ac_property(struct power_supply *psy, 81 enum power_supply_property psp, 82 union power_supply_propval *val) 83 { 84 struct acpi_ac *ac = to_acpi_ac(psy); 85 86 if (!ac) 87 return -ENODEV; 88 89 if (acpi_ac_get_state(ac)) 90 return -ENODEV; 91 92 switch (psp) { 93 case POWER_SUPPLY_PROP_ONLINE: 94 val->intval = ac->state; 95 break; 96 default: 97 return -EINVAL; 98 } 99 100 return 0; 101 } 102 103 static const enum power_supply_property ac_props[] = { 104 POWER_SUPPLY_PROP_ONLINE, 105 }; 106 107 /* Driver Model */ 108 static void acpi_ac_notify(acpi_handle handle, u32 event, void *data) 109 { 110 struct acpi_ac *ac = data; 111 struct acpi_device *adev = ac->device; 112 113 switch (event) { 114 default: 115 acpi_handle_debug(adev->handle, "Unsupported event [0x%x]\n", 116 event); 117 fallthrough; 118 case ACPI_AC_NOTIFY_STATUS: 119 case ACPI_NOTIFY_BUS_CHECK: 120 case ACPI_NOTIFY_DEVICE_CHECK: 121 /* 122 * A buggy BIOS may notify AC first and then sleep for 123 * a specific time before doing actual operations in the 124 * EC event handler (_Qxx). This will cause the AC state 125 * reported by the ACPI event to be incorrect, so wait for a 126 * specific time for the EC event handler to make progress. 127 */ 128 if (ac_sleep_before_get_state_ms > 0) 129 msleep(ac_sleep_before_get_state_ms); 130 131 acpi_ac_get_state(ac); 132 acpi_bus_generate_netlink_event(ACPI_AC_CLASS, 133 dev_name(&adev->dev), event, 134 ac->state); 135 acpi_notifier_call_chain(ACPI_AC_CLASS, acpi_device_bid(adev), 136 event, ac->state); 137 power_supply_changed(ac->charger); 138 } 139 } 140 141 static int acpi_ac_battery_notify(struct notifier_block *nb, 142 unsigned long action, void *data) 143 { 144 struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb); 145 struct acpi_bus_event *event = (struct acpi_bus_event *)data; 146 147 /* 148 * On HP Pavilion dv6-6179er AC status notifications aren't triggered 149 * when adapter is plugged/unplugged. However, battery status 150 * notifications are triggered when battery starts charging or 151 * discharging. Re-reading AC status triggers lost AC notifications, 152 * if AC status has changed. 153 */ 154 if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 && 155 event->type == ACPI_BATTERY_NOTIFY_STATUS) 156 acpi_ac_get_state(ac); 157 158 return NOTIFY_OK; 159 } 160 161 static int __init thinkpad_e530_quirk(const struct dmi_system_id *d) 162 { 163 ac_sleep_before_get_state_ms = 1000; 164 return 0; 165 } 166 167 static int __init ac_only_quirk(const struct dmi_system_id *d) 168 { 169 ac_only = 1; 170 return 0; 171 } 172 173 /* Please keep this list alphabetically sorted */ 174 static const struct dmi_system_id ac_dmi_table[] __initconst = { 175 { 176 /* Kodlix GK45 returning incorrect state */ 177 .callback = ac_only_quirk, 178 .matches = { 179 DMI_MATCH(DMI_PRODUCT_NAME, "GK45"), 180 }, 181 }, 182 { 183 /* Lenovo Thinkpad e530, see comment in acpi_ac_notify() */ 184 .callback = thinkpad_e530_quirk, 185 .matches = { 186 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 187 DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"), 188 }, 189 }, 190 {}, 191 }; 192 193 static int acpi_ac_probe(struct platform_device *pdev) 194 { 195 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); 196 struct power_supply_config psy_cfg = {}; 197 struct acpi_ac *ac; 198 int result; 199 200 ac = kzalloc_obj(struct acpi_ac); 201 if (!ac) 202 return -ENOMEM; 203 204 ac->device = adev; 205 206 platform_set_drvdata(pdev, ac); 207 208 result = acpi_ac_get_state(ac); 209 if (result) 210 goto err_release_ac; 211 212 psy_cfg.drv_data = ac; 213 214 ac->charger_desc.name = acpi_device_bid(adev); 215 ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS; 216 ac->charger_desc.properties = ac_props; 217 ac->charger_desc.num_properties = ARRAY_SIZE(ac_props); 218 ac->charger_desc.get_property = get_ac_property; 219 ac->charger = power_supply_register(&pdev->dev, 220 &ac->charger_desc, &psy_cfg); 221 if (IS_ERR(ac->charger)) { 222 result = PTR_ERR(ac->charger); 223 goto err_release_ac; 224 } 225 226 pr_info("AC Adapter [%s] (%s-line)\n", acpi_device_bid(adev), 227 str_on_off(ac->state)); 228 229 ac->battery_nb.notifier_call = acpi_ac_battery_notify; 230 register_acpi_notifier(&ac->battery_nb); 231 232 result = acpi_dev_install_notify_handler(adev, ACPI_ALL_NOTIFY, 233 acpi_ac_notify, ac); 234 if (result) 235 goto err_unregister; 236 237 return 0; 238 239 err_unregister: 240 power_supply_unregister(ac->charger); 241 unregister_acpi_notifier(&ac->battery_nb); 242 err_release_ac: 243 kfree(ac); 244 245 return result; 246 } 247 248 #ifdef CONFIG_PM_SLEEP 249 static int acpi_ac_resume(struct device *dev) 250 { 251 struct acpi_ac *ac = dev_get_drvdata(dev); 252 unsigned int old_state; 253 254 old_state = ac->state; 255 if (acpi_ac_get_state(ac)) 256 return 0; 257 if (old_state != ac->state) 258 power_supply_changed(ac->charger); 259 260 return 0; 261 } 262 #endif 263 264 static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume); 265 266 static void acpi_ac_remove(struct platform_device *pdev) 267 { 268 struct acpi_ac *ac = platform_get_drvdata(pdev); 269 270 acpi_dev_remove_notify_handler(ac->device, ACPI_ALL_NOTIFY, 271 acpi_ac_notify); 272 power_supply_unregister(ac->charger); 273 unregister_acpi_notifier(&ac->battery_nb); 274 275 kfree(ac); 276 } 277 278 static struct platform_driver acpi_ac_driver = { 279 .probe = acpi_ac_probe, 280 .remove = acpi_ac_remove, 281 .driver = { 282 .name = "ac", 283 .acpi_match_table = ac_device_ids, 284 .pm = &acpi_ac_pm, 285 }, 286 }; 287 288 static int __init acpi_ac_init(void) 289 { 290 int result; 291 292 if (acpi_disabled) 293 return -ENODEV; 294 295 if (acpi_quirk_skip_acpi_ac_and_battery()) 296 return -ENODEV; 297 298 dmi_check_system(ac_dmi_table); 299 300 result = platform_driver_register(&acpi_ac_driver); 301 if (result < 0) 302 return -ENODEV; 303 304 return 0; 305 } 306 307 static void __exit acpi_ac_exit(void) 308 { 309 platform_driver_unregister(&acpi_ac_driver); 310 } 311 module_init(acpi_ac_init); 312 module_exit(acpi_ac_exit); 313