1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ACPI Time and Alarm (TAD) Device Driver
4 *
5 * Copyright (C) 2018 - 2026 Intel Corporation
6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 *
8 * This driver is based on ACPI 6.6, Section 9.17.
9 *
10 * Provided are sysfs attributes, available under the TAD platform device,
11 * allowing user space to manage the AC and DC wakeup timers of the TAD:
12 * set and read their values, set and check their expire timer wake policies,
13 * check and clear their status and check the capabilities of the TAD reported
14 * by AML. The DC timer attributes are only present if the TAD supports a
15 * separate DC alarm timer.
16 *
17 * The wakeup events handling and power management of the TAD is expected to
18 * be taken care of by the ACPI PM domain attached to its platform device.
19 *
20 * If the TAD supports the get/set real time features, as indicated by the
21 * capability mask returned by _GCP under the TAD object, additional sysfs
22 * attributes are created allowing the real time to be set and read and an RTC
23 * class device is registered under the TAD platform device.
24 */
25
26 #include <linux/acpi.h>
27 #include <linux/kernel.h>
28 #include <linux/ktime.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/platform_device.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/rtc.h>
34 #include <linux/suspend.h>
35
36 MODULE_DESCRIPTION("ACPI Time and Alarm (TAD) Device Driver");
37 MODULE_LICENSE("GPL v2");
38 MODULE_AUTHOR("Rafael J. Wysocki");
39
40 /* ACPI TAD capability flags (ACPI 6.6, Section 9.17.2) */
41 #define ACPI_TAD_AC_WAKE BIT(0)
42 #define ACPI_TAD_DC_WAKE BIT(1)
43 #define ACPI_TAD_RT BIT(2)
44 #define ACPI_TAD_RT_IN_MS BIT(3)
45 #define ACPI_TAD_S4_S5__GWS BIT(4)
46 #define ACPI_TAD_AC_S4_WAKE BIT(5)
47 #define ACPI_TAD_AC_S5_WAKE BIT(6)
48 #define ACPI_TAD_DC_S4_WAKE BIT(7)
49 #define ACPI_TAD_DC_S5_WAKE BIT(8)
50
51 /* ACPI TAD alarm timer selection */
52 #define ACPI_TAD_AC_TIMER (u32)0
53 #define ACPI_TAD_DC_TIMER (u32)1
54
55 /* Special value for disabled timer or expired timer wake policy. */
56 #define ACPI_TAD_WAKE_DISABLED (~(u32)0)
57
58 /* ACPI TAD RTC */
59 #define ACPI_TAD_TZ_UNSPEC 2047
60 #define ACPI_TAD_TIME_ISDST 3
61
62 struct acpi_tad_driver_data {
63 u32 capabilities;
64 };
65
66 struct acpi_tad_rt {
67 u16 year; /* 1900 - 9999 */
68 u8 month; /* 1 - 12 */
69 u8 day; /* 1 - 31 */
70 u8 hour; /* 0 - 23 */
71 u8 minute; /* 0 - 59 */
72 u8 second; /* 0 - 59 */
73 u8 valid; /* 0 (failed) or 1 (success) for reads, 0 for writes */
74 u16 msec; /* 1 - 1000 */
75 s16 tz; /* -1440 to 1440 or 2047 (unspecified) */
76 u8 daylight;
77 u8 padding[3]; /* must be 0 */
78 } __packed;
79
acpi_tad_rt_is_invalid(struct acpi_tad_rt * rt)80 static bool acpi_tad_rt_is_invalid(struct acpi_tad_rt *rt)
81 {
82 return 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 ||
86 (rt->tz > 1440 && rt->tz != ACPI_TAD_TZ_UNSPEC) ||
87 rt->daylight > 3;
88 }
89
90 static DEFINE_MUTEX(acpi_tad_aml_lock);
91
acpi_tad_set_real_time(struct device * dev,struct acpi_tad_rt * rt)92 static int acpi_tad_set_real_time(struct device *dev, struct acpi_tad_rt *rt)
93 {
94 acpi_handle handle = ACPI_HANDLE(dev);
95 union acpi_object args[] = {
96 { .type = ACPI_TYPE_BUFFER, },
97 };
98 struct acpi_object_list arg_list = {
99 .pointer = args,
100 .count = ARRAY_SIZE(args),
101 };
102 unsigned long long retval;
103 acpi_status status;
104
105 if (acpi_tad_rt_is_invalid(rt))
106 return -EINVAL;
107
108 rt->valid = 0;
109 rt->msec = 0;
110 memset(rt->padding, 0, 3);
111
112 args[0].buffer.pointer = (u8 *)rt;
113 args[0].buffer.length = sizeof(*rt);
114
115 PM_RUNTIME_ACQUIRE(dev, pm);
116 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
117 return -ENXIO;
118
119 guard(mutex)(&acpi_tad_aml_lock);
120
121 status = acpi_evaluate_integer(handle, "_SRT", &arg_list, &retval);
122 if (ACPI_FAILURE(status) || retval)
123 return -EIO;
124
125 return 0;
126 }
127
acpi_tad_evaluate_grt(struct device * dev,struct acpi_tad_rt * rt)128 static int acpi_tad_evaluate_grt(struct device *dev, struct acpi_tad_rt *rt)
129 {
130 acpi_handle handle = ACPI_HANDLE(dev);
131 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER };
132 acpi_status status;
133 int ret = -EIO;
134
135 guard(mutex)(&acpi_tad_aml_lock);
136
137 status = acpi_evaluate_object(handle, "_GRT", NULL, &output);
138 if (ACPI_SUCCESS(status)) {
139 union acpi_object *out_obj;
140
141 out_obj = output.pointer;
142 if (out_obj->type == ACPI_TYPE_BUFFER &&
143 out_obj->buffer.length == sizeof(*rt)) {
144 struct acpi_tad_rt *data;
145
146 data = (struct acpi_tad_rt *)(out_obj->buffer.pointer);
147 if (data->valid) {
148 memcpy(rt, data, sizeof(*rt));
149 ret = 0;
150 }
151 }
152 }
153 ACPI_FREE(output.pointer);
154 return ret;
155 }
156
__acpi_tad_get_real_time(struct device * dev,struct acpi_tad_rt * rt)157 static int __acpi_tad_get_real_time(struct device *dev, struct acpi_tad_rt *rt)
158 {
159 int ret;
160
161 ret = acpi_tad_evaluate_grt(dev, rt);
162 if (ret)
163 return ret;
164
165 if (acpi_tad_rt_is_invalid(rt))
166 return -ENODATA;
167
168 return 0;
169 }
170
acpi_tad_get_real_time(struct device * dev,struct acpi_tad_rt * rt)171 static int acpi_tad_get_real_time(struct device *dev, struct acpi_tad_rt *rt)
172 {
173 PM_RUNTIME_ACQUIRE(dev, pm);
174 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
175 return -ENXIO;
176
177 return __acpi_tad_get_real_time(dev, rt);
178 }
179
__acpi_tad_wake_set(struct device * dev,char * method,u32 timer_id,u32 value)180 static int __acpi_tad_wake_set(struct device *dev, char *method, u32 timer_id,
181 u32 value)
182 {
183 acpi_handle handle = ACPI_HANDLE(dev);
184 union acpi_object args[] = {
185 { .type = ACPI_TYPE_INTEGER, },
186 { .type = ACPI_TYPE_INTEGER, },
187 };
188 struct acpi_object_list arg_list = {
189 .pointer = args,
190 .count = ARRAY_SIZE(args),
191 };
192 unsigned long long retval;
193 acpi_status status;
194
195 args[0].integer.value = timer_id;
196 args[1].integer.value = value;
197
198 guard(mutex)(&acpi_tad_aml_lock);
199
200 status = acpi_evaluate_integer(handle, method, &arg_list, &retval);
201 if (ACPI_FAILURE(status) || retval)
202 return -EIO;
203
204 return 0;
205 }
206
__acpi_tad_wake_read(struct device * dev,char * method,u32 timer_id,unsigned long long * retval)207 static int __acpi_tad_wake_read(struct device *dev, char *method, u32 timer_id,
208 unsigned long long *retval)
209 {
210 acpi_handle handle = ACPI_HANDLE(dev);
211 union acpi_object args[] = {
212 { .type = ACPI_TYPE_INTEGER, },
213 };
214 struct acpi_object_list arg_list = {
215 .pointer = args,
216 .count = ARRAY_SIZE(args),
217 };
218 acpi_status status;
219
220 args[0].integer.value = timer_id;
221
222 guard(mutex)(&acpi_tad_aml_lock);
223
224 status = acpi_evaluate_integer(handle, method, &arg_list, retval);
225 if (ACPI_FAILURE(status))
226 return -EIO;
227
228 return 0;
229 }
230
231 /* sysfs interface */
232
acpi_tad_rt_next_field(char * s,int * val)233 static char *acpi_tad_rt_next_field(char *s, int *val)
234 {
235 char *p;
236
237 p = strchr(s, ':');
238 if (!p)
239 return NULL;
240
241 *p = '\0';
242 if (kstrtoint(s, 10, val))
243 return NULL;
244
245 return p + 1;
246 }
247
time_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)248 static ssize_t time_store(struct device *dev, struct device_attribute *attr,
249 const char *buf, size_t count)
250 {
251 struct acpi_tad_rt rt;
252 int val, ret;
253 char *s;
254
255 char *str __free(kfree) = kmemdup_nul(buf, count, GFP_KERNEL);
256 if (!str)
257 return -ENOMEM;
258
259 s = acpi_tad_rt_next_field(str, &val);
260 if (!s)
261 return -ENODATA;
262
263 rt.year = val;
264
265 s = acpi_tad_rt_next_field(s, &val);
266 if (!s)
267 return -ENODATA;
268
269 rt.month = val;
270
271 s = acpi_tad_rt_next_field(s, &val);
272 if (!s)
273 return -ENODATA;
274
275 rt.day = val;
276
277 s = acpi_tad_rt_next_field(s, &val);
278 if (!s)
279 return -ENODATA;
280
281 rt.hour = val;
282
283 s = acpi_tad_rt_next_field(s, &val);
284 if (!s)
285 return -ENODATA;
286
287 rt.minute = val;
288
289 s = acpi_tad_rt_next_field(s, &val);
290 if (!s)
291 return -ENODATA;
292
293 rt.second = val;
294
295 s = acpi_tad_rt_next_field(s, &val);
296 if (!s)
297 return -ENODATA;
298
299 rt.tz = val;
300
301 if (kstrtoint(s, 10, &val))
302 return -ENODATA;
303
304 rt.daylight = val;
305
306 ret = acpi_tad_set_real_time(dev, &rt);
307 if (ret)
308 return ret;
309
310 return count;
311 }
312
time_show(struct device * dev,struct device_attribute * attr,char * buf)313 static ssize_t time_show(struct device *dev, struct device_attribute *attr,
314 char *buf)
315 {
316 struct acpi_tad_rt rt;
317 int ret;
318
319 ret = acpi_tad_get_real_time(dev, &rt);
320 if (ret)
321 return ret;
322
323 return sysfs_emit(buf, "%u:%u:%u:%u:%u:%u:%d:%u\n",
324 rt.year, rt.month, rt.day, rt.hour, rt.minute, rt.second,
325 rt.tz, rt.daylight);
326 }
327
328 static DEVICE_ATTR_RW(time);
329
acpi_tad_wake_set(struct device * dev,char * method,u32 timer_id,u32 value)330 static int acpi_tad_wake_set(struct device *dev, char *method, u32 timer_id,
331 u32 value)
332 {
333 PM_RUNTIME_ACQUIRE(dev, pm);
334 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
335 return -ENXIO;
336
337 return __acpi_tad_wake_set(dev, method, timer_id, value);
338 }
339
acpi_tad_wake_write(struct device * dev,const char * buf,char * method,u32 timer_id,const char * specval)340 static int acpi_tad_wake_write(struct device *dev, const char *buf, char *method,
341 u32 timer_id, const char *specval)
342 {
343 u32 value;
344
345 if (sysfs_streq(buf, specval)) {
346 value = ACPI_TAD_WAKE_DISABLED;
347 } else {
348 int ret = kstrtou32(buf, 0, &value);
349
350 if (ret)
351 return ret;
352
353 if (value == ACPI_TAD_WAKE_DISABLED)
354 return -EINVAL;
355 }
356
357 return acpi_tad_wake_set(dev, method, timer_id, value);
358 }
359
acpi_tad_wake_read(struct device * dev,char * buf,char * method,u32 timer_id,const char * specval)360 static ssize_t acpi_tad_wake_read(struct device *dev, char *buf, char *method,
361 u32 timer_id, const char *specval)
362 {
363 unsigned long long retval;
364 int ret;
365
366 PM_RUNTIME_ACQUIRE(dev, pm);
367 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
368 return -ENXIO;
369
370 ret = __acpi_tad_wake_read(dev, method, timer_id, &retval);
371 if (ret)
372 return ret;
373
374 if ((u32)retval == ACPI_TAD_WAKE_DISABLED)
375 return sprintf(buf, "%s\n", specval);
376
377 return sprintf(buf, "%u\n", (u32)retval);
378 }
379
380 static const char *alarm_specval = "disabled";
381
acpi_tad_alarm_write(struct device * dev,const char * buf,u32 timer_id)382 static int acpi_tad_alarm_write(struct device *dev, const char *buf,
383 u32 timer_id)
384 {
385 return acpi_tad_wake_write(dev, buf, "_STV", timer_id, alarm_specval);
386 }
387
acpi_tad_alarm_read(struct device * dev,char * buf,u32 timer_id)388 static ssize_t acpi_tad_alarm_read(struct device *dev, char *buf, u32 timer_id)
389 {
390 return acpi_tad_wake_read(dev, buf, "_TIV", timer_id, alarm_specval);
391 }
392
393 static const char *policy_specval = "never";
394
acpi_tad_policy_write(struct device * dev,const char * buf,u32 timer_id)395 static int acpi_tad_policy_write(struct device *dev, const char *buf,
396 u32 timer_id)
397 {
398 return acpi_tad_wake_write(dev, buf, "_STP", timer_id, policy_specval);
399 }
400
acpi_tad_policy_read(struct device * dev,char * buf,u32 timer_id)401 static ssize_t acpi_tad_policy_read(struct device *dev, char *buf, u32 timer_id)
402 {
403 return acpi_tad_wake_read(dev, buf, "_TIP", timer_id, policy_specval);
404 }
405
acpi_tad_clear_status(struct device * dev,u32 timer_id)406 static int acpi_tad_clear_status(struct device *dev, u32 timer_id)
407 {
408 acpi_handle handle = ACPI_HANDLE(dev);
409 union acpi_object args[] = {
410 { .type = ACPI_TYPE_INTEGER, },
411 };
412 struct acpi_object_list arg_list = {
413 .pointer = args,
414 .count = ARRAY_SIZE(args),
415 };
416 unsigned long long retval;
417 acpi_status status;
418
419 args[0].integer.value = timer_id;
420
421 PM_RUNTIME_ACQUIRE(dev, pm);
422 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
423 return -ENXIO;
424
425 guard(mutex)(&acpi_tad_aml_lock);
426
427 status = acpi_evaluate_integer(handle, "_CWS", &arg_list, &retval);
428 if (ACPI_FAILURE(status) || retval)
429 return -EIO;
430
431 return 0;
432 }
433
acpi_tad_status_write(struct device * dev,const char * buf,u32 timer_id)434 static int acpi_tad_status_write(struct device *dev, const char *buf, u32 timer_id)
435 {
436 int ret, value;
437
438 ret = kstrtoint(buf, 0, &value);
439 if (ret)
440 return ret;
441
442 if (value)
443 return -EINVAL;
444
445 return acpi_tad_clear_status(dev, timer_id);
446 }
447
acpi_tad_status_read(struct device * dev,char * buf,u32 timer_id)448 static ssize_t acpi_tad_status_read(struct device *dev, char *buf, u32 timer_id)
449 {
450 acpi_handle handle = ACPI_HANDLE(dev);
451 union acpi_object args[] = {
452 { .type = ACPI_TYPE_INTEGER, },
453 };
454 struct acpi_object_list arg_list = {
455 .pointer = args,
456 .count = ARRAY_SIZE(args),
457 };
458 unsigned long long retval;
459 acpi_status status;
460
461 args[0].integer.value = timer_id;
462
463 PM_RUNTIME_ACQUIRE(dev, pm);
464 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
465 return -ENXIO;
466
467 guard(mutex)(&acpi_tad_aml_lock);
468
469 status = acpi_evaluate_integer(handle, "_GWS", &arg_list, &retval);
470 if (ACPI_FAILURE(status))
471 return -EIO;
472
473 return sprintf(buf, "0x%02X\n", (u32)retval);
474 }
475
caps_show(struct device * dev,struct device_attribute * attr,char * buf)476 static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
477 char *buf)
478 {
479 struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
480
481 return sysfs_emit(buf, "0x%02X\n", dd->capabilities);
482 }
483
484 static DEVICE_ATTR_RO(caps);
485
ac_alarm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)486 static ssize_t ac_alarm_store(struct device *dev, struct device_attribute *attr,
487 const char *buf, size_t count)
488 {
489 int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_AC_TIMER);
490
491 return ret ? ret : count;
492 }
493
ac_alarm_show(struct device * dev,struct device_attribute * attr,char * buf)494 static ssize_t ac_alarm_show(struct device *dev, struct device_attribute *attr,
495 char *buf)
496 {
497 return acpi_tad_alarm_read(dev, buf, ACPI_TAD_AC_TIMER);
498 }
499
500 static DEVICE_ATTR_RW(ac_alarm);
501
ac_policy_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)502 static ssize_t ac_policy_store(struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t count)
504 {
505 int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_AC_TIMER);
506
507 return ret ? ret : count;
508 }
509
ac_policy_show(struct device * dev,struct device_attribute * attr,char * buf)510 static ssize_t ac_policy_show(struct device *dev, struct device_attribute *attr,
511 char *buf)
512 {
513 return acpi_tad_policy_read(dev, buf, ACPI_TAD_AC_TIMER);
514 }
515
516 static DEVICE_ATTR_RW(ac_policy);
517
ac_status_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)518 static ssize_t ac_status_store(struct device *dev, struct device_attribute *attr,
519 const char *buf, size_t count)
520 {
521 int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_AC_TIMER);
522
523 return ret ? ret : count;
524 }
525
ac_status_show(struct device * dev,struct device_attribute * attr,char * buf)526 static ssize_t ac_status_show(struct device *dev, struct device_attribute *attr,
527 char *buf)
528 {
529 return acpi_tad_status_read(dev, buf, ACPI_TAD_AC_TIMER);
530 }
531
532 static DEVICE_ATTR_RW(ac_status);
533
dc_alarm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)534 static ssize_t dc_alarm_store(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
536 {
537 int ret = acpi_tad_alarm_write(dev, buf, ACPI_TAD_DC_TIMER);
538
539 return ret ? ret : count;
540 }
541
dc_alarm_show(struct device * dev,struct device_attribute * attr,char * buf)542 static ssize_t dc_alarm_show(struct device *dev, struct device_attribute *attr,
543 char *buf)
544 {
545 return acpi_tad_alarm_read(dev, buf, ACPI_TAD_DC_TIMER);
546 }
547
548 static DEVICE_ATTR_RW(dc_alarm);
549
dc_policy_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)550 static ssize_t dc_policy_store(struct device *dev, struct device_attribute *attr,
551 const char *buf, size_t count)
552 {
553 int ret = acpi_tad_policy_write(dev, buf, ACPI_TAD_DC_TIMER);
554
555 return ret ? ret : count;
556 }
557
dc_policy_show(struct device * dev,struct device_attribute * attr,char * buf)558 static ssize_t dc_policy_show(struct device *dev, struct device_attribute *attr,
559 char *buf)
560 {
561 return acpi_tad_policy_read(dev, buf, ACPI_TAD_DC_TIMER);
562 }
563
564 static DEVICE_ATTR_RW(dc_policy);
565
dc_status_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)566 static ssize_t dc_status_store(struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
568 {
569 int ret = acpi_tad_status_write(dev, buf, ACPI_TAD_DC_TIMER);
570
571 return ret ? ret : count;
572 }
573
dc_status_show(struct device * dev,struct device_attribute * attr,char * buf)574 static ssize_t dc_status_show(struct device *dev, struct device_attribute *attr,
575 char *buf)
576 {
577 return acpi_tad_status_read(dev, buf, ACPI_TAD_DC_TIMER);
578 }
579
580 static DEVICE_ATTR_RW(dc_status);
581
582 static struct attribute *acpi_tad_attrs[] = {
583 &dev_attr_caps.attr,
584 &dev_attr_ac_alarm.attr,
585 &dev_attr_ac_policy.attr,
586 &dev_attr_ac_status.attr,
587 &dev_attr_dc_alarm.attr,
588 &dev_attr_dc_policy.attr,
589 &dev_attr_dc_status.attr,
590 &dev_attr_time.attr,
591 NULL,
592 };
593
acpi_tad_attr_is_visible(struct kobject * kobj,struct attribute * a,int n)594 static umode_t acpi_tad_attr_is_visible(struct kobject *kobj,
595 struct attribute *a, int n)
596 {
597 struct acpi_tad_driver_data *dd = dev_get_drvdata(kobj_to_dev(kobj));
598
599 if (a == &dev_attr_caps.attr)
600 return a->mode;
601
602 if ((dd->capabilities & ACPI_TAD_AC_WAKE) &&
603 (a == &dev_attr_ac_alarm.attr || a == &dev_attr_ac_policy.attr ||
604 a == &dev_attr_ac_status.attr))
605 return a->mode;
606
607 if ((dd->capabilities & ACPI_TAD_DC_WAKE) &&
608 (a == &dev_attr_dc_alarm.attr || a == &dev_attr_dc_policy.attr ||
609 a == &dev_attr_dc_status.attr))
610 return a->mode;
611
612 if ((dd->capabilities & ACPI_TAD_RT) && a == &dev_attr_time.attr)
613 return a->mode;
614
615 return 0;
616 }
617
618 static const struct attribute_group acpi_tad_group = {
619 .attrs = acpi_tad_attrs,
620 .is_visible = acpi_tad_attr_is_visible,
621 };
622
623 __ATTRIBUTE_GROUPS(acpi_tad);
624
625 #ifdef CONFIG_RTC_CLASS
626 /* RTC class device interface */
627
acpi_tad_rt_to_tm(struct acpi_tad_rt * rt,struct rtc_time * tm)628 static void acpi_tad_rt_to_tm(struct acpi_tad_rt *rt, struct rtc_time *tm)
629 {
630 tm->tm_year = rt->year - 1900;
631 tm->tm_mon = rt->month - 1;
632 tm->tm_mday = rt->day;
633 tm->tm_hour = rt->hour;
634 tm->tm_min = rt->minute;
635 tm->tm_sec = rt->second;
636 tm->tm_isdst = rt->daylight == ACPI_TAD_TIME_ISDST;
637 }
638
acpi_tad_rtc_set_time(struct device * dev,struct rtc_time * tm)639 static int acpi_tad_rtc_set_time(struct device *dev, struct rtc_time *tm)
640 {
641 struct acpi_tad_rt rt;
642
643 rt.year = tm->tm_year + 1900;
644 rt.month = tm->tm_mon + 1;
645 rt.day = tm->tm_mday;
646 rt.hour = tm->tm_hour;
647 rt.minute = tm->tm_min;
648 rt.second = tm->tm_sec;
649 rt.tz = ACPI_TAD_TZ_UNSPEC;
650 rt.daylight = ACPI_TAD_TIME_ISDST * !!tm->tm_isdst;
651
652 return acpi_tad_set_real_time(dev, &rt);
653 }
654
acpi_tad_rtc_read_time(struct device * dev,struct rtc_time * tm)655 static int acpi_tad_rtc_read_time(struct device *dev, struct rtc_time *tm)
656 {
657 struct acpi_tad_rt rt;
658 int ret;
659
660 ret = acpi_tad_get_real_time(dev, &rt);
661 if (ret)
662 return ret;
663
664 acpi_tad_rt_to_tm(&rt, tm);
665
666 return 0;
667 }
668
acpi_tad_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * t)669 static int acpi_tad_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
670 {
671 struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
672 s64 value = ACPI_TAD_WAKE_DISABLED;
673 struct rtc_time tm_now;
674 struct acpi_tad_rt rt;
675 int ret;
676
677 PM_RUNTIME_ACQUIRE(dev, pm);
678 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
679 return -ENXIO;
680
681 if (t->enabled) {
682 /*
683 * The value to pass to _STV is expected to be the number of
684 * seconds between the time when the timer is programmed and the
685 * time when it expires represented as a 32-bit integer.
686 */
687 ret = __acpi_tad_get_real_time(dev, &rt);
688 if (ret)
689 return ret;
690
691 acpi_tad_rt_to_tm(&rt, &tm_now);
692
693 value = rtc_tm_to_time64(&t->time) - rtc_tm_to_time64(&tm_now);
694 if (value <= 0 || value >= U32_MAX)
695 return -EINVAL;
696 }
697
698 ret = __acpi_tad_wake_set(dev, "_STV", ACPI_TAD_AC_TIMER, value);
699 if (ret && t->enabled)
700 return ret;
701
702 /*
703 * If a separate DC alarm timer is supported, set it to the same value
704 * as the AC alarm timer.
705 */
706 if (dd->capabilities & ACPI_TAD_DC_WAKE) {
707 ret = __acpi_tad_wake_set(dev, "_STV", ACPI_TAD_DC_TIMER, value);
708 if (ret && t->enabled) {
709 __acpi_tad_wake_set(dev, "_STV", ACPI_TAD_AC_TIMER,
710 ACPI_TAD_WAKE_DISABLED);
711 return ret;
712 }
713 }
714
715 /* Assume success if the alarm is being disabled. */
716 return 0;
717 }
718
acpi_tad_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * t)719 static int acpi_tad_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
720 {
721 unsigned long long retval;
722 struct rtc_time tm_now;
723 struct acpi_tad_rt rt;
724 int ret;
725
726 PM_RUNTIME_ACQUIRE(dev, pm);
727 if (PM_RUNTIME_ACQUIRE_ERR(&pm))
728 return -ENXIO;
729
730 ret = __acpi_tad_get_real_time(dev, &rt);
731 if (ret)
732 return ret;
733
734 acpi_tad_rt_to_tm(&rt, &tm_now);
735
736 /*
737 * Assume that the alarm was set by acpi_tad_rtc_set_alarm(), so the AC
738 * and DC alarm timer settings are the same and it is sufficient to read
739 * the former.
740 *
741 * The value returned by _TIV should be the number of seconds till the
742 * expiration of the timer, represented as a 32-bit integer, or the
743 * special ACPI_TAD_WAKE_DISABLED value meaning that the timer has
744 * been disabled.
745 */
746 ret = __acpi_tad_wake_read(dev, "_TIV", ACPI_TAD_AC_TIMER, &retval);
747 if (ret)
748 return ret;
749
750 if (retval > U32_MAX)
751 return -ENODATA;
752
753 t->pending = 0;
754
755 if (retval != ACPI_TAD_WAKE_DISABLED) {
756 t->enabled = 1;
757 rtc_time64_to_tm(rtc_tm_to_time64(&tm_now) + retval, &t->time);
758 } else {
759 t->enabled = 0;
760 t->time = tm_now;
761 }
762
763 return 0;
764 }
765
766 static const struct rtc_class_ops acpi_tad_rtc_ops = {
767 .read_time = acpi_tad_rtc_read_time,
768 .set_time = acpi_tad_rtc_set_time,
769 .set_alarm = acpi_tad_rtc_set_alarm,
770 .read_alarm = acpi_tad_rtc_read_alarm,
771 };
772
acpi_tad_register_rtc(struct device * dev,unsigned long long caps)773 static void acpi_tad_register_rtc(struct device *dev, unsigned long long caps)
774 {
775 struct rtc_device *rtc;
776
777 rtc = devm_rtc_allocate_device(dev);
778 if (IS_ERR(rtc))
779 return;
780
781 rtc->range_min = mktime64(1900, 1, 1, 0, 0, 0);
782 rtc->range_max = mktime64(9999, 12, 31, 23, 59, 59);
783
784 rtc->ops = &acpi_tad_rtc_ops;
785
786 if (!(caps & ACPI_TAD_AC_WAKE))
787 clear_bit(RTC_FEATURE_ALARM, rtc->features);
788
789 devm_rtc_register_device(rtc);
790 }
791 #else /* !CONFIG_RTC_CLASS */
acpi_tad_register_rtc(struct device * dev,unsigned long long caps)792 static inline void acpi_tad_register_rtc(struct device *dev,
793 unsigned long long caps) {}
794 #endif /* !CONFIG_RTC_CLASS */
795
796 /* Platform driver interface */
797
acpi_tad_disable_timer(struct device * dev,u32 timer_id)798 static int acpi_tad_disable_timer(struct device *dev, u32 timer_id)
799 {
800 return acpi_tad_wake_set(dev, "_STV", timer_id, ACPI_TAD_WAKE_DISABLED);
801 }
802
acpi_tad_remove(void * data)803 static void acpi_tad_remove(void *data)
804 {
805 struct device *dev = data;
806 struct acpi_tad_driver_data *dd = dev_get_drvdata(dev);
807
808 device_init_wakeup(dev, false);
809
810 scoped_guard(pm_runtime_noresume, dev) {
811 if (dd->capabilities & ACPI_TAD_AC_WAKE) {
812 acpi_tad_disable_timer(dev, ACPI_TAD_AC_TIMER);
813 acpi_tad_clear_status(dev, ACPI_TAD_AC_TIMER);
814 }
815 if (dd->capabilities & ACPI_TAD_DC_WAKE) {
816 acpi_tad_disable_timer(dev, ACPI_TAD_DC_TIMER);
817 acpi_tad_clear_status(dev, ACPI_TAD_DC_TIMER);
818 }
819 }
820
821 pm_runtime_suspend(dev);
822 pm_runtime_disable(dev);
823 }
824
acpi_tad_probe(struct platform_device * pdev)825 static int acpi_tad_probe(struct platform_device *pdev)
826 {
827 struct device *dev = &pdev->dev;
828 struct acpi_tad_driver_data *dd;
829 acpi_handle handle;
830 acpi_status status;
831 unsigned long long caps;
832 int ret;
833
834 handle = ACPI_HANDLE(dev);
835 if (!handle)
836 return -ENODEV;
837
838 /*
839 * Initialization failure messages are mostly about firmware issues, so
840 * print them at the "info" level.
841 */
842 status = acpi_evaluate_integer(handle, "_GCP", NULL, &caps);
843 if (ACPI_FAILURE(status)) {
844 dev_info(dev, "Unable to get capabilities\n");
845 return -ENODEV;
846 }
847
848 if (!acpi_has_method(handle, "_PRW")) {
849 dev_info(dev, "Missing _PRW\n");
850 caps &= ~(ACPI_TAD_AC_WAKE | ACPI_TAD_DC_WAKE);
851 }
852
853 if (!(caps & ACPI_TAD_AC_WAKE))
854 caps &= ~ACPI_TAD_DC_WAKE;
855
856 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
857 if (!dd)
858 return -ENOMEM;
859
860 dd->capabilities = caps;
861 dev_set_drvdata(dev, dd);
862
863 /*
864 * Assume that the ACPI PM domain has been attached to the device and
865 * simply enable system wakeup and runtime PM and put the device into
866 * runtime suspend. Everything else should be taken care of by the ACPI
867 * PM domain callbacks.
868 */
869 if (caps & ACPI_TAD_AC_WAKE) {
870 device_init_wakeup(dev, true);
871 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
872 DPM_FLAG_MAY_SKIP_RESUME);
873 }
874
875 /*
876 * The platform bus type probe callback tells the ACPI PM domain to
877 * power up the device, so set the runtime PM status of it to "active".
878 */
879 pm_runtime_set_active(dev);
880 pm_runtime_enable(dev);
881 pm_runtime_suspend(dev);
882
883 /*
884 * acpi_tad_remove() needs to run after unregistering the RTC class
885 * device to avoid racing with the latter's callbacks.
886 */
887 ret = devm_add_action_or_reset(&pdev->dev, acpi_tad_remove, &pdev->dev);
888 if (ret)
889 return ret;
890
891 if (caps & ACPI_TAD_RT)
892 acpi_tad_register_rtc(dev, caps);
893
894 return 0;
895 }
896
897 static const struct acpi_device_id acpi_tad_ids[] = {
898 {"ACPI000E", 0},
899 {}
900 };
901
902 static struct platform_driver acpi_tad_driver = {
903 .driver = {
904 .name = "acpi-tad",
905 .acpi_match_table = acpi_tad_ids,
906 .dev_groups = acpi_tad_groups,
907 },
908 .probe = acpi_tad_probe,
909 };
910 MODULE_DEVICE_TABLE(acpi, acpi_tad_ids);
911
912 module_platform_driver(acpi_tad_driver);
913