printf: Remove unused 'bprintf'
[drm/drm-misc.git] / drivers / acpi / acpi_tad.c
blob825c2a8acea436be10eb4cf5d78b030aa6ebffb7
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_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 {
53 u32 capabilities;
56 struct acpi_tad_rt {
57 u16 year; /* 1900 - 9999 */
58 u8 month; /* 1 - 12 */
59 u8 day; /* 1 - 31 */
60 u8 hour; /* 0 - 23 */
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) */
66 u8 daylight;
67 u8 padding[3]; /* must be 0 */
68 } __packed;
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 = {
77 .pointer = args,
78 .count = ARRAY_SIZE(args),
80 unsigned long long retval;
81 acpi_status status;
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) ||
87 rt->daylight > 3)
88 return -ERANGE;
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)
100 return -EIO;
102 return 0;
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;
111 acpi_status status;
112 int ret = -EIO;
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))
121 goto out_free;
123 out_obj = output.pointer;
124 if (out_obj->type != ACPI_TYPE_BUFFER)
125 goto out_free;
127 if (out_obj->buffer.length != sizeof(*rt))
128 goto out_free;
130 data = (struct acpi_tad_rt *)(out_obj->buffer.pointer);
131 if (!data->valid)
132 goto out_free;
134 memcpy(rt, data, sizeof(*rt));
135 ret = 0;
137 out_free:
138 ACPI_FREE(output.pointer);
139 return ret;
142 static char *acpi_tad_rt_next_field(char *s, int *val)
144 char *p;
146 p = strchr(s, ':');
147 if (!p)
148 return NULL;
150 *p = '\0';
151 if (kstrtoint(s, 10, val))
152 return NULL;
154 return p + 1;
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;
161 char *str, *s;
162 int val, ret = -ENODATA;
164 str = kmemdup_nul(buf, count, GFP_KERNEL);
165 if (!str)
166 return -ENOMEM;
168 s = acpi_tad_rt_next_field(str, &val);
169 if (!s)
170 goto out_free;
172 rt.year = val;
174 s = acpi_tad_rt_next_field(s, &val);
175 if (!s)
176 goto out_free;
178 rt.month = val;
180 s = acpi_tad_rt_next_field(s, &val);
181 if (!s)
182 goto out_free;
184 rt.day = val;
186 s = acpi_tad_rt_next_field(s, &val);
187 if (!s)
188 goto out_free;
190 rt.hour = val;
192 s = acpi_tad_rt_next_field(s, &val);
193 if (!s)
194 goto out_free;
196 rt.minute = val;
198 s = acpi_tad_rt_next_field(s, &val);
199 if (!s)
200 goto out_free;
202 rt.second = val;
204 s = acpi_tad_rt_next_field(s, &val);
205 if (!s)
206 goto out_free;
208 rt.tz = val;
210 if (kstrtoint(s, 10, &val))
211 goto out_free;
213 rt.daylight = val;
215 rt.valid = 0;
216 rt.msec = 0;
217 memset(rt.padding, 0, 3);
219 ret = acpi_tad_set_real_time(dev, &rt);
221 out_free:
222 kfree(str);
223 return ret ? ret : count;
226 static ssize_t time_show(struct device *dev, struct device_attribute *attr,
227 char *buf)
229 struct acpi_tad_rt rt;
230 int ret;
232 ret = acpi_tad_get_real_time(dev, &rt);
233 if (ret)
234 return ret;
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,
238 rt.tz, rt.daylight);
241 static DEVICE_ATTR_RW(time);
243 static struct attribute *acpi_tad_time_attrs[] = {
244 &dev_attr_time.attr,
245 NULL,
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,
252 u32 value)
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 = {
260 .pointer = args,
261 .count = ARRAY_SIZE(args),
263 unsigned long long retval;
264 acpi_status status;
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)
276 return -EIO;
278 return 0;
281 static int acpi_tad_wake_write(struct device *dev, const char *buf, char *method,
282 u32 timer_id, const char *specval)
284 u32 value;
286 if (sysfs_streq(buf, specval)) {
287 value = ACPI_TAD_WAKE_DISABLED;
288 } else {
289 int ret = kstrtou32(buf, 0, &value);
291 if (ret)
292 return ret;
294 if (value == ACPI_TAD_WAKE_DISABLED)
295 return -EINVAL;
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 = {
309 .pointer = args,
310 .count = ARRAY_SIZE(args),
312 unsigned long long retval;
313 acpi_status status;
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))
324 return -EIO;
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,
335 u32 timer_id)
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,
348 u32 timer_id)
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 = {
365 .pointer = args,
366 .count = ARRAY_SIZE(args),
368 unsigned long long retval;
369 acpi_status status;
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)
380 return -EIO;
382 return 0;
385 static int acpi_tad_status_write(struct device *dev, const char *buf, u32 timer_id)
387 int ret, value;
389 ret = kstrtoint(buf, 0, &value);
390 if (ret)
391 return ret;
393 if (value)
394 return -EINVAL;
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 = {
406 .pointer = args,
407 .count = ARRAY_SIZE(args),
409 unsigned long long retval;
410 acpi_status status;
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))
421 return -EIO;
423 return sprintf(buf, "0x%02X\n", (u32)retval);
426 static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
427 char *buf)
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,
445 char *buf)
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,
461 char *buf)
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,
477 char *buf)
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[] = {
485 &dev_attr_caps.attr,
486 &dev_attr_ac_alarm.attr,
487 &dev_attr_ac_policy.attr,
488 &dev_attr_ac_status.attr,
489 NULL,
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,
504 char *buf)
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,
520 char *buf)
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,
536 char *buf)
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,
547 NULL,
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;
590 acpi_status status;
591 unsigned long long caps;
592 int ret;
594 ret = acpi_install_cmos_rtc_space_handler(handle);
595 if (ret < 0) {
596 dev_info(dev, "Unable to install space handler\n");
597 return -ENODEV;
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");
606 ret = -ENODEV;
607 goto remove_handler;
610 if (!(caps & ACPI_TAD_AC_WAKE)) {
611 dev_info(dev, "Unsupported capabilities\n");
612 ret = -ENODEV;
613 goto remove_handler;
616 if (!acpi_has_method(handle, "_PRW")) {
617 dev_info(dev, "Missing _PRW\n");
618 ret = -ENODEV;
619 goto remove_handler;
622 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
623 if (!dd) {
624 ret = -ENOMEM;
625 goto remove_handler;
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);
649 if (ret)
650 goto fail;
652 if (caps & ACPI_TAD_DC_WAKE) {
653 ret = sysfs_create_group(&dev->kobj, &acpi_tad_dc_attr_group);
654 if (ret)
655 goto fail;
658 if (caps & ACPI_TAD_RT) {
659 ret = sysfs_create_group(&dev->kobj, &acpi_tad_time_attr_group);
660 if (ret)
661 goto fail;
664 return 0;
666 fail:
667 acpi_tad_remove(pdev);
668 /* Don't fallthrough because cmos rtc space handler is removed in acpi_tad_remove() */
669 return ret;
671 remove_handler:
672 acpi_remove_cmos_rtc_space_handler(handle);
673 return ret;
676 static const struct acpi_device_id acpi_tad_ids[] = {
677 {"ACPI000E", 0},
681 static struct platform_driver acpi_tad_driver = {
682 .driver = {
683 .name = "acpi-tad",
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);