Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * RTC subsystem, interface functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2005 Tower Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Author: Alessandro Zummo <a.zummo@towertech.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * based on arch/arm/common/rtctime.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/log2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <trace/events/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	time64_t secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	if (!rtc->offset_secs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	secs = rtc_tm_to_time64(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	 * Since the reading time values from RTC device are always in the RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	 * original valid range, but we need to skip the overlapped region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	 * between expanded range and original range, which is no need to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	 * the offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	    (rtc->start_secs < rtc->range_min &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	     secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	rtc_time64_to_tm(secs + rtc->offset_secs, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	time64_t secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	if (!rtc->offset_secs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	secs = rtc_tm_to_time64(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	 * If the setting time values are in the valid range of RTC hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	 * device, then no need to subtract the offset when setting time to RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	 * device. Otherwise we need to subtract the offset to make the time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	 * values are valid for RTC hardware device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	if (secs >= rtc->range_min && secs <= rtc->range_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	rtc_time64_to_tm(secs - rtc->offset_secs, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	if (rtc->range_min != rtc->range_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 		time64_t time = rtc_tm_to_time64(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		time64_t range_min = rtc->set_start_time ? rtc->start_secs :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 			rtc->range_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		timeu64_t range_max = rtc->set_start_time ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 			(rtc->start_secs + rtc->range_max - rtc->range_min) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 			rtc->range_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		if (time < range_min || time > range_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 			return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	if (!rtc->ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	} else if (!rtc->ops->read_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		memset(tm, 0, sizeof(struct rtc_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		err = rtc->ops->read_time(rtc->dev.parent, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 			dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 				err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		rtc_add_offset(rtc, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		err = rtc_valid_tm(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 			dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	err = __rtc_read_time(rtc, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	trace_rtc_read_time(rtc_tm_to_time64(tm), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) EXPORT_SYMBOL_GPL(rtc_read_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	int err, uie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	err = rtc_valid_tm(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	err = rtc_valid_range(rtc, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	rtc_subtract_offset(rtc, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	uie = rtc->uie_rtctimer.enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (uie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		err = rtc_update_irq_enable(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	if (!rtc->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	else if (rtc->ops->set_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		err = rtc->ops->set_time(rtc->dev.parent, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	pm_stay_awake(rtc->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	/* A timer might have just expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	schedule_work(&rtc->irqwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	if (uie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		err = rtc_update_irq_enable(rtc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	trace_rtc_set_time(rtc_tm_to_time64(tm), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) EXPORT_SYMBOL_GPL(rtc_set_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) static int rtc_read_alarm_internal(struct rtc_device *rtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 				   struct rtc_wkalrm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	if (!rtc->ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	} else if (!rtc->ops->read_alarm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		alarm->enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		alarm->pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		alarm->time.tm_sec = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		alarm->time.tm_min = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		alarm->time.tm_hour = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		alarm->time.tm_mday = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		alarm->time.tm_mon = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		alarm->time.tm_year = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		alarm->time.tm_wday = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		alarm->time.tm_yday = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		alarm->time.tm_isdst = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	struct rtc_time before, now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	int first_time = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	time64_t t_now, t_alm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	enum { none, day, month, year } missing = none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	unsigned int days;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* The lower level RTC driver may return -1 in some fields,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	 * creating invalid alarm->time values, for reasons like:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	 *   - The hardware may not be capable of filling them in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	 *     many alarms match only on time-of-day fields, not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	 *     day/month/year calendar data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	 *   - Some hardware uses illegal values as "wildcard" match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	 *     values, which non-Linux firmware (like a BIOS) may try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	 *     to set up as e.g. "alarm 15 minutes after each hour".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	 *     Linux uses only oneshot alarms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	 * When we see that here, we deal with it by using values from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	 * a current RTC timestamp for any missing (-1) values.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	 * RTC driver prevents "periodic alarm" modes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	 * But this can be racey, because some fields of the RTC timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	 * may have wrapped in the interval since we read the RTC alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	 * which would lead to us inserting inconsistent values in place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	 * of the -1 fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	 * Reading the alarm and timestamp in the reverse sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	 * would have the same race condition, and not solve the issue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	 * So, we must first read the RTC timestamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	 * then read the RTC alarm value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	 * and then read a second RTC timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	 * If any fields of the second timestamp have changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	 * when compared with the first timestamp, then we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	 * our timestamp may be inconsistent with that used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	 * the low-level rtc_read_alarm_internal() function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	 * So, when the two timestamps disagree, we just loop and do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	 * the process again to get a fully consistent set of values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	 * This could all instead be done in the lower level driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	 * but since more than one lower level RTC implementation needs it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	 * then it's probably best best to do it here instead of there..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	/* Get the "before" timestamp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	err = rtc_read_time(rtc, &before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		if (!first_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			memcpy(&before, &now, sizeof(struct rtc_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		first_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		/* get the RTC alarm values, which may be incomplete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		err = rtc_read_alarm_internal(rtc, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		/* full-function RTCs won't have such missing fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		if (rtc_valid_tm(&alarm->time) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 			rtc_add_offset(rtc, &alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		/* get the "after" timestamp, to detect wrapped fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		err = rtc_read_time(rtc, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		/* note that tm_sec is a "don't care" value here: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	} while (before.tm_min  != now.tm_min ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		 before.tm_hour != now.tm_hour ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		 before.tm_mon  != now.tm_mon ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		 before.tm_year != now.tm_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	/* Fill in the missing alarm fields using the timestamp; we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	 * know there's at least one since alarm->time is invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	if (alarm->time.tm_sec == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		alarm->time.tm_sec = now.tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	if (alarm->time.tm_min == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		alarm->time.tm_min = now.tm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	if (alarm->time.tm_hour == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		alarm->time.tm_hour = now.tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	/* For simplicity, only support date rollover for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		alarm->time.tm_mday = now.tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		missing = day;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	if ((unsigned int)alarm->time.tm_mon >= 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		alarm->time.tm_mon = now.tm_mon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		if (missing == none)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			missing = month;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	if (alarm->time.tm_year == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		alarm->time.tm_year = now.tm_year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		if (missing == none)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			missing = year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	/* Can't proceed if alarm is still invalid after replacing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	 * missing fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	err = rtc_valid_tm(&alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	/* with luck, no rollover is needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	t_now = rtc_tm_to_time64(&now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	t_alm = rtc_tm_to_time64(&alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	if (t_now < t_alm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	switch (missing) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	/* 24 hour rollover ... if it's now 10am Monday, an alarm that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	 * that will trigger at 5am will do so at 5am Tuesday, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	 * could also be in the next month or year.  This is a common
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	 * case, especially for PCs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	case day:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		t_alm += 24 * 60 * 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		rtc_time64_to_tm(t_alm, &alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	/* Month rollover ... if it's the 31th, an alarm on the 3rd will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	 * be next month.  An alarm matching on the 30th, 29th, or 28th
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	 * may end up in the month after that!  Many newer PCs support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	 * this type of alarm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	case month:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			if (alarm->time.tm_mon < 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 				alarm->time.tm_mon++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 				alarm->time.tm_mon = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 				alarm->time.tm_year++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 			days = rtc_month_days(alarm->time.tm_mon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 					      alarm->time.tm_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		} while (days < alarm->time.tm_mday);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	/* Year rollover ... easy except for leap years! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	case year:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			alarm->time.tm_year++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		} while (!is_leap_year(alarm->time.tm_year + 1900) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			 rtc_valid_tm(&alarm->time) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		dev_warn(&rtc->dev, "alarm rollover not handled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	err = rtc_valid_tm(&alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			 &alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (!rtc->ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	} else if (!rtc->ops->read_alarm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		memset(alarm, 0, sizeof(struct rtc_wkalrm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		alarm->enabled = rtc->aie_timer.enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) EXPORT_SYMBOL_GPL(rtc_read_alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	time64_t now, scheduled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	err = rtc_valid_tm(&alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	scheduled = rtc_tm_to_time64(&alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	/* Make sure we're not setting alarms in the past */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	err = __rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	now = rtc_tm_to_time64(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	if (scheduled <= now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		return -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	 * XXX - We just checked to make sure the alarm time is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	 * in the past, but there is still a race window where if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	 * the is alarm set for the next second and the second ticks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	 * over right here, before we set the alarm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	rtc_subtract_offset(rtc, &alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (!rtc->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	else if (!rtc->ops->set_alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (!rtc->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	else if (!rtc->ops->set_alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	err = rtc_valid_tm(&alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	err = rtc_valid_range(rtc, &alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (rtc->aie_timer.enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		rtc_timer_remove(rtc, &rtc->aie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	rtc->aie_timer.period = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (alarm->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) EXPORT_SYMBOL_GPL(rtc_set_alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) /* Called once per device from rtc_device_register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	struct rtc_time now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	err = rtc_valid_tm(&alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	err = rtc_read_time(rtc, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	rtc->aie_timer.period = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	/* Alarm has to be enabled & in the future for us to enqueue it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (alarm->enabled && (rtc_tm_to_ktime(now) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			 rtc->aie_timer.node.expires)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		rtc->aie_timer.enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		trace_rtc_timer_enqueue(&rtc->aie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	if (rtc->aie_timer.enabled != enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			rtc_timer_remove(rtc, &rtc->aie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		/* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	else if (!rtc->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	else if (!rtc->ops->alarm_irq_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	trace_rtc_alarm_irq_enable(enabled, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	int rc = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (enabled == 0 && rtc->uie_irq_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		return rtc_dev_update_irq_enable_emul(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	/* make sure we're changing state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (rtc->uie_rtctimer.enabled == enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	if (rtc->uie_unsupported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	if (enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		ktime_t now, onesec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		rc = __rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		onesec = ktime_set(1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		now = rtc_tm_to_ktime(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		rtc->uie_rtctimer.period = ktime_set(1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		rtc_timer_remove(rtc, &rtc->uie_rtctimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	 * __rtc_read_time() failed, this probably means that the RTC time has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	 * never been set or less probably there is a transient error on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	 * bus. In any case, avoid enabling emulation has this will fail when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	 * reading the time too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	 * Enable emulation if the driver returned -EINVAL to signal that it has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	 * been configured without interrupts or they are not available at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	 * moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	if (err == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		err = rtc_dev_update_irq_enable_emul(rtc, enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  * @rtc: pointer to the rtc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  * @num: number of occurence of the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614)  * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616)  * This function is called when an AIE, UIE or PIE mode interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617)  * has occurred (or been emulated).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	/* mark one irq of the appropriate mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	spin_lock_irqsave(&rtc->irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	spin_unlock_irqrestore(&rtc->irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	wake_up_interruptible(&rtc->irq_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  * rtc_aie_update_irq - AIE mode rtctimer hook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635)  * @rtc: pointer to the rtc_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  * This functions is called when the aie_timer expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) void rtc_aie_update_irq(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	rtc_handle_legacy_irq(rtc, 1, RTC_AF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645)  * rtc_uie_update_irq - UIE mode rtctimer hook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  * @rtc: pointer to the rtc_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  * This functions is called when the uie_timer expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) void rtc_uie_update_irq(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	rtc_handle_legacy_irq(rtc, 1,  RTC_UF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656)  * rtc_pie_update_irq - PIE mode hrtimer hook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657)  * @timer: pointer to the pie mode hrtimer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659)  * This function is used to emulate PIE mode interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660)  * using an hrtimer. This function is called when the periodic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661)  * hrtimer expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	ktime_t period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	u64 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	rtc = container_of(timer, struct rtc_device, pie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	period = NSEC_PER_SEC / rtc->irq_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	count = hrtimer_forward_now(timer, period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	rtc_handle_legacy_irq(rtc, count, RTC_PF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	return HRTIMER_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680)  * rtc_update_irq - Triggered when a RTC interrupt occurs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  * @rtc: the rtc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682)  * @num: how many irqs are being reported (usually one)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683)  * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684)  * Context: any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) void rtc_update_irq(struct rtc_device *rtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		    unsigned long num, unsigned long events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	if (IS_ERR_OR_NULL(rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	pm_stay_awake(rtc->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	schedule_work(&rtc->irqwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) EXPORT_SYMBOL_GPL(rtc_update_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) struct rtc_device *rtc_class_open(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	struct rtc_device *rtc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	dev = class_find_device_by_name(rtc_class, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	if (rtc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		if (!try_module_get(rtc->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			rtc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	return rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) EXPORT_SYMBOL_GPL(rtc_class_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) void rtc_class_close(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	module_put(rtc->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	put_device(&rtc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) EXPORT_SYMBOL_GPL(rtc_class_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	 * We always cancel the timer here first, because otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	 * when we manage to start the timer before the callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	 * returns HRTIMER_RESTART.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	 * We cannot use hrtimer_cancel() here as a running callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	 * would spin forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748)  * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749)  * @rtc: the rtc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750)  * @enabled: true to enable periodic IRQs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751)  * Context: any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  * Note that rtc_irq_set_freq() should previously have been used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * specify the desired frequency of periodic IRQ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	while (rtc_update_hrtimer(rtc, enabled) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	rtc->pie_enabled = enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	trace_rtc_irq_set_state(enabled, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)  * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)  * @rtc: the rtc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772)  * @freq: positive frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773)  * Context: any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775)  * Note that rtc_irq_set_state() is used to enable or disable the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776)  * periodic IRQs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (freq <= 0 || freq > RTC_MAX_FREQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	rtc->irq_freq = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	trace_rtc_irq_set_freq(freq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794)  * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  * @rtc: rtc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  * @timer: timer being added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  * Enqueues a timer onto the rtc devices timerqueue and sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  * the next alarm event appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  * Sets the enabled bit on the added timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  * Must hold ops_lock for proper serialization of timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	err = __rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	timer->enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	now = rtc_tm_to_ktime(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	/* Skip over expired timers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	while (next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (next->expires >= now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		next = timerqueue_iterate_next(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	timerqueue_add(&rtc->timerqueue, &timer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	trace_rtc_timer_enqueue(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (!next || ktime_before(timer->node.expires, next->expires)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		struct rtc_wkalrm alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		alarm.time = rtc_ktime_to_tm(timer->node.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		alarm.enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		err = __rtc_set_alarm(rtc, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		if (err == -ETIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			pm_stay_awake(rtc->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			schedule_work(&rtc->irqwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		} else if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			timerqueue_del(&rtc->timerqueue, &timer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			trace_rtc_timer_dequeue(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			timer->enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) static void rtc_alarm_disable(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	if (!rtc->ops || !rtc->ops->alarm_irq_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	trace_rtc_alarm_irq_enable(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857)  * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858)  * @rtc: rtc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859)  * @timer: timer being removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861)  * Removes a timer onto the rtc devices timerqueue and sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  * the next alarm event appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864)  * Clears the enabled bit on the removed timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866)  * Must hold ops_lock for proper serialization of timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	timerqueue_del(&rtc->timerqueue, &timer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	trace_rtc_timer_dequeue(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	timer->enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	if (next == &timer->node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		struct rtc_wkalrm alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		next = timerqueue_getnext(&rtc->timerqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		if (!next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			rtc_alarm_disable(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		alarm.time = rtc_ktime_to_tm(next->expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		alarm.enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		err = __rtc_set_alarm(rtc, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		if (err == -ETIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			pm_stay_awake(rtc->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			schedule_work(&rtc->irqwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895)  * rtc_timer_do_work - Expires rtc timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896)  * @work: work item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  * Expires rtc timers. Reprograms next alarm event if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  * Called via worktask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  * Serializes access to timerqueue via ops_lock mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) void rtc_timer_do_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	struct rtc_timer *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	struct timerqueue_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct rtc_device *rtc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		container_of(work, struct rtc_device, irqwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	mutex_lock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	__rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	now = rtc_tm_to_ktime(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	while ((next = timerqueue_getnext(&rtc->timerqueue))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		if (next->expires > now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		/* expire timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		timer = container_of(next, struct rtc_timer, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		timerqueue_del(&rtc->timerqueue, &timer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		trace_rtc_timer_dequeue(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		timer->enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		if (timer->func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			timer->func(timer->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		trace_rtc_timer_fired(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		/* Re-add/fwd periodic timers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		if (ktime_to_ns(timer->period)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			timer->node.expires = ktime_add(timer->node.expires,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 							timer->period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			timer->enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			timerqueue_add(&rtc->timerqueue, &timer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			trace_rtc_timer_enqueue(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	/* Set next alarm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	if (next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		struct rtc_wkalrm alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		int retry = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		alarm.time = rtc_ktime_to_tm(next->expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		alarm.enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) reprogram:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		err = __rtc_set_alarm(rtc, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		if (err == -ETIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		} else if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			if (retry-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 				goto reprogram;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			timer = container_of(next, struct rtc_timer, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			timerqueue_del(&rtc->timerqueue, &timer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			trace_rtc_timer_dequeue(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			timer->enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		rtc_alarm_disable(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	pm_relax(rtc->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) /* rtc_timer_init - Initializes an rtc_timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972)  * @timer: timer to be intiialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973)  * @f: function pointer to be called when timer fires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974)  * @rtc: pointer to the rtc_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976)  * Kernel interface to initializing an rtc_timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		    struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	timerqueue_init(&timer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	timer->enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	timer->func = f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	timer->rtc = rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) /* rtc_timer_start - Sets an rtc_timer to fire in the future
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988)  * @ rtc: rtc device to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989)  * @ timer: timer being set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990)  * @ expires: time at which to expire the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991)  * @ period: period that the timer will recur
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993)  * Kernel interface to set an rtc_timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		    ktime_t expires, ktime_t period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	mutex_lock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	if (timer->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		rtc_timer_remove(rtc, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	timer->node.expires = expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	timer->period = period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	ret = rtc_timer_enqueue(rtc, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) /* rtc_timer_cancel - Stops an rtc_timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)  * @ rtc: rtc device to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)  * @ timer: timer being set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)  * Kernel interface to cancel an rtc_timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	mutex_lock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (timer->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		rtc_timer_remove(rtc, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * rtc_read_offset - Read the amount of rtc offset in parts per billion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  * @rtc: rtc device to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  * @offset: the offset in parts per billion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  * see below for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  * Kernel interface to read rtc clock offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)  * Returns 0 on success, or a negative number on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)  * If read_offset() is not implemented for the rtc, return -EINVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) int rtc_read_offset(struct rtc_device *rtc, long *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (!rtc->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	if (!rtc->ops->read_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	mutex_lock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	ret = rtc->ops->read_offset(rtc->dev.parent, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	trace_rtc_read_offset(*offset, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)  * rtc_set_offset - Adjusts the duration of the average second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)  * @rtc: rtc device to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)  * @offset: the offset in parts per billion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)  * Some rtc's allow an adjustment to the average duration of a second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)  * to compensate for differences in the actual clock rate due to temperature,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)  * the crystal, capacitor, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)  * The adjustment applied is as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)  *   t = t0 * (1 + offset * 1e-9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)  * where t0 is the measured length of 1 RTC second with offset = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)  * Kernel interface to adjust an rtc clock offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  * Return 0 on success, or a negative number on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)  * If the rtc offset is not setable (or not implemented), return -EINVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) int rtc_set_offset(struct rtc_device *rtc, long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	if (!rtc->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	if (!rtc->ops->set_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	mutex_lock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	ret = rtc->ops->set_offset(rtc->dev.parent, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	trace_rtc_set_offset(offset, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }