1 // SPDX-License-Identifier: GPL-2.0
3 * ACPI Time and Alarm (TAD) Device Driver
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
8 * This driver is based on Section 9.18 of the ACPI 6.2 specification revision.
10 * It only supports the system wakeup capabilities of the TAD.
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.
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.
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>
30 MODULE_LICENSE("GPL v2");
31 MODULE_AUTHOR("Rafael J. Wysocki");
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)
44 /* ACPI TAD alarm timer selection */
45 #define ACPI_TAD_AC_TIMER (u32)0
46 #define ACPI_TAD_DC_TIMER (u32)1
48 /* Special value for disabled timer or expired timer wake policy. */
49 #define ACPI_TAD_WAKE_DISABLED (~(u32)0)
51 struct acpi_tad_driver_data {
56 u16 year; /* 1900 - 9999 */
57 u8 month; /* 1 - 12 */
60 u8 minute; /* 0 - 59 */
61 u8 second; /* 0 - 59 */
62 u8 valid; /* 0 (failed) or 1 (success) for reads, 0 for writes */
63 u16 msec; /* 1 - 1000 */
64 s16 tz; /* -1440 to 1440 or 2047 (unspecified) */
66 u8 padding[3]; /* must be 0 */
69 static int acpi_tad_set_real_time(struct device *dev, struct acpi_tad_rt *rt)
71 acpi_handle handle = ACPI_HANDLE(dev);
72 union acpi_object args[] = {
73 { .type = ACPI_TYPE_BUFFER, },
75 struct acpi_object_list arg_list = {
77 .count = ARRAY_SIZE(args),
79 unsigned long long retval;
82 if (rt->year < 1900 || rt->year > 9999 ||
83 rt->month < 1 || rt->month > 12 ||
84 rt->hour > 23 || rt->minute > 59 || rt->second > 59 ||
85 rt->tz < -1440 || (rt->tz > 1440 && rt->tz != 2047) ||
89 args[0].buffer.pointer = (u8 *)rt;
90 args[0].buffer.length = sizeof(*rt);
92 pm_runtime_get_sync(dev);
94 status = acpi_evaluate_integer(handle, "_SRT", &arg_list, &retval);
96 pm_runtime_put_sync(dev);
98 if (ACPI_FAILURE(status) || retval)
104 static int acpi_tad_get_real_time(struct device *dev, struct acpi_tad_rt *rt)
106 acpi_handle handle = ACPI_HANDLE(dev);
107 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER };
108 union acpi_object *out_obj;
109 struct acpi_tad_rt *data;
113 pm_runtime_get_sync(dev);
115 status = acpi_evaluate_object(handle, "_GRT", NULL, &output);
117 pm_runtime_put_sync(dev);
119 if (ACPI_FAILURE(status))
122 out_obj = output.pointer;
123 if (out_obj->type != ACPI_TYPE_BUFFER)
126 if (out_obj->buffer.length != sizeof(*rt))
129 data = (struct acpi_tad_rt *)(out_obj->buffer.pointer);
133 memcpy(rt, data, sizeof(*rt));
137 ACPI_FREE(output.pointer);
141 static char *acpi_tad_rt_next_field(char *s, int *val)
150 if (kstrtoint(s, 10, val))
156 static ssize_t time_store(struct device *dev, struct device_attribute *attr,
157 const char *buf, size_t count)
159 struct acpi_tad_rt rt;
161 int val, ret = -ENODATA;
163 str = kmemdup_nul(buf, count, GFP_KERNEL);
167 s = acpi_tad_rt_next_field(str, &val);
173 s = acpi_tad_rt_next_field(s, &val);
179 s = acpi_tad_rt_next_field(s, &val);
185 s = acpi_tad_rt_next_field(s, &val);
191 s = acpi_tad_rt_next_field(s, &val);
197 s = acpi_tad_rt_next_field(s, &val);
203 s = acpi_tad_rt_next_field(s, &val);
209 if (kstrtoint(s, 10, &val))
216 memset(rt.padding, 0, 3);
218 ret = acpi_tad_set_real_time(dev, &rt);
222 return ret ? ret : count;
225 static ssize_t time_show(struct device *dev, struct device_attribute *attr,
228 struct acpi_tad_rt rt;
231 ret = acpi_tad_get_real_time(dev, &rt);
235 return sprintf(buf, "%u:%u:%u:%u:%u:%u:%d:%u\n",
236 rt.year, rt.month, rt.day, rt.hour, rt.minute, rt.second,
240 static DEVICE_ATTR_RW(time);
242 static struct attribute *acpi_tad_time_attrs[] = {
246 static const struct attribute_group acpi_tad_time_attr_group = {
247 .attrs = acpi_tad_time_attrs,
250 static int acpi_tad_wake_set(struct device *dev, char *method, u32 timer_id,
253 acpi_handle handle = ACPI_HANDLE(dev);
254 union acpi_object args[] = {
255 { .type = ACPI_TYPE_INTEGER, },
256 { .type = ACPI_TYPE_INTEGER, },
258 struct acpi_object_list arg_list = {
260 .count = ARRAY_SIZE(args),
262 unsigned long long retval;
265 args[0].integer.value = timer_id;
266 args[1].integer.value = value;
268 pm_runtime_get_sync(dev);
270 status = acpi_evaluate_integer(handle, method, &arg_list, &retval);
272 pm_runtime_put_sync(dev);
274 if (ACPI_FAILURE(status) || retval)
280 static int acpi_tad_wake_write(struct device *dev, const char *buf, char *method,
281 u32 timer_id, const char *specval)
285 if (sysfs_streq(buf, specval)) {
286 value = ACPI_TAD_WAKE_DISABLED;
288 int ret = kstrtou32(buf, 0, &value);
293 if (value == ACPI_TAD_WAKE_DISABLED)
297 return acpi_tad_wake_set(dev, method, timer_id, value);
300 static ssize_t acpi_tad_wake_read(struct device *dev, char *buf, char *method,
301 u32 timer_id, const char *specval)
303 acpi_handle handle = ACPI_HANDLE(dev);
304 union acpi_object args[] = {
305 { .type = ACPI_TYPE_INTEGER, },
307 struct acpi_object_list arg_list = {
309 .count = ARRAY_SIZE(args),
311 unsigned long long retval;
314 args[0].integer.value = timer_id;
316 pm_runtime_get_sync(dev);
318 status = acpi_evaluate_integer(handle, method, &arg_list, &retval);
320 pm_runtime_put_sync(dev);
322 if (ACPI_FAILURE(status))
325 if ((u32)retval == ACPI_TAD_WAKE_DISABLED)
326 return sprintf(buf, "%s\n", specval);
328 return sprintf(buf, "%u\n", (u32)retval);
331 static const char *alarm_specval = "disabled";
333 static int acpi_tad_alarm_write(struct device *dev, const char *buf,
336 return acpi_tad_wake_write(dev, buf, "_STV", timer_id, alarm_specval);
339 static ssize_t acpi_tad_alarm_read(struct device *dev, char *buf, u32 timer_id)
341 return acpi_tad_wake_read(dev, buf, "_TIV", timer_id, alarm_specval);
344 static const char *policy_specval = "never";
346 static int acpi_tad_policy_write(struct device *dev, const char *buf,
349 return acpi_tad_wake_write(dev, buf, "_STP", timer_id, policy_specval);
352 static ssize_t acpi_tad_policy_read(struct device *dev, char *buf, u32 timer_id)
354 return acpi_tad_wake_read(dev, buf, "_TIP", timer_id, policy_specval);
357 static int acpi_tad_clear_status(struct device *dev, u32 timer_id)
359 acpi_handle handle = ACPI_HANDLE(dev);
360 union acpi_object args[] = {
361 { .type = ACPI_TYPE_INTEGER, },
363 struct acpi_object_list arg_list = {
365 .count = ARRAY_SIZE(args),
367 unsigned long long retval;
370 args[0].integer.value = timer_id;
372 pm_runtime_get_sync(dev);
374 status = acpi_evaluate_integer(handle, "_CWS", &arg_list, &retval);
376 pm_runtime_put_sync(dev);
378 if (ACPI_FAILURE(status) || retval)
384 static int acpi_tad_status_write(struct device *dev, const char *buf, u32 timer_id)
388 ret = kstrtoint(buf, 0, &value);
395 return acpi_tad_clear_status(dev, timer_id);
398 static ssize_t acpi_tad_status_read(struct device *dev, char *buf, u32 timer_id)
400 acpi_handle handle = ACPI_HANDLE(dev);
401 union acpi_object args[] = {
402 { .type = ACPI_TYPE_INTEGER, },
404 struct acpi_object_list arg_list = {
406 .count = ARRAY_SIZE(args),
408 unsigned long long retval;
411 args[0].integer.value = timer_id;
413 pm_runtime_get_sync(dev);
415 status = acpi_evaluate_integer(handle, "_GWS", &arg_list, &retval);
417 pm_runtime_put_sync(dev);
419 if (ACPI_FAILURE(status))
422 return sprintf(buf, "0x%02X\n", (u32)retval);
425 static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
428 struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
430 return sprintf(buf, "0x%02X\n", dd->capabilities);
433 static DEVICE_ATTR_RO(caps);
435 static ssize_t ac_alarm_store(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
438 int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_AC_TIMER);
440 return ret ? ret : count;
443 static ssize_t ac_alarm_show(struct device *dev, struct device_attribute *attr,
446 return acpi_tad_alarm_read(dev, buf, ACPI_TAD_AC_TIMER);
449 static DEVICE_ATTR_RW(ac_alarm);
451 static ssize_t ac_policy_store(struct device *dev, struct device_attribute *attr,
452 const char *buf, size_t count)
454 int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_AC_TIMER);
456 return ret ? ret : count;
459 static ssize_t ac_policy_show(struct device *dev, struct device_attribute *attr,
462 return acpi_tad_policy_read(dev, buf, ACPI_TAD_AC_TIMER);
465 static DEVICE_ATTR_RW(ac_policy);
467 static ssize_t ac_status_store(struct device *dev, struct device_attribute *attr,
468 const char *buf, size_t count)
470 int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_AC_TIMER);
472 return ret ? ret : count;
475 static ssize_t ac_status_show(struct device *dev, struct device_attribute *attr,
478 return acpi_tad_status_read(dev, buf, ACPI_TAD_AC_TIMER);
481 static DEVICE_ATTR_RW(ac_status);
483 static struct attribute *acpi_tad_attrs[] = {
485 &dev_attr_ac_alarm.attr,
486 &dev_attr_ac_policy.attr,
487 &dev_attr_ac_status.attr,
490 static const struct attribute_group acpi_tad_attr_group = {
491 .attrs = acpi_tad_attrs,
494 static ssize_t dc_alarm_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
497 int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_DC_TIMER);
499 return ret ? ret : count;
502 static ssize_t dc_alarm_show(struct device *dev, struct device_attribute *attr,
505 return acpi_tad_alarm_read(dev, buf, ACPI_TAD_DC_TIMER);
508 static DEVICE_ATTR_RW(dc_alarm);
510 static ssize_t dc_policy_store(struct device *dev, struct device_attribute *attr,
511 const char *buf, size_t count)
513 int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_DC_TIMER);
515 return ret ? ret : count;
518 static ssize_t dc_policy_show(struct device *dev, struct device_attribute *attr,
521 return acpi_tad_policy_read(dev, buf, ACPI_TAD_DC_TIMER);
524 static DEVICE_ATTR_RW(dc_policy);
526 static ssize_t dc_status_store(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
529 int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_DC_TIMER);
531 return ret ? ret : count;
534 static ssize_t dc_status_show(struct device *dev, struct device_attribute *attr,
537 return acpi_tad_status_read(dev, buf, ACPI_TAD_DC_TIMER);
540 static DEVICE_ATTR_RW(dc_status);
542 static struct attribute *acpi_tad_dc_attrs[] = {
543 &dev_attr_dc_alarm.attr,
544 &dev_attr_dc_policy.attr,
545 &dev_attr_dc_status.attr,
548 static const struct attribute_group acpi_tad_dc_attr_group = {
549 .attrs = acpi_tad_dc_attrs,
552 static int acpi_tad_disable_timer(struct device *dev, u32 timer_id)
554 return acpi_tad_wake_set(dev, "_STV", timer_id, ACPI_TAD_WAKE_DISABLED);
557 static int acpi_tad_remove(struct platform_device *pdev)
559 struct device *dev = &pdev->dev;
560 struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
562 device_init_wakeup(dev, false);
564 pm_runtime_get_sync(dev);
566 if (dd->capabilities & ACPI_TAD_DC_WAKE)
567 sysfs_remove_group(&dev->kobj, &acpi_tad_dc_attr_group);
569 sysfs_remove_group(&dev->kobj, &acpi_tad_attr_group);
571 acpi_tad_disable_timer(dev, ACPI_TAD_AC_TIMER);
572 acpi_tad_clear_status(dev, ACPI_TAD_AC_TIMER);
573 if (dd->capabilities & ACPI_TAD_DC_WAKE) {
574 acpi_tad_disable_timer(dev, ACPI_TAD_DC_TIMER);
575 acpi_tad_clear_status(dev, ACPI_TAD_DC_TIMER);
578 pm_runtime_put_sync(dev);
579 pm_runtime_disable(dev);
583 static int acpi_tad_probe(struct platform_device *pdev)
585 struct device *dev = &pdev->dev;
586 acpi_handle handle = ACPI_HANDLE(dev);
587 struct acpi_tad_driver_data *dd;
589 unsigned long long caps;
593 * Initialization failure messages are mostly about firmware issues, so
594 * print them at the "info" level.
596 status = acpi_evaluate_integer(handle, "_GCP", NULL, &caps);
597 if (ACPI_FAILURE(status)) {
598 dev_info(dev, "Unable to get capabilities\n");
602 if (!(caps & ACPI_TAD_AC_WAKE)) {
603 dev_info(dev, "Unsupported capabilities\n");
607 if (!acpi_has_method(handle, "_PRW")) {
608 dev_info(dev, "Missing _PRW\n");
612 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
616 dd->capabilities = caps;
617 dev_set_drvdata(dev, dd);
620 * Assume that the ACPI PM domain has been attached to the device and
621 * simply enable system wakeup and runtime PM and put the device into
622 * runtime suspend. Everything else should be taken care of by the ACPI
623 * PM domain callbacks.
625 device_init_wakeup(dev, true);
626 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
627 DPM_FLAG_MAY_SKIP_RESUME);
629 * The platform bus type layer tells the ACPI PM domain powers up the
630 * device, so set the runtime PM status of it to "active".
632 pm_runtime_set_active(dev);
633 pm_runtime_enable(dev);
634 pm_runtime_suspend(dev);
636 ret = sysfs_create_group(&dev->kobj, &acpi_tad_attr_group);
640 if (caps & ACPI_TAD_DC_WAKE) {
641 ret = sysfs_create_group(&dev->kobj, &acpi_tad_dc_attr_group);
646 if (caps & ACPI_TAD_RT) {
647 ret = sysfs_create_group(&dev->kobj, &acpi_tad_time_attr_group);
655 acpi_tad_remove(pdev);
659 static const struct acpi_device_id acpi_tad_ids[] = {
664 static struct platform_driver acpi_tad_driver = {
667 .acpi_match_table = acpi_tad_ids,
669 .probe = acpi_tad_probe,
670 .remove = acpi_tad_remove,
672 MODULE_DEVICE_TABLE(acpi, acpi_tad_ids);
674 module_platform_driver(acpi_tad_driver);