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