ACPI: TAD: Add alarm support to RTC class device interface
Subsystems (2)
Rafael J. Wysocki
maintainercover ACPI: TAD: Add alarm support to RTC class device interface
Hi All,
This series adds alarm support via the RTC class device interface of the ACPI time and alarm device (TAD) driver.
The code is first rearranged to facilitate re-use (patches [1-3/4] and then the RTC class interface of the driver is extended to support alarm (patch [4/4]).
Thanks!
Rafael J. Wysocki
maintainerPATCH 1/4 ACPI: TAD: Split three functions to untangle runtime PM handling
From: Rafael J. Wysocki <[email protected]>
Move the core functionality of acpi_tad_get_real_time(), acpi_tad_wake_set(), and acpi_tad_wake_read() into separate functions called __acpi_tad_get_real_time(), __acpi_tad_wake_set(), and __acpi_tad_wake_read(), respectively, which can be called from code blocks following a single runtime resume of the device.
This will facilitate adding alarm support to the RTC class device interface of the driver going forward.
No intentional functional impact.
--- drivers/acpi/acpi_tad.c | 57 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-)
drivers/acpi/acpi_tad.c +40 -17
@@ -151,14 +151,10 @@ out_free:return ret; } -static int acpi_tad_get_real_time (struct device *dev,struct acpi_tad_rt *rt) +static int __acpi_tad_get_real_time (struct device *dev,struct acpi_tad_rt *rt) {int ret; -PM_RUNTIME_ACQUIRE (dev, pm); -if (PM_RUNTIME_ACQUIRE_ERR (&pm)) -return -ENXIO ; - ret =acpi_tad_evaluate_grt (dev, rt);if (ret)return ret;
@@ -169,6 +165,15 @@ static int acpi_tad_get_real_time(structreturn 0 ; } +static int acpi_tad_get_real_time (struct device *dev,struct acpi_tad_rt *rt) +{ +PM_RUNTIME_ACQUIRE (dev, pm); +if (PM_RUNTIME_ACQUIRE_ERR (&pm)) +return -ENXIO ; + +return __acpi_tad_get_real_time (dev, rt); +} +/* sysfs interface */ static char *acpi_tad_rt_next_field (char *s,int *val)
@@ -268,8 +273,8 @@ static ssize_t time_show(struct device *static DEVICE_ATTR_RW (time); -static int acpi_tad_wake_set (struct device *dev,char *method,u32 timer_id, -u32 value) +static int __acpi_tad_wake_set (struct device *dev,char *method,u32 timer_id, +u32 value) {acpi_handle handle =ACPI_HANDLE (dev);union acpi_object args[] = {
@@ -286,10 +291,6 @@ static int acpi_tad_wake_set(struct devi args[0 ].integer.value = timer_id; args[1 ].integer.value = value; -PM_RUNTIME_ACQUIRE (dev, pm); -if (PM_RUNTIME_ACQUIRE_ERR (&pm)) -return -ENXIO ; - status =acpi_evaluate_integer (handle, method, &arg_list, &retval);if (ACPI_FAILURE (status) || retval)return -EIO ;
@@ -297,6 +298,16 @@ static int acpi_tad_wake_set(struct devireturn 0 ; } +static int acpi_tad_wake_set (struct device *dev,char *method,u32 timer_id, +u32 value) +{ +PM_RUNTIME_ACQUIRE (dev, pm); +if (PM_RUNTIME_ACQUIRE_ERR (&pm)) +return -ENXIO ; + +return __acpi_tad_wake_set (dev, method, timer_id, value); +} +static int acpi_tad_wake_write (struct device *dev,const char *buf,char *method,u32 timer_id,const char *specval) {
@@ -317,8 +328,8 @@ static int acpi_tad_wake_write(struct dereturn acpi_tad_wake_set (dev, method, timer_id, value); } -static ssize_t acpi_tad_wake_read (struct device *dev,char *buf,char *method, -u32 timer_id,const char *specval) +static int __acpi_tad_wake_read (struct device *dev,char *method,u32 timer_id, +unsigned long long *retval) {acpi_handle handle =ACPI_HANDLE (dev);union acpi_object args[] = {
@@ -328,18 +339,30 @@ static ssize_t acpi_tad_wake_read(struct .pointer = args, .count = ARRAY_SIZE(args), }; -unsigned long long retval;acpi_status status; args[0 ].integer.value = timer_id; + status =acpi_evaluate_integer (handle, method, &arg_list, retval); +if (ACPI_FAILURE (status)) +return -EIO ; + +return 0 ; +} + +static ssize_t acpi_tad_wake_read (struct device *dev,char *buf,char *method, +u32 timer_id,const char *specval) +{ +unsigned long long retval; +int ret; +PM_RUNTIME_ACQUIRE (dev, pm);if (PM_RUNTIME_ACQUIRE_ERR (&pm))return -ENXIO ; - status =acpi_evaluate_integer (handle, method, &arg_list, &retval); -if (ACPI_FAILURE (status)) -return -EIO ; + ret =__acpi_tad_wake_read (dev, method, timer_id, &retval); +if (ret) +return ret;if ((u32 )retval ==ACPI_TAD_WAKE_DISABLED )return sprintf (buf,"%s\n" , specval);
Rafael J. Wysocki
maintainerPATCH 2/4 ACPI: TAD: Relocate two functions
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Move two functions introduced previously, __acpi_tad_wake_set() and __acpi_tad_wake_read(), to the part of the code preceding the sysfs interface implementation, since subsequently they will be used by the RTC device interface too.
No intentional functional impact.
--- drivers/acpi/acpi_tad.c | 94 ++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 47 deletions(-)
drivers/acpi/acpi_tad.c +47 -47
@@ -174,6 +174,53 @@ static int acpi_tad_get_real_time(structreturn __acpi_tad_get_real_time (dev, rt); } +static int __acpi_tad_wake_set (struct device *dev,char *method,u32 timer_id, +u32 value) +{ +acpi_handle handle =ACPI_HANDLE (dev); +union acpi_object args[] = { + { .type =ACPI_TYPE_INTEGER , }, + { .type =ACPI_TYPE_INTEGER , }, + }; +struct acpi_object_list arg_list = { + .pointer = args, + .count =ARRAY_SIZE (args), + }; +unsigned long long retval; +acpi_status status; + + args[0 ].integer.value = timer_id; + args[1 ].integer.value = value; + + status =acpi_evaluate_integer (handle, method, &arg_list, &retval); +if (ACPI_FAILURE (status) || retval) +return -EIO ; + +return 0 ; +} + +static int __acpi_tad_wake_read (struct device *dev,char *method,u32 timer_id, +unsigned long long *retval) +{ +acpi_handle handle =ACPI_HANDLE (dev); +union acpi_object args[] = { + { .type =ACPI_TYPE_INTEGER , }, + }; +struct acpi_object_list arg_list = { + .pointer = args, + .count =ARRAY_SIZE (args), + }; +acpi_status status; + + args[0 ].integer.value = timer_id; + + status =acpi_evaluate_integer (handle, method, &arg_list, retval); +if (ACPI_FAILURE (status)) +return -EIO ; + +return 0 ; +} +/* sysfs interface */ static char *acpi_tad_rt_next_field (char *s,int *val)
@@ -273,31 +320,6 @@ static ssize_t time_show(struct device *static DEVICE_ATTR_RW (time); -static int __acpi_tad_wake_set (struct device *dev,char *method,u32 timer_id, -u32 value) -{ -acpi_handle handle =ACPI_HANDLE (dev); -union acpi_object args[] = { - { .type =ACPI_TYPE_INTEGER , }, - { .type =ACPI_TYPE_INTEGER , }, - }; -struct acpi_object_list arg_list = { - .pointer = args, - .count =ARRAY_SIZE (args), - }; -unsigned long long retval; -acpi_status status; - - args[0 ].integer.value = timer_id; - args[1 ].integer.value = value; - - status =acpi_evaluate_integer (handle, method, &arg_list, &retval); -if (ACPI_FAILURE (status) || retval) -return -EIO ; - -return 0 ; -} -static int acpi_tad_wake_set (struct device *dev,char *method,u32 timer_id,u32 value) {
@@ -328,28 +350,6 @@ static int acpi_tad_wake_write(struct dereturn acpi_tad_wake_set (dev, method, timer_id, value); } -static int __acpi_tad_wake_read (struct device *dev,char *method,u32 timer_id, -unsigned long long *retval) -{ -acpi_handle handle =ACPI_HANDLE (dev); -union acpi_object args[] = { - { .type =ACPI_TYPE_INTEGER , }, - }; -struct acpi_object_list arg_list = { - .pointer = args, - .count =ARRAY_SIZE (args), - }; -acpi_status status; - - args[0 ].integer.value = timer_id; - - status =acpi_evaluate_integer (handle, method, &arg_list, retval); -if (ACPI_FAILURE (status)) -return -EIO ; - -return 0 ; -} -static ssize_t acpi_tad_wake_read (struct device *dev,char *buf,char *method,u32 timer_id,const char *specval) {
Rafael J. Wysocki
maintainerPATCH 3/4 ACPI: TAD: Split acpi_tad_rtc_set_time()
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Move the code converting a struct acpi_tad_rt into a struct rtc_time from acpi_tad_rtc_set_time() into a new function, acpi_tad_rt_to_tm(), to facilitate adding alarm support to the driver's RTC class device interface going forward.
No intentional functional impact.
--- drivers/acpi/acpi_tad.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/acpi/acpi_tad.c +12 -7
@@ -617,6 +617,17 @@ static const struct attribute_group *acp#ifdef CONFIG_RTC_CLASS /* RTC class device interface */ +static void acpi_tad_rt_to_tm (struct acpi_tad_rt *rt,struct rtc_time *tm) +{ + tm->tm_year = rt->year -1900 ; + tm->tm_mon = rt->month -1 ; + tm->tm_mday = rt->day; + tm->tm_hour = rt->hour; + tm->tm_min = rt->minute; + tm->tm_sec = rt->second; + tm->tm_isdst = rt->daylight ==ACPI_TAD_TIME_ISDST ; +} +static int acpi_tad_rtc_set_time (struct device *dev,struct rtc_time *tm) {struct acpi_tad_rt rt;
@@ -642,13 +653,7 @@ static int acpi_tad_rtc_read_time(structif (ret)return ret; - tm->tm_year = rt.year -1900 ; - tm->tm_mon = rt.month -1 ; - tm->tm_mday = rt.day; - tm->tm_hour = rt.hour; - tm->tm_min = rt.minute; - tm->tm_sec = rt.second; - tm->tm_isdst = rt.daylight ==ACPI_TAD_TIME_ISDST ; +acpi_tad_rt_to_tm (&rt, tm);return 0 ; }
Rafael J. Wysocki
maintainerPATCH 4/4 ACPI: TAD: Add alarm support to the RTC class device interface
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Add alarm support, based on Section 9.17 of ACPI 6.6 [1], to the RTC class device interface of the driver.
The ACPI time and alarm device (TAD) can support two separate alarm timers, one for waking up the system when it is on AC power, and one for waking it up when it is on DC power. In principle, each of them can be set to a different value representing the number of seconds till the given alarm timer expires.
However, the RTC class device can only set one alarm, so it will set both the alarm timers of the ACPI TAD (if the DC one is supported) to the same value. That is somewhat cumbersome because there is no way in the ACPI TAD firmware interface to set both timers in one go, so they need to be set sequentially, but that's how it goes.
On the alarm read side, the driver assumes that both timers have been set to the same value, so it is sufficient to access one of them (the AC one specifically).
--- drivers/acpi/acpi_tad.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 3 deletions(-)
drivers/acpi/acpi_tad.c +109 -3
@@ -25,6 +25,7 @@#include <linux/acpi.h> #include <linux/kernel.h> +#include <linux/ktime.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/pm_runtime.h>
@@ -658,12 +659,113 @@ static int acpi_tad_rtc_read_time(structreturn 0 ; } +static int acpi_tad_rtc_set_alarm (struct device *dev,struct rtc_wkalrm *t) +{ +struct acpi_tad_driver_data *dd =dev_get_drvdata (dev); +s64 value =ACPI_TAD_WAKE_DISABLED ; +struct rtc_time tm_now; +struct acpi_tad_rt rt; +int ret; + +PM_RUNTIME_ACQUIRE (dev, pm); +if (PM_RUNTIME_ACQUIRE_ERR (&pm)) +return -ENXIO ; + +if (t->enabled) { +/* +* The value to pass to _STV is expected to be the number of +* seconds between the time when the timer is programmed and the +* time when it expires represented as a 32-bit integer. +*/ + ret =__acpi_tad_get_real_time (dev, &rt); +if (ret) +return ret; + +acpi_tad_rt_to_tm (&rt, &tm_now); + + value =ktime_divns (ktime_sub (rtc_tm_to_ktime (t->time), +rtc_tm_to_ktime (tm_now)),NSEC_PER_SEC ); +if (value <=0 || value >U32_MAX ) +return -EINVAL ; + } + + ret =__acpi_tad_wake_set (dev,"_STV" ,ACPI_TAD_AC_TIMER , value); +if (ret && t->enabled) +return ret; + +/* +* If a separate DC alarm timer is supported, set it to the same value +* as the AC alarm timer. +*/ +if (dd->capabilities &ACPI_TAD_DC_WAKE ) { + ret =__acpi_tad_wake_set (dev,"_STV" ,ACPI_TAD_DC_TIMER , value); +if (ret && t->enabled) { +__acpi_tad_wake_set (dev,"_STV" ,ACPI_TAD_AC_TIMER , +ACPI_TAD_WAKE_DISABLED ); +return ret; + } + } + +/* Assume success if the alarm is being disabled. */ +return 0 ; +} + +static int acpi_tad_rtc_read_alarm (struct device *dev,struct rtc_wkalrm *t) +{ +unsigned long long retval; +struct rtc_time tm_now; +struct acpi_tad_rt rt; +int ret; + +PM_RUNTIME_ACQUIRE (dev, pm); +if (PM_RUNTIME_ACQUIRE_ERR (&pm)) +return -ENXIO ; + + ret =__acpi_tad_get_real_time (dev, &rt); +if (ret) +return ret; + +acpi_tad_rt_to_tm (&rt, &tm_now); + +/* +* Assume that the alarm was set by acpi_tad_rtc_set_alarm(), so the AC +* and DC alarm timer settings are the same and it is sufficient to read +* the former. +* +* The value returned by _TIV should be the number of seconds till the +* expiration of the timer, represented as a 32-bit integer, or the +* special ACPI_TAD_WAKE_DISABLED value meaning that the timer has +* been disabled. +*/ + ret =__acpi_tad_wake_read (dev,"_TIV" ,ACPI_TAD_AC_TIMER , &retval); +if (ret) +return ret; + +if (retval >U32_MAX ) +return -ENODATA ; + + t->pending =0 ; + +if (retval !=ACPI_TAD_WAKE_DISABLED ) { + t->enabled =1 ; + t->time =rtc_ktime_to_tm (ktime_add_ns (rtc_tm_to_ktime (tm_now), + (u64 )retval *NSEC_PER_SEC )); + }else { + t->enabled =0 ; + t->time = tm_now; + } + +return 0 ; +} +static const struct rtc_class_ops acpi_tad_rtc_ops = { .read_time = acpi_tad_rtc_read_time, .set_time = acpi_tad_rtc_set_time, + .set_alarm = acpi_tad_rtc_set_alarm, + .read_alarm = acpi_tad_rtc_read_alarm, }; -static void acpi_tad_register_rtc (struct device *dev) +static void acpi_tad_register_rtc (struct device *dev,unsigned long long caps) {struct rtc_device *rtc;
@@ -676,10 +778,14 @@ static void acpi_tad_register_rtc(struct rtc->ops = &acpi_tad_rtc_ops; +if (!(caps &ACPI_TAD_AC_WAKE )) +clear_bit (RTC_FEATURE_ALARM , rtc->features); + devm_rtc_register_device(rtc); }#else /* !CONFIG_RTC_CLASS */ -static inline void acpi_tad_register_rtc (struct device *dev) {} +static inline void acpi_tad_register_rtc (struct device *dev, +unsigned long long caps) {}#endif /* !CONFIG_RTC_CLASS */ /* Platform driver interface */
@@ -765,7 +871,7 @@ static int acpi_tad_probe(struct platforpm_runtime_suspend (dev);if (caps &ACPI_TAD_RT ) -acpi_tad_register_rtc (dev); +acpi_tad_register_rtc (dev, caps);return 0 ; }
Quoted 11 lines · Rafael J. Wysocki
Hi All,
This series adds alarm support via the RTC class device interface of the ACPI time and alarm device (TAD) driver.
The code is first rearranged to facilitate re-use (patches [1-3/4] and then the RTC class interface of the driver is extended to support alarm (patch [4/4]).
Thanks!
Quoted 2 lines
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com