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