| 1 | |
| 2 | |
| 3 | * acpi_ac.c - ACPI AC Adapter Driver (Revision: 27)
|
| 4 | *
|
| 5 | * Copyright (C) 2001, 2002 Andy Grover <[email protected]>
|
| 6 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 174 | static const struct dmi_system_id ac_dmi_table[] __initconst = {
|
| 175 | {
|
| 176 |
|
| 177 | .callback = ac_only_quirk,
|
| 178 | .matches = {
|
| 179 | DMI_MATCH(DMI_PRODUCT_NAME, "GK45"),
|
| 180 | },
|
| 181 | },
|
| 182 | {
|
| 183 |
|
| 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 power_supply_config psy_cfg = {};
|
| 196 | struct device *dev = &pdev->dev;
|
| 197 | struct acpi_device *adev;
|
| 198 | struct acpi_ac *ac;
|
| 199 | int result;
|
| 200 |
|
| 201 | adev = ACPI_COMPANION(&pdev->dev);
|
| 202 | if (!adev)
|
| 203 | return -ENODEV;
|
| 204 |
|
| 205 | ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
|
| 206 | if (!ac)
|
| 207 | return -ENOMEM;
|
| 208 |
|
| 209 | ac->device = adev;
|
| 210 |
|
| 211 | platform_set_drvdata(pdev, ac);
|
| 212 |
|
| 213 | result = acpi_ac_get_state(ac);
|
| 214 | if (result)
|
| 215 | return result;
|
| 216 |
|
| 217 | psy_cfg.drv_data = ac;
|
| 218 |
|
| 219 | ac->charger_desc.name = acpi_device_bid(adev);
|
| 220 | ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS;
|
| 221 | ac->charger_desc.properties = ac_props;
|
| 222 | ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
|
| 223 | ac->charger_desc.get_property = get_ac_property;
|
| 224 | ac->charger = devm_power_supply_register(dev, &ac->charger_desc, &psy_cfg);
|
| 225 | if (IS_ERR(ac->charger))
|
| 226 | return PTR_ERR(ac->charger);
|
| 227 |
|
| 228 | pr_info("AC Adapter [%s] (%s-line)\n", acpi_device_bid(adev),
|
| 229 | str_on_off(ac->state));
|
| 230 |
|
| 231 | result = devm_acpi_install_notify_handler(dev, ACPI_ALL_NOTIFY,
|
| 232 | acpi_ac_notify, ac);
|
| 233 | if (result)
|
| 234 | return result;
|
| 235 |
|
| 236 | ac->battery_nb.notifier_call = acpi_ac_battery_notify;
|
| 237 | register_acpi_notifier(&ac->battery_nb);
|
| 238 |
|
| 239 | return 0;
|
| 240 | }
|
| 241 |
|
| 242 | #ifdef CONFIG_PM_SLEEP
|
| 243 | static int acpi_ac_resume(struct device *dev)
|
| 244 | {
|
| 245 | struct acpi_ac *ac = dev_get_drvdata(dev);
|
| 246 | unsigned int old_state;
|
| 247 |
|
| 248 | old_state = ac->state;
|
| 249 | if (acpi_ac_get_state(ac))
|
| 250 | return 0;
|
| 251 | if (old_state != ac->state)
|
| 252 | power_supply_changed(ac->charger);
|
| 253 |
|
| 254 | return 0;
|
| 255 | }
|
| 256 | #endif
|
| 257 |
|
| 258 | static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
|
| 259 |
|
| 260 | static void acpi_ac_remove(struct platform_device *pdev)
|
| 261 | {
|
| 262 | struct acpi_ac *ac = platform_get_drvdata(pdev);
|
| 263 |
|
| 264 | unregister_acpi_notifier(&ac->battery_nb);
|
| 265 | }
|
| 266 |
|
| 267 | static struct platform_driver acpi_ac_driver = {
|
| 268 | .probe = acpi_ac_probe,
|
| 269 | .remove = acpi_ac_remove,
|
| 270 | .driver = {
|
| 271 | .name = "ac",
|
| 272 | .acpi_match_table = ac_device_ids,
|
| 273 | .pm = &acpi_ac_pm,
|
| 274 | },
|
| 275 | };
|
| 276 |
|
| 277 | static int __init acpi_ac_init(void)
|
| 278 | {
|
| 279 | int result;
|
| 280 |
|
| 281 | if (acpi_disabled)
|
| 282 | return -ENODEV;
|
| 283 |
|
| 284 | if (acpi_quirk_skip_acpi_ac_and_battery())
|
| 285 | return -ENODEV;
|
| 286 |
|
| 287 | dmi_check_system(ac_dmi_table);
|
| 288 |
|
| 289 | result = platform_driver_register(&acpi_ac_driver);
|
| 290 | if (result < 0)
|
| 291 | return -ENODEV;
|
| 292 |
|
| 293 | return 0;
|
| 294 | }
|
| 295 |
|
| 296 | static void __exit acpi_ac_exit(void)
|
| 297 | {
|
| 298 | platform_driver_unregister(&acpi_ac_driver);
|
| 299 | }
|
| 300 | module_init(acpi_ac_init);
|
| 301 | module_exit(acpi_ac_exit);
|