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 and date/time utility functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2005-06 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 and other bits
^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/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static const unsigned char rtc_days_in_month[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static const unsigned short rtc_ydays[2][13] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	/* Normal years */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	/* Leap years */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * The number of days in the month.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) int rtc_month_days(unsigned int month, unsigned int year)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) EXPORT_SYMBOL(rtc_month_days);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * The number of days since January 1. (0 to 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	return rtc_ydays[is_leap_year(year)][month] + day - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) EXPORT_SYMBOL(rtc_year_days);
^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)  * rtc_time64_to_tm - Converts time64_t to rtc_time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned int month, year, secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int days;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	/* time must be positive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	days = div_s64_rem(time, 86400, &secs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/* day of the week, 1970-01-01 was a Thursday */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	tm->tm_wday = (days + 4) % 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	year = 1970 + days / 365;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	days -= (year - 1970) * 365
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		+ LEAPS_THRU_END_OF(year - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		- LEAPS_THRU_END_OF(1970 - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	while (days < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		year -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		days += 365 + is_leap_year(year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	tm->tm_year = year - 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	tm->tm_yday = days + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	for (month = 0; month < 11; month++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		int newdays;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		newdays = days - rtc_month_days(month, year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (newdays < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		days = newdays;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	tm->tm_mon = month;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	tm->tm_mday = days + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	tm->tm_hour = secs / 3600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	secs -= tm->tm_hour * 3600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	tm->tm_min = secs / 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	tm->tm_sec = secs - tm->tm_min * 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	tm->tm_isdst = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) EXPORT_SYMBOL(rtc_time64_to_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * Does the rtc_time represent a valid date/time?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) int rtc_valid_tm(struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (tm->tm_year < 70 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	    tm->tm_year > (INT_MAX - 1900) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	    ((unsigned int)tm->tm_mon) >= 12 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	    tm->tm_mday < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	    tm->tm_mday > rtc_month_days(tm->tm_mon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 					 ((unsigned int)tm->tm_year + 1900)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	    ((unsigned int)tm->tm_hour) >= 24 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	    ((unsigned int)tm->tm_min) >= 60 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	    ((unsigned int)tm->tm_sec) >= 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) EXPORT_SYMBOL(rtc_valid_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * rtc_tm_to_time64 - Converts rtc_time to time64_t.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) time64_t rtc_tm_to_time64(struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) EXPORT_SYMBOL(rtc_tm_to_time64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * Convert rtc_time to ktime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ktime_t rtc_tm_to_ktime(struct rtc_time tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return ktime_set(rtc_tm_to_time64(&tm), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * Convert ktime to rtc_time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct rtc_time rtc_ktime_to_tm(ktime_t kt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct rtc_time ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	ts = ktime_to_timespec64(kt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* Round up any ns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (ts.tv_nsec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		ts.tv_sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	rtc_time64_to_tm(ts.tv_sec, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);