xref: /openbmc/linux/drivers/acpi/acpi_tad.c (revision 96de2506)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ACPI Time and Alarm (TAD) Device Driver
4  *
5  * Copyright (C) 2018 Intel Corporation
6  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  *
8  * This driver is based on Section 9.18 of the ACPI 6.2 specification revision.
9  *
10  * It only supports the system wakeup capabilities of the TAD.
11  *
12  * Provided are sysfs attributes, available under the TAD platform device,
13  * allowing user space to manage the AC and DC wakeup timers of the TAD:
14  * set and read their values, set and check their expire timer wake policies,
15  * check and clear their status and check the capabilities of the TAD reported
16  * by AML.  The DC timer attributes are only present if the TAD supports a
17  * separate DC alarm timer.
18  *
19  * The wakeup events handling and power management of the TAD is expected to
20  * be taken care of by the ACPI PM domain attached to its platform device.
21  */
22 
23 #include <linux/acpi.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/suspend.h>
29 
30 MODULE_LICENSE("GPL v2");
31 MODULE_AUTHOR("Rafael J. Wysocki");
32 
33 /* ACPI TAD capability flags (ACPI 6.2, Section 9.18.2) */
34 #define ACPI_TAD_AC_WAKE	BIT(0)
35 #define ACPI_TAD_DC_WAKE	BIT(1)
36 #define ACPI_TAD_RT		BIT(2)
37 #define ACPI_TAD_RT_IN_MS	BIT(3)
38 #define ACPI_TAD_S4_S5__GWS	BIT(4)
39 #define ACPI_TAD_AC_S4_WAKE	BIT(5)
40 #define ACPI_TAD_AC_S5_WAKE	BIT(6)
41 #define ACPI_TAD_DC_S4_WAKE	BIT(7)
42 #define ACPI_TAD_DC_S5_WAKE	BIT(8)
43 
44 /* ACPI TAD alarm timer selection */
45 #define ACPI_TAD_AC_TIMER	(u32)0
46 #define ACPI_TAD_DC_TIMER	(u32)1
47 
48 /* Special value for disabled timer or expired timer wake policy. */
49 #define ACPI_TAD_WAKE_DISABLED	(~(u32)0)
50 
51 struct acpi_tad_driver_data {
52 	u32 capabilities;
53 };
54 
55 static int acpi_tad_wake_set(struct device *dev, char *method, u32 timer_id,
56 			     u32 value)
57 {
58 	acpi_handle handle = ACPI_HANDLE(dev);
59 	union acpi_object args[] = {
60 		{ .type = ACPI_TYPE_INTEGER, },
61 		{ .type = ACPI_TYPE_INTEGER, },
62 	};
63 	struct acpi_object_list arg_list = {
64 		.pointer = args,
65 		.count = ARRAY_SIZE(args),
66 	};
67 	unsigned long long retval;
68 	acpi_status status;
69 
70 	args[0].integer.value = timer_id;
71 	args[1].integer.value = value;
72 
73 	pm_runtime_get_sync(dev);
74 
75 	status = acpi_evaluate_integer(handle, method, &arg_list, &retval);
76 
77 	pm_runtime_put_sync(dev);
78 
79 	if (ACPI_FAILURE(status) || retval)
80 		return -EIO;
81 
82 	return 0;
83 }
84 
85 static int acpi_tad_wake_write(struct device *dev, const char *buf, char *method,
86 			       u32 timer_id, const char *specval)
87 {
88 	u32 value;
89 
90 	if (sysfs_streq(buf, specval)) {
91 		value = ACPI_TAD_WAKE_DISABLED;
92 	} else {
93 		int ret = kstrtou32(buf, 0, &value);
94 
95 		if (ret)
96 			return ret;
97 
98 		if (value == ACPI_TAD_WAKE_DISABLED)
99 			return -EINVAL;
100 	}
101 
102 	return acpi_tad_wake_set(dev, method, timer_id, value);
103 }
104 
105 static ssize_t acpi_tad_wake_read(struct device *dev, char *buf, char *method,
106 				  u32 timer_id, const char *specval)
107 {
108 	acpi_handle handle = ACPI_HANDLE(dev);
109 	union acpi_object args[] = {
110 		{ .type = ACPI_TYPE_INTEGER, },
111 	};
112 	struct acpi_object_list arg_list = {
113 		.pointer = args,
114 		.count = ARRAY_SIZE(args),
115 	};
116 	unsigned long long retval;
117 	acpi_status status;
118 
119 	args[0].integer.value = timer_id;
120 
121 	pm_runtime_get_sync(dev);
122 
123 	status = acpi_evaluate_integer(handle, method, &arg_list, &retval);
124 
125 	pm_runtime_put_sync(dev);
126 
127 	if (ACPI_FAILURE(status))
128 		return -EIO;
129 
130 	if ((u32)retval == ACPI_TAD_WAKE_DISABLED)
131 		return sprintf(buf, "%s\n", specval);
132 
133 	return sprintf(buf, "%u\n", (u32)retval);
134 }
135 
136 static const char *alarm_specval = "disabled";
137 
138 static int acpi_tad_alarm_write(struct device *dev, const char *buf,
139 				u32 timer_id)
140 {
141 	return acpi_tad_wake_write(dev, buf, "_STV", timer_id, alarm_specval);
142 }
143 
144 static ssize_t acpi_tad_alarm_read(struct device *dev, char *buf, u32 timer_id)
145 {
146 	return acpi_tad_wake_read(dev, buf, "_TIV", timer_id, alarm_specval);
147 }
148 
149 static const char *policy_specval = "never";
150 
151 static int acpi_tad_policy_write(struct device *dev, const char *buf,
152 				 u32 timer_id)
153 {
154 	return acpi_tad_wake_write(dev, buf, "_STP", timer_id, policy_specval);
155 }
156 
157 static ssize_t acpi_tad_policy_read(struct device *dev, char *buf, u32 timer_id)
158 {
159 	return acpi_tad_wake_read(dev, buf, "_TIP", timer_id, policy_specval);
160 }
161 
162 static int acpi_tad_clear_status(struct device *dev, u32 timer_id)
163 {
164 	acpi_handle handle = ACPI_HANDLE(dev);
165 	union acpi_object args[] = {
166 		{ .type = ACPI_TYPE_INTEGER, },
167 	};
168 	struct acpi_object_list arg_list = {
169 		.pointer = args,
170 		.count = ARRAY_SIZE(args),
171 	};
172 	unsigned long long retval;
173 	acpi_status status;
174 
175 	args[0].integer.value = timer_id;
176 
177 	pm_runtime_get_sync(dev);
178 
179 	status = acpi_evaluate_integer(handle, "_CWS", &arg_list, &retval);
180 
181 	pm_runtime_put_sync(dev);
182 
183 	if (ACPI_FAILURE(status) || retval)
184 		return -EIO;
185 
186 	return 0;
187 }
188 
189 static int acpi_tad_status_write(struct device *dev, const char *buf, u32 timer_id)
190 {
191 	int ret, value;
192 
193 	ret = kstrtoint(buf, 0, &value);
194 	if (ret)
195 		return ret;
196 
197 	if (value)
198 		return -EINVAL;
199 
200 	return acpi_tad_clear_status(dev, timer_id);
201 }
202 
203 static ssize_t acpi_tad_status_read(struct device *dev, char *buf, u32 timer_id)
204 {
205 	acpi_handle handle = ACPI_HANDLE(dev);
206 	union acpi_object args[] = {
207 		{ .type = ACPI_TYPE_INTEGER, },
208 	};
209 	struct acpi_object_list arg_list = {
210 		.pointer = args,
211 		.count = ARRAY_SIZE(args),
212 	};
213 	unsigned long long retval;
214 	acpi_status status;
215 
216 	args[0].integer.value = timer_id;
217 
218 	pm_runtime_get_sync(dev);
219 
220 	status = acpi_evaluate_integer(handle, "_GWS", &arg_list, &retval);
221 
222 	pm_runtime_put_sync(dev);
223 
224 	if (ACPI_FAILURE(status))
225 		return -EIO;
226 
227 	return sprintf(buf, "0x%02X\n", (u32)retval);
228 }
229 
230 static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
231 			 char *buf)
232 {
233 	struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
234 
235 	return sprintf(buf, "0x%02X\n", dd->capabilities);
236 }
237 
238 static DEVICE_ATTR_RO(caps);
239 
240 static ssize_t ac_alarm_store(struct device *dev, struct device_attribute *attr,
241 			      const char *buf, size_t count)
242 {
243 	int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_AC_TIMER);
244 
245 	return ret ? ret : count;
246 }
247 
248 static ssize_t ac_alarm_show(struct device *dev, struct device_attribute *attr,
249 			     char *buf)
250 {
251 	return acpi_tad_alarm_read(dev, buf, ACPI_TAD_AC_TIMER);
252 }
253 
254 static DEVICE_ATTR(ac_alarm, S_IRUSR | S_IWUSR, ac_alarm_show, ac_alarm_store);
255 
256 static ssize_t ac_policy_store(struct device *dev, struct device_attribute *attr,
257 			       const char *buf, size_t count)
258 {
259 	int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_AC_TIMER);
260 
261 	return ret ? ret : count;
262 }
263 
264 static ssize_t ac_policy_show(struct device *dev, struct device_attribute *attr,
265 			      char *buf)
266 {
267 	return acpi_tad_policy_read(dev, buf, ACPI_TAD_AC_TIMER);
268 }
269 
270 static DEVICE_ATTR(ac_policy, S_IRUSR | S_IWUSR, ac_policy_show, ac_policy_store);
271 
272 static ssize_t ac_status_store(struct device *dev, struct device_attribute *attr,
273 			       const char *buf, size_t count)
274 {
275 	int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_AC_TIMER);
276 
277 	return ret ? ret : count;
278 }
279 
280 static ssize_t ac_status_show(struct device *dev, struct device_attribute *attr,
281 			      char *buf)
282 {
283 	return acpi_tad_status_read(dev, buf, ACPI_TAD_AC_TIMER);
284 }
285 
286 static DEVICE_ATTR(ac_status, S_IRUSR | S_IWUSR, ac_status_show, ac_status_store);
287 
288 static struct attribute *acpi_tad_attrs[] = {
289 	&dev_attr_caps.attr,
290 	&dev_attr_ac_alarm.attr,
291 	&dev_attr_ac_policy.attr,
292 	&dev_attr_ac_status.attr,
293 	NULL,
294 };
295 static const struct attribute_group acpi_tad_attr_group = {
296 	.attrs	= acpi_tad_attrs,
297 };
298 
299 static ssize_t dc_alarm_store(struct device *dev, struct device_attribute *attr,
300 			      const char *buf, size_t count)
301 {
302 	int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_DC_TIMER);
303 
304 	return ret ? ret : count;
305 }
306 
307 static ssize_t dc_alarm_show(struct device *dev, struct device_attribute *attr,
308 			     char *buf)
309 {
310 	return acpi_tad_alarm_read(dev, buf, ACPI_TAD_DC_TIMER);
311 }
312 
313 static DEVICE_ATTR(dc_alarm, S_IRUSR | S_IWUSR, dc_alarm_show, dc_alarm_store);
314 
315 static ssize_t dc_policy_store(struct device *dev, struct device_attribute *attr,
316 			       const char *buf, size_t count)
317 {
318 	int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_DC_TIMER);
319 
320 	return ret ? ret : count;
321 }
322 
323 static ssize_t dc_policy_show(struct device *dev, struct device_attribute *attr,
324 			      char *buf)
325 {
326 	return acpi_tad_policy_read(dev, buf, ACPI_TAD_DC_TIMER);
327 }
328 
329 static DEVICE_ATTR(dc_policy, S_IRUSR | S_IWUSR, dc_policy_show, dc_policy_store);
330 
331 static ssize_t dc_status_store(struct device *dev, struct device_attribute *attr,
332 			       const char *buf, size_t count)
333 {
334 	int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_DC_TIMER);
335 
336 	return ret ? ret : count;
337 }
338 
339 static ssize_t dc_status_show(struct device *dev, struct device_attribute *attr,
340 			      char *buf)
341 {
342 	return acpi_tad_status_read(dev, buf, ACPI_TAD_DC_TIMER);
343 }
344 
345 static DEVICE_ATTR(dc_status, S_IRUSR | S_IWUSR, dc_status_show, dc_status_store);
346 
347 static struct attribute *acpi_tad_dc_attrs[] = {
348 	&dev_attr_dc_alarm.attr,
349 	&dev_attr_dc_policy.attr,
350 	&dev_attr_dc_status.attr,
351 	NULL,
352 };
353 static const struct attribute_group acpi_tad_dc_attr_group = {
354 	.attrs	= acpi_tad_dc_attrs,
355 };
356 
357 static int acpi_tad_disable_timer(struct device *dev, u32 timer_id)
358 {
359 	return acpi_tad_wake_set(dev, "_STV", timer_id, ACPI_TAD_WAKE_DISABLED);
360 }
361 
362 static int acpi_tad_remove(struct platform_device *pdev)
363 {
364 	struct device *dev = &pdev->dev;
365 	struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
366 
367 	device_init_wakeup(dev, false);
368 
369 	pm_runtime_get_sync(dev);
370 
371 	if (dd->capabilities & ACPI_TAD_DC_WAKE)
372 		sysfs_remove_group(&dev->kobj, &acpi_tad_dc_attr_group);
373 
374 	sysfs_remove_group(&dev->kobj, &acpi_tad_attr_group);
375 
376 	acpi_tad_disable_timer(dev, ACPI_TAD_AC_TIMER);
377 	acpi_tad_clear_status(dev, ACPI_TAD_AC_TIMER);
378 	if (dd->capabilities & ACPI_TAD_DC_WAKE) {
379 		acpi_tad_disable_timer(dev, ACPI_TAD_DC_TIMER);
380 		acpi_tad_clear_status(dev, ACPI_TAD_DC_TIMER);
381 	}
382 
383 	pm_runtime_put_sync(dev);
384 	pm_runtime_disable(dev);
385 	return 0;
386 }
387 
388 static int acpi_tad_probe(struct platform_device *pdev)
389 {
390 	struct device *dev = &pdev->dev;
391 	acpi_handle handle = ACPI_HANDLE(dev);
392 	struct acpi_tad_driver_data *dd;
393 	acpi_status status;
394 	unsigned long long caps;
395 	int ret;
396 
397 	/*
398 	 * Initialization failure messages are mostly about firmware issues, so
399 	 * print them at the "info" level.
400 	 */
401 	status = acpi_evaluate_integer(handle, "_GCP", NULL, &caps);
402 	if (ACPI_FAILURE(status)) {
403 		dev_info(dev, "Unable to get capabilities\n");
404 		return -ENODEV;
405 	}
406 
407 	if (!(caps & ACPI_TAD_AC_WAKE)) {
408 		dev_info(dev, "Unsupported capabilities\n");
409 		return -ENODEV;
410 	}
411 
412 	if (!acpi_has_method(handle, "_PRW")) {
413 		dev_info(dev, "Missing _PRW\n");
414 		return -ENODEV;
415 	}
416 
417 	dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
418 	if (!dd)
419 		return -ENOMEM;
420 
421 	dd->capabilities = caps;
422 	dev_set_drvdata(dev, dd);
423 
424 	/*
425 	 * Assume that the ACPI PM domain has been attached to the device and
426 	 * simply enable system wakeup and runtime PM and put the device into
427 	 * runtime suspend.  Everything else should be taken care of by the ACPI
428 	 * PM domain callbacks.
429 	 */
430 	device_init_wakeup(dev, true);
431 	dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
432 				     DPM_FLAG_LEAVE_SUSPENDED);
433 	/*
434 	 * The platform bus type layer tells the ACPI PM domain powers up the
435 	 * device, so set the runtime PM status of it to "active".
436 	 */
437 	pm_runtime_set_active(dev);
438 	pm_runtime_enable(dev);
439 	pm_runtime_suspend(dev);
440 
441 	ret = sysfs_create_group(&dev->kobj, &acpi_tad_attr_group);
442 	if (ret)
443 		goto fail;
444 
445 	if (caps & ACPI_TAD_DC_WAKE) {
446 		ret = sysfs_create_group(&dev->kobj, &acpi_tad_dc_attr_group);
447 		if (ret)
448 			goto fail;
449 	}
450 
451 	return 0;
452 
453 fail:
454 	acpi_tad_remove(pdev);
455 	return ret;
456 }
457 
458 static const struct acpi_device_id acpi_tad_ids[] = {
459 	{"ACPI000E", 0},
460 	{}
461 };
462 
463 static struct platform_driver acpi_tad_driver = {
464 	.driver = {
465 		.name = "acpi-tad",
466 		.acpi_match_table = acpi_tad_ids,
467 	},
468 	.probe = acpi_tad_probe,
469 	.remove = acpi_tad_remove,
470 };
471 MODULE_DEVICE_TABLE(acpi, acpi_tad_ids);
472 
473 module_platform_driver(acpi_tad_driver);
474