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_DESCRIPTION("ACPI Time and Alarm (TAD) Device Driver");
31 MODULE_LICENSE("GPL v2");
32 MODULE_AUTHOR("Rafael J. Wysocki");
34 /* ACPI TAD capability flags (ACPI 6.2, Section 9.18.2) */
35 #define ACPI_TAD_AC_WAKE BIT(0)
36 #define ACPI_TAD_DC_WAKE BIT(1)
37 #define ACPI_TAD_RT BIT(2)
38 #define ACPI_TAD_RT_IN_MS BIT(3)
39 #define ACPI_TAD_S4_S5__GWS BIT(4)
40 #define ACPI_TAD_AC_S4_WAKE BIT(5)
41 #define ACPI_TAD_AC_S5_WAKE BIT(6)
42 #define ACPI_TAD_DC_S4_WAKE BIT(7)
43 #define ACPI_TAD_DC_S5_WAKE BIT(8)
45 /* ACPI TAD alarm timer selection */
46 #define ACPI_TAD_AC_TIMER (u32)0
47 #define ACPI_TAD_DC_TIMER (u32)1
49 /* Special value for disabled timer or expired timer wake policy. */
50 #define ACPI_TAD_WAKE_DISABLED (~(u32)0)
52 struct acpi_tad_driver_data
{
57 u16 year
; /* 1900 - 9999 */
58 u8 month
; /* 1 - 12 */
61 u8 minute
; /* 0 - 59 */
62 u8 second
; /* 0 - 59 */
63 u8 valid
; /* 0 (failed) or 1 (success) for reads, 0 for writes */
64 u16 msec
; /* 1 - 1000 */
65 s16 tz
; /* -1440 to 1440 or 2047 (unspecified) */
67 u8 padding
[3]; /* must be 0 */
70 static int acpi_tad_set_real_time(struct device
*dev
, struct acpi_tad_rt
*rt
)
72 acpi_handle handle
= ACPI_HANDLE(dev
);
73 union acpi_object args
[] = {
74 { .type
= ACPI_TYPE_BUFFER
, },
76 struct acpi_object_list arg_list
= {
78 .count
= ARRAY_SIZE(args
),
80 unsigned long long retval
;
83 if (rt
->year
< 1900 || rt
->year
> 9999 ||
84 rt
->month
< 1 || rt
->month
> 12 ||
85 rt
->hour
> 23 || rt
->minute
> 59 || rt
->second
> 59 ||
86 rt
->tz
< -1440 || (rt
->tz
> 1440 && rt
->tz
!= 2047) ||
90 args
[0].buffer
.pointer
= (u8
*)rt
;
91 args
[0].buffer
.length
= sizeof(*rt
);
93 pm_runtime_get_sync(dev
);
95 status
= acpi_evaluate_integer(handle
, "_SRT", &arg_list
, &retval
);
97 pm_runtime_put_sync(dev
);
99 if (ACPI_FAILURE(status
) || retval
)
105 static int acpi_tad_get_real_time(struct device
*dev
, struct acpi_tad_rt
*rt
)
107 acpi_handle handle
= ACPI_HANDLE(dev
);
108 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
};
109 union acpi_object
*out_obj
;
110 struct acpi_tad_rt
*data
;
114 pm_runtime_get_sync(dev
);
116 status
= acpi_evaluate_object(handle
, "_GRT", NULL
, &output
);
118 pm_runtime_put_sync(dev
);
120 if (ACPI_FAILURE(status
))
123 out_obj
= output
.pointer
;
124 if (out_obj
->type
!= ACPI_TYPE_BUFFER
)
127 if (out_obj
->buffer
.length
!= sizeof(*rt
))
130 data
= (struct acpi_tad_rt
*)(out_obj
->buffer
.pointer
);
134 memcpy(rt
, data
, sizeof(*rt
));
138 ACPI_FREE(output
.pointer
);
142 static char *acpi_tad_rt_next_field(char *s
, int *val
)
151 if (kstrtoint(s
, 10, val
))
157 static ssize_t
time_store(struct device
*dev
, struct device_attribute
*attr
,
158 const char *buf
, size_t count
)
160 struct acpi_tad_rt rt
;
162 int val
, ret
= -ENODATA
;
164 str
= kmemdup_nul(buf
, count
, GFP_KERNEL
);
168 s
= acpi_tad_rt_next_field(str
, &val
);
174 s
= acpi_tad_rt_next_field(s
, &val
);
180 s
= acpi_tad_rt_next_field(s
, &val
);
186 s
= acpi_tad_rt_next_field(s
, &val
);
192 s
= acpi_tad_rt_next_field(s
, &val
);
198 s
= acpi_tad_rt_next_field(s
, &val
);
204 s
= acpi_tad_rt_next_field(s
, &val
);
210 if (kstrtoint(s
, 10, &val
))
217 memset(rt
.padding
, 0, 3);
219 ret
= acpi_tad_set_real_time(dev
, &rt
);
223 return ret
? ret
: count
;
226 static ssize_t
time_show(struct device
*dev
, struct device_attribute
*attr
,
229 struct acpi_tad_rt rt
;
232 ret
= acpi_tad_get_real_time(dev
, &rt
);
236 return sprintf(buf
, "%u:%u:%u:%u:%u:%u:%d:%u\n",
237 rt
.year
, rt
.month
, rt
.day
, rt
.hour
, rt
.minute
, rt
.second
,
241 static DEVICE_ATTR_RW(time
);
243 static struct attribute
*acpi_tad_time_attrs
[] = {
247 static const struct attribute_group acpi_tad_time_attr_group
= {
248 .attrs
= acpi_tad_time_attrs
,
251 static int acpi_tad_wake_set(struct device
*dev
, char *method
, u32 timer_id
,
254 acpi_handle handle
= ACPI_HANDLE(dev
);
255 union acpi_object args
[] = {
256 { .type
= ACPI_TYPE_INTEGER
, },
257 { .type
= ACPI_TYPE_INTEGER
, },
259 struct acpi_object_list arg_list
= {
261 .count
= ARRAY_SIZE(args
),
263 unsigned long long retval
;
266 args
[0].integer
.value
= timer_id
;
267 args
[1].integer
.value
= value
;
269 pm_runtime_get_sync(dev
);
271 status
= acpi_evaluate_integer(handle
, method
, &arg_list
, &retval
);
273 pm_runtime_put_sync(dev
);
275 if (ACPI_FAILURE(status
) || retval
)
281 static int acpi_tad_wake_write(struct device
*dev
, const char *buf
, char *method
,
282 u32 timer_id
, const char *specval
)
286 if (sysfs_streq(buf
, specval
)) {
287 value
= ACPI_TAD_WAKE_DISABLED
;
289 int ret
= kstrtou32(buf
, 0, &value
);
294 if (value
== ACPI_TAD_WAKE_DISABLED
)
298 return acpi_tad_wake_set(dev
, method
, timer_id
, value
);
301 static ssize_t
acpi_tad_wake_read(struct device
*dev
, char *buf
, char *method
,
302 u32 timer_id
, const char *specval
)
304 acpi_handle handle
= ACPI_HANDLE(dev
);
305 union acpi_object args
[] = {
306 { .type
= ACPI_TYPE_INTEGER
, },
308 struct acpi_object_list arg_list
= {
310 .count
= ARRAY_SIZE(args
),
312 unsigned long long retval
;
315 args
[0].integer
.value
= timer_id
;
317 pm_runtime_get_sync(dev
);
319 status
= acpi_evaluate_integer(handle
, method
, &arg_list
, &retval
);
321 pm_runtime_put_sync(dev
);
323 if (ACPI_FAILURE(status
))
326 if ((u32
)retval
== ACPI_TAD_WAKE_DISABLED
)
327 return sprintf(buf
, "%s\n", specval
);
329 return sprintf(buf
, "%u\n", (u32
)retval
);
332 static const char *alarm_specval
= "disabled";
334 static int acpi_tad_alarm_write(struct device
*dev
, const char *buf
,
337 return acpi_tad_wake_write(dev
, buf
, "_STV", timer_id
, alarm_specval
);
340 static ssize_t
acpi_tad_alarm_read(struct device
*dev
, char *buf
, u32 timer_id
)
342 return acpi_tad_wake_read(dev
, buf
, "_TIV", timer_id
, alarm_specval
);
345 static const char *policy_specval
= "never";
347 static int acpi_tad_policy_write(struct device
*dev
, const char *buf
,
350 return acpi_tad_wake_write(dev
, buf
, "_STP", timer_id
, policy_specval
);
353 static ssize_t
acpi_tad_policy_read(struct device
*dev
, char *buf
, u32 timer_id
)
355 return acpi_tad_wake_read(dev
, buf
, "_TIP", timer_id
, policy_specval
);
358 static int acpi_tad_clear_status(struct device
*dev
, u32 timer_id
)
360 acpi_handle handle
= ACPI_HANDLE(dev
);
361 union acpi_object args
[] = {
362 { .type
= ACPI_TYPE_INTEGER
, },
364 struct acpi_object_list arg_list
= {
366 .count
= ARRAY_SIZE(args
),
368 unsigned long long retval
;
371 args
[0].integer
.value
= timer_id
;
373 pm_runtime_get_sync(dev
);
375 status
= acpi_evaluate_integer(handle
, "_CWS", &arg_list
, &retval
);
377 pm_runtime_put_sync(dev
);
379 if (ACPI_FAILURE(status
) || retval
)
385 static int acpi_tad_status_write(struct device
*dev
, const char *buf
, u32 timer_id
)
389 ret
= kstrtoint(buf
, 0, &value
);
396 return acpi_tad_clear_status(dev
, timer_id
);
399 static ssize_t
acpi_tad_status_read(struct device
*dev
, char *buf
, u32 timer_id
)
401 acpi_handle handle
= ACPI_HANDLE(dev
);
402 union acpi_object args
[] = {
403 { .type
= ACPI_TYPE_INTEGER
, },
405 struct acpi_object_list arg_list
= {
407 .count
= ARRAY_SIZE(args
),
409 unsigned long long retval
;
412 args
[0].integer
.value
= timer_id
;
414 pm_runtime_get_sync(dev
);
416 status
= acpi_evaluate_integer(handle
, "_GWS", &arg_list
, &retval
);
418 pm_runtime_put_sync(dev
);
420 if (ACPI_FAILURE(status
))
423 return sprintf(buf
, "0x%02X\n", (u32
)retval
);
426 static ssize_t
caps_show(struct device
*dev
, struct device_attribute
*attr
,
429 struct acpi_tad_driver_data
*dd
= dev_get_drvdata(dev
);
431 return sprintf(buf
, "0x%02X\n", dd
->capabilities
);
434 static DEVICE_ATTR_RO(caps
);
436 static ssize_t
ac_alarm_store(struct device
*dev
, struct device_attribute
*attr
,
437 const char *buf
, size_t count
)
439 int ret
= acpi_tad_alarm_write(dev
, buf
, ACPI_TAD_AC_TIMER
);
441 return ret
? ret
: count
;
444 static ssize_t
ac_alarm_show(struct device
*dev
, struct device_attribute
*attr
,
447 return acpi_tad_alarm_read(dev
, buf
, ACPI_TAD_AC_TIMER
);
450 static DEVICE_ATTR_RW(ac_alarm
);
452 static ssize_t
ac_policy_store(struct device
*dev
, struct device_attribute
*attr
,
453 const char *buf
, size_t count
)
455 int ret
= acpi_tad_policy_write(dev
, buf
, ACPI_TAD_AC_TIMER
);
457 return ret
? ret
: count
;
460 static ssize_t
ac_policy_show(struct device
*dev
, struct device_attribute
*attr
,
463 return acpi_tad_policy_read(dev
, buf
, ACPI_TAD_AC_TIMER
);
466 static DEVICE_ATTR_RW(ac_policy
);
468 static ssize_t
ac_status_store(struct device
*dev
, struct device_attribute
*attr
,
469 const char *buf
, size_t count
)
471 int ret
= acpi_tad_status_write(dev
, buf
, ACPI_TAD_AC_TIMER
);
473 return ret
? ret
: count
;
476 static ssize_t
ac_status_show(struct device
*dev
, struct device_attribute
*attr
,
479 return acpi_tad_status_read(dev
, buf
, ACPI_TAD_AC_TIMER
);
482 static DEVICE_ATTR_RW(ac_status
);
484 static struct attribute
*acpi_tad_attrs
[] = {
486 &dev_attr_ac_alarm
.attr
,
487 &dev_attr_ac_policy
.attr
,
488 &dev_attr_ac_status
.attr
,
491 static const struct attribute_group acpi_tad_attr_group
= {
492 .attrs
= acpi_tad_attrs
,
495 static ssize_t
dc_alarm_store(struct device
*dev
, struct device_attribute
*attr
,
496 const char *buf
, size_t count
)
498 int ret
= acpi_tad_alarm_write(dev
, buf
, ACPI_TAD_DC_TIMER
);
500 return ret
? ret
: count
;
503 static ssize_t
dc_alarm_show(struct device
*dev
, struct device_attribute
*attr
,
506 return acpi_tad_alarm_read(dev
, buf
, ACPI_TAD_DC_TIMER
);
509 static DEVICE_ATTR_RW(dc_alarm
);
511 static ssize_t
dc_policy_store(struct device
*dev
, struct device_attribute
*attr
,
512 const char *buf
, size_t count
)
514 int ret
= acpi_tad_policy_write(dev
, buf
, ACPI_TAD_DC_TIMER
);
516 return ret
? ret
: count
;
519 static ssize_t
dc_policy_show(struct device
*dev
, struct device_attribute
*attr
,
522 return acpi_tad_policy_read(dev
, buf
, ACPI_TAD_DC_TIMER
);
525 static DEVICE_ATTR_RW(dc_policy
);
527 static ssize_t
dc_status_store(struct device
*dev
, struct device_attribute
*attr
,
528 const char *buf
, size_t count
)
530 int ret
= acpi_tad_status_write(dev
, buf
, ACPI_TAD_DC_TIMER
);
532 return ret
? ret
: count
;
535 static ssize_t
dc_status_show(struct device
*dev
, struct device_attribute
*attr
,
538 return acpi_tad_status_read(dev
, buf
, ACPI_TAD_DC_TIMER
);
541 static DEVICE_ATTR_RW(dc_status
);
543 static struct attribute
*acpi_tad_dc_attrs
[] = {
544 &dev_attr_dc_alarm
.attr
,
545 &dev_attr_dc_policy
.attr
,
546 &dev_attr_dc_status
.attr
,
549 static const struct attribute_group acpi_tad_dc_attr_group
= {
550 .attrs
= acpi_tad_dc_attrs
,
553 static int acpi_tad_disable_timer(struct device
*dev
, u32 timer_id
)
555 return acpi_tad_wake_set(dev
, "_STV", timer_id
, ACPI_TAD_WAKE_DISABLED
);
558 static void acpi_tad_remove(struct platform_device
*pdev
)
560 struct device
*dev
= &pdev
->dev
;
561 acpi_handle handle
= ACPI_HANDLE(dev
);
562 struct acpi_tad_driver_data
*dd
= dev_get_drvdata(dev
);
564 device_init_wakeup(dev
, false);
566 pm_runtime_get_sync(dev
);
568 if (dd
->capabilities
& ACPI_TAD_DC_WAKE
)
569 sysfs_remove_group(&dev
->kobj
, &acpi_tad_dc_attr_group
);
571 sysfs_remove_group(&dev
->kobj
, &acpi_tad_attr_group
);
573 acpi_tad_disable_timer(dev
, ACPI_TAD_AC_TIMER
);
574 acpi_tad_clear_status(dev
, ACPI_TAD_AC_TIMER
);
575 if (dd
->capabilities
& ACPI_TAD_DC_WAKE
) {
576 acpi_tad_disable_timer(dev
, ACPI_TAD_DC_TIMER
);
577 acpi_tad_clear_status(dev
, ACPI_TAD_DC_TIMER
);
580 pm_runtime_put_sync(dev
);
581 pm_runtime_disable(dev
);
582 acpi_remove_cmos_rtc_space_handler(handle
);
585 static int acpi_tad_probe(struct platform_device
*pdev
)
587 struct device
*dev
= &pdev
->dev
;
588 acpi_handle handle
= ACPI_HANDLE(dev
);
589 struct acpi_tad_driver_data
*dd
;
591 unsigned long long caps
;
594 ret
= acpi_install_cmos_rtc_space_handler(handle
);
596 dev_info(dev
, "Unable to install space handler\n");
600 * Initialization failure messages are mostly about firmware issues, so
601 * print them at the "info" level.
603 status
= acpi_evaluate_integer(handle
, "_GCP", NULL
, &caps
);
604 if (ACPI_FAILURE(status
)) {
605 dev_info(dev
, "Unable to get capabilities\n");
610 if (!(caps
& ACPI_TAD_AC_WAKE
)) {
611 dev_info(dev
, "Unsupported capabilities\n");
616 if (!acpi_has_method(handle
, "_PRW")) {
617 dev_info(dev
, "Missing _PRW\n");
622 dd
= devm_kzalloc(dev
, sizeof(*dd
), GFP_KERNEL
);
628 dd
->capabilities
= caps
;
629 dev_set_drvdata(dev
, dd
);
632 * Assume that the ACPI PM domain has been attached to the device and
633 * simply enable system wakeup and runtime PM and put the device into
634 * runtime suspend. Everything else should be taken care of by the ACPI
635 * PM domain callbacks.
637 device_init_wakeup(dev
, true);
638 dev_pm_set_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
|
639 DPM_FLAG_MAY_SKIP_RESUME
);
641 * The platform bus type layer tells the ACPI PM domain powers up the
642 * device, so set the runtime PM status of it to "active".
644 pm_runtime_set_active(dev
);
645 pm_runtime_enable(dev
);
646 pm_runtime_suspend(dev
);
648 ret
= sysfs_create_group(&dev
->kobj
, &acpi_tad_attr_group
);
652 if (caps
& ACPI_TAD_DC_WAKE
) {
653 ret
= sysfs_create_group(&dev
->kobj
, &acpi_tad_dc_attr_group
);
658 if (caps
& ACPI_TAD_RT
) {
659 ret
= sysfs_create_group(&dev
->kobj
, &acpi_tad_time_attr_group
);
667 acpi_tad_remove(pdev
);
668 /* Don't fallthrough because cmos rtc space handler is removed in acpi_tad_remove() */
672 acpi_remove_cmos_rtc_space_handler(handle
);
676 static const struct acpi_device_id acpi_tad_ids
[] = {
681 static struct platform_driver acpi_tad_driver
= {
684 .acpi_match_table
= acpi_tad_ids
,
686 .probe
= acpi_tad_probe
,
687 .remove
= acpi_tad_remove
,
689 MODULE_DEVICE_TABLE(acpi
, acpi_tad_ids
);
691 module_platform_driver(acpi_tad_driver
);