Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / acpi / acpi_tad.c
blob7d45cce0c3c187ffc33e4f7d3526d181acf1088b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 {
52 u32 capabilities;
55 struct acpi_tad_rt {
56 u16 year; /* 1900 - 9999 */
57 u8 month; /* 1 - 12 */
58 u8 day; /* 1 - 31 */
59 u8 hour; /* 0 - 23 */
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) */
65 u8 daylight;
66 u8 padding[3]; /* must be 0 */
67 } __packed;
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 = {
76 .pointer = args,
77 .count = ARRAY_SIZE(args),
79 unsigned long long retval;
80 acpi_status status;
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) ||
86 rt->daylight > 3)
87 return -ERANGE;
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)
99 return -EIO;
101 return 0;
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;
110 acpi_status status;
111 int ret = -EIO;
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))
120 goto out_free;
122 out_obj = output.pointer;
123 if (out_obj->type != ACPI_TYPE_BUFFER)
124 goto out_free;
126 if (out_obj->buffer.length != sizeof(*rt))
127 goto out_free;
129 data = (struct acpi_tad_rt *)(out_obj->buffer.pointer);
130 if (!data->valid)
131 goto out_free;
133 memcpy(rt, data, sizeof(*rt));
134 ret = 0;
136 out_free:
137 ACPI_FREE(output.pointer);
138 return ret;
141 static char *acpi_tad_rt_next_field(char *s, int *val)
143 char *p;
145 p = strchr(s, ':');
146 if (!p)
147 return NULL;
149 *p = '\0';
150 if (kstrtoint(s, 10, val))
151 return NULL;
153 return p + 1;
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;
160 char *str, *s;
161 int val, ret = -ENODATA;
163 str = kmemdup_nul(buf, count, GFP_KERNEL);
164 if (!str)
165 return -ENOMEM;
167 s = acpi_tad_rt_next_field(str, &val);
168 if (!s)
169 goto out_free;
171 rt.year = val;
173 s = acpi_tad_rt_next_field(s, &val);
174 if (!s)
175 goto out_free;
177 rt.month = val;
179 s = acpi_tad_rt_next_field(s, &val);
180 if (!s)
181 goto out_free;
183 rt.day = val;
185 s = acpi_tad_rt_next_field(s, &val);
186 if (!s)
187 goto out_free;
189 rt.hour = val;
191 s = acpi_tad_rt_next_field(s, &val);
192 if (!s)
193 goto out_free;
195 rt.minute = val;
197 s = acpi_tad_rt_next_field(s, &val);
198 if (!s)
199 goto out_free;
201 rt.second = val;
203 s = acpi_tad_rt_next_field(s, &val);
204 if (!s)
205 goto out_free;
207 rt.tz = val;
209 if (kstrtoint(s, 10, &val))
210 goto out_free;
212 rt.daylight = val;
214 rt.valid = 0;
215 rt.msec = 0;
216 memset(rt.padding, 0, 3);
218 ret = acpi_tad_set_real_time(dev, &rt);
220 out_free:
221 kfree(str);
222 return ret ? ret : count;
225 static ssize_t time_show(struct device *dev, struct device_attribute *attr,
226 char *buf)
228 struct acpi_tad_rt rt;
229 int ret;
231 ret = acpi_tad_get_real_time(dev, &rt);
232 if (ret)
233 return ret;
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,
237 rt.tz, rt.daylight);
240 static DEVICE_ATTR(time, S_IRUSR | S_IWUSR, time_show, time_store);
242 static struct attribute *acpi_tad_time_attrs[] = {
243 &dev_attr_time.attr,
244 NULL,
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,
251 u32 value)
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 = {
259 .pointer = args,
260 .count = ARRAY_SIZE(args),
262 unsigned long long retval;
263 acpi_status status;
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)
275 return -EIO;
277 return 0;
280 static int acpi_tad_wake_write(struct device *dev, const char *buf, char *method,
281 u32 timer_id, const char *specval)
283 u32 value;
285 if (sysfs_streq(buf, specval)) {
286 value = ACPI_TAD_WAKE_DISABLED;
287 } else {
288 int ret = kstrtou32(buf, 0, &value);
290 if (ret)
291 return ret;
293 if (value == ACPI_TAD_WAKE_DISABLED)
294 return -EINVAL;
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 = {
308 .pointer = args,
309 .count = ARRAY_SIZE(args),
311 unsigned long long retval;
312 acpi_status status;
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))
323 return -EIO;
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,
334 u32 timer_id)
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,
347 u32 timer_id)
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 = {
364 .pointer = args,
365 .count = ARRAY_SIZE(args),
367 unsigned long long retval;
368 acpi_status status;
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)
379 return -EIO;
381 return 0;
384 static int acpi_tad_status_write(struct device *dev, const char *buf, u32 timer_id)
386 int ret, value;
388 ret = kstrtoint(buf, 0, &value);
389 if (ret)
390 return ret;
392 if (value)
393 return -EINVAL;
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 = {
405 .pointer = args,
406 .count = ARRAY_SIZE(args),
408 unsigned long long retval;
409 acpi_status status;
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))
420 return -EIO;
422 return sprintf(buf, "0x%02X\n", (u32)retval);
425 static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
426 char *buf)
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,
444 char *buf)
446 return acpi_tad_alarm_read(dev, buf, ACPI_TAD_AC_TIMER);
449 static DEVICE_ATTR(ac_alarm, S_IRUSR | S_IWUSR, ac_alarm_show, ac_alarm_store);
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,
460 char *buf)
462 return acpi_tad_policy_read(dev, buf, ACPI_TAD_AC_TIMER);
465 static DEVICE_ATTR(ac_policy, S_IRUSR | S_IWUSR, ac_policy_show, ac_policy_store);
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,
476 char *buf)
478 return acpi_tad_status_read(dev, buf, ACPI_TAD_AC_TIMER);
481 static DEVICE_ATTR(ac_status, S_IRUSR | S_IWUSR, ac_status_show, ac_status_store);
483 static struct attribute *acpi_tad_attrs[] = {
484 &dev_attr_caps.attr,
485 &dev_attr_ac_alarm.attr,
486 &dev_attr_ac_policy.attr,
487 &dev_attr_ac_status.attr,
488 NULL,
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,
503 char *buf)
505 return acpi_tad_alarm_read(dev, buf, ACPI_TAD_DC_TIMER);
508 static DEVICE_ATTR(dc_alarm, S_IRUSR | S_IWUSR, dc_alarm_show, dc_alarm_store);
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,
519 char *buf)
521 return acpi_tad_policy_read(dev, buf, ACPI_TAD_DC_TIMER);
524 static DEVICE_ATTR(dc_policy, S_IRUSR | S_IWUSR, dc_policy_show, dc_policy_store);
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,
535 char *buf)
537 return acpi_tad_status_read(dev, buf, ACPI_TAD_DC_TIMER);
540 static DEVICE_ATTR(dc_status, S_IRUSR | S_IWUSR, dc_status_show, dc_status_store);
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,
546 NULL,
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);
580 return 0;
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;
588 acpi_status status;
589 unsigned long long caps;
590 int ret;
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");
599 return -ENODEV;
602 if (!(caps & ACPI_TAD_AC_WAKE)) {
603 dev_info(dev, "Unsupported capabilities\n");
604 return -ENODEV;
607 if (!acpi_has_method(handle, "_PRW")) {
608 dev_info(dev, "Missing _PRW\n");
609 return -ENODEV;
612 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
613 if (!dd)
614 return -ENOMEM;
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);
637 if (ret)
638 goto fail;
640 if (caps & ACPI_TAD_DC_WAKE) {
641 ret = sysfs_create_group(&dev->kobj, &acpi_tad_dc_attr_group);
642 if (ret)
643 goto fail;
646 if (caps & ACPI_TAD_RT) {
647 ret = sysfs_create_group(&dev->kobj, &acpi_tad_time_attr_group);
648 if (ret)
649 goto fail;
652 return 0;
654 fail:
655 acpi_tad_remove(pdev);
656 return ret;
659 static const struct acpi_device_id acpi_tad_ids[] = {
660 {"ACPI000E", 0},
664 static struct platform_driver acpi_tad_driver = {
665 .driver = {
666 .name = "acpi-tad",
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);