xref: /linux/drivers/acpi/ac.c (revision 48f76a12713253f3abaa39c4ff7606d6fed05a7e)
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 */
acpi_ac_get_state(struct acpi_ac * ac)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 */
get_ac_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)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 */
acpi_ac_notify(acpi_handle handle,u32 event,void * data)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 
acpi_ac_battery_notify(struct notifier_block * nb,unsigned long action,void * data)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 
thinkpad_e530_quirk(const struct dmi_system_id * d)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 
ac_only_quirk(const struct dmi_system_id * d)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 
acpi_ac_probe(struct platform_device * pdev)193 static int acpi_ac_probe(struct platform_device *pdev)
194 {
195 	struct power_supply_config psy_cfg = {};
196 	struct acpi_device *adev;
197 	struct acpi_ac *ac;
198 	int result;
199 
200 	adev = ACPI_COMPANION(&pdev->dev);
201 	if (!adev)
202 		return -ENODEV;
203 
204 	ac = kzalloc_obj(struct acpi_ac);
205 	if (!ac)
206 		return -ENOMEM;
207 
208 	ac->device = adev;
209 
210 	platform_set_drvdata(pdev, ac);
211 
212 	result = acpi_ac_get_state(ac);
213 	if (result)
214 		goto err_release_ac;
215 
216 	psy_cfg.drv_data = ac;
217 
218 	ac->charger_desc.name = acpi_device_bid(adev);
219 	ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS;
220 	ac->charger_desc.properties = ac_props;
221 	ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
222 	ac->charger_desc.get_property = get_ac_property;
223 	ac->charger = power_supply_register(&pdev->dev,
224 					    &ac->charger_desc, &psy_cfg);
225 	if (IS_ERR(ac->charger)) {
226 		result = PTR_ERR(ac->charger);
227 		goto err_release_ac;
228 	}
229 
230 	pr_info("AC Adapter [%s] (%s-line)\n", acpi_device_bid(adev),
231 		str_on_off(ac->state));
232 
233 	ac->battery_nb.notifier_call = acpi_ac_battery_notify;
234 	register_acpi_notifier(&ac->battery_nb);
235 
236 	result = acpi_dev_install_notify_handler(adev, ACPI_ALL_NOTIFY,
237 						 acpi_ac_notify, ac);
238 	if (result)
239 		goto err_unregister;
240 
241 	return 0;
242 
243 err_unregister:
244 	power_supply_unregister(ac->charger);
245 	unregister_acpi_notifier(&ac->battery_nb);
246 err_release_ac:
247 	kfree(ac);
248 
249 	return result;
250 }
251 
252 #ifdef CONFIG_PM_SLEEP
acpi_ac_resume(struct device * dev)253 static int acpi_ac_resume(struct device *dev)
254 {
255 	struct acpi_ac *ac = dev_get_drvdata(dev);
256 	unsigned int old_state;
257 
258 	old_state = ac->state;
259 	if (acpi_ac_get_state(ac))
260 		return 0;
261 	if (old_state != ac->state)
262 		power_supply_changed(ac->charger);
263 
264 	return 0;
265 }
266 #endif
267 
268 static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
269 
acpi_ac_remove(struct platform_device * pdev)270 static void acpi_ac_remove(struct platform_device *pdev)
271 {
272 	struct acpi_ac *ac = platform_get_drvdata(pdev);
273 
274 	acpi_dev_remove_notify_handler(ac->device, ACPI_ALL_NOTIFY,
275 				       acpi_ac_notify);
276 	power_supply_unregister(ac->charger);
277 	unregister_acpi_notifier(&ac->battery_nb);
278 
279 	kfree(ac);
280 }
281 
282 static struct platform_driver acpi_ac_driver = {
283 	.probe = acpi_ac_probe,
284 	.remove = acpi_ac_remove,
285 	.driver = {
286 		.name = "ac",
287 		.acpi_match_table = ac_device_ids,
288 		.pm = &acpi_ac_pm,
289 	},
290 };
291 
acpi_ac_init(void)292 static int __init acpi_ac_init(void)
293 {
294 	int result;
295 
296 	if (acpi_disabled)
297 		return -ENODEV;
298 
299 	if (acpi_quirk_skip_acpi_ac_and_battery())
300 		return -ENODEV;
301 
302 	dmi_check_system(ac_dmi_table);
303 
304 	result = platform_driver_register(&acpi_ac_driver);
305 	if (result < 0)
306 		return -ENODEV;
307 
308 	return 0;
309 }
310 
acpi_ac_exit(void)311 static void __exit acpi_ac_exit(void)
312 {
313 	platform_driver_unregister(&acpi_ac_driver);
314 }
315 module_init(acpi_ac_init);
316 module_exit(acpi_ac_exit);
317