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, base class
^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)  * class skeleton from drivers/hwmon/hwmon.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) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^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/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kdev_t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "rtc-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static DEFINE_IDA(rtc_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct class *rtc_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static void rtc_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct rtc_device *rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	ida_simple_remove(&rtc_ida, rtc->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	kfree(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #ifdef CONFIG_RTC_HCTOSYS_DEVICE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* Result of the last RTC to system clock attempt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) int rtc_hctosys_ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * whether it stores the most close value or the value with partial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * seconds truncated. However, it is important that we use it to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * the truncated value. This is because otherwise it is necessary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * in an rtc sync function, to read both xtime.tv_sec and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * of >32bits is not possible. So storing the most close value would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * slow down the sync API. So here we have the truncated value and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * the best guess is to add 0.5s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static void rtc_hctosys(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct timespec64 tv64 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		.tv_nsec = NSEC_PER_SEC >> 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		dev_err(rtc->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			"hctosys: unable to read the hardware clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		goto err_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	tv64.tv_sec = rtc_tm_to_time64(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #if BITS_PER_LONG == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (tv64.tv_sec > INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		err = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		goto err_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	err = do_settimeofday64(&tv64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		 &tm, (long long)tv64.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) err_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	rtc_hctosys_ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * On suspend(), measure the delta between one RTC and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * system's wall clock; restore it on resume().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static struct timespec64 old_rtc, old_system, old_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static int rtc_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct rtc_device	*rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct rtc_time		tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct timespec64	delta, delta_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (timekeeping_rtc_skipsuspend())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* snapshot the current RTC and system time at suspend*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	ktime_get_real_ts64(&old_system);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	old_rtc.tv_sec = rtc_tm_to_time64(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 * To avoid drift caused by repeated suspend/resumes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	 * which each can add ~1 second drift error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	 * try to compensate so the difference in system time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	 * and rtc time stays close to constant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	delta = timespec64_sub(old_system, old_rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	delta_delta = timespec64_sub(delta, old_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		 * if delta_delta is too large, assume time correction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		 * has occurred and set old_delta to the current delta.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		old_delta = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		/* Otherwise try to adjust old_system to compensate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		old_system = timespec64_sub(old_system, delta_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int rtc_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct rtc_device	*rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct rtc_time		tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct timespec64	new_system, new_rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct timespec64	sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (timekeeping_rtc_skipresume())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	rtc_hctosys_ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/* snapshot the current rtc and system time at resume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	ktime_get_real_ts64(&new_system);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	new_rtc.tv_sec = rtc_tm_to_time64(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	new_rtc.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (new_rtc.tv_sec < old_rtc.tv_sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		pr_debug("%s:  time travel!\n", dev_name(&rtc->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/* calculate the RTC time delta (sleep time)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	sleep_time = timespec64_sub(new_rtc, old_rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 * Since these RTC suspend/resume handlers are not called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * at the very end of suspend or the start of resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 * some run-time may pass on either sides of the sleep time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 * so subtract kernel run-time between rtc_suspend to rtc_resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 * to keep things accurate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	sleep_time = timespec64_sub(sleep_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				    timespec64_sub(new_system, old_system));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (sleep_time.tv_sec >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		timekeeping_inject_sleeptime64(&sleep_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	rtc_hctosys_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #define RTC_CLASS_DEV_PM_OPS	(&rtc_class_dev_pm_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #define RTC_CLASS_DEV_PM_OPS	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* Ensure the caller will set the id before releasing the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static struct rtc_device *rtc_allocate_device(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (!rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	device_initialize(&rtc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	/* Drivers can revise this default after allocating the device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	rtc->set_offset_nsec =  NSEC_PER_SEC / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	rtc->irq_freq = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	rtc->max_user_freq = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	rtc->dev.class = rtc_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	rtc->dev.groups = rtc_get_dev_attribute_groups();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	rtc->dev.release = rtc_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	mutex_init(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	spin_lock_init(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	init_waitqueue_head(&rtc->irq_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	/* Init timerqueue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	timerqueue_init_head(&rtc->timerqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	/* Init aie timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	/* Init uie timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* Init pie timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	rtc->pie_timer.function = rtc_pie_update_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	rtc->pie_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	return rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int rtc_device_get_id(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	int of_id = -1, id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (dev->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		of_id = of_alias_get_id(dev->of_node, "rtc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	else if (dev->parent && dev->parent->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		of_id = of_alias_get_id(dev->parent->of_node, "rtc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (of_id >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		id = ida_simple_get(&rtc_ida, of_id, of_id + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			dev_warn(dev, "/aliases ID %d not available\n", of_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void rtc_device_get_offset(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	time64_t range_secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	u32 start_year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	 * If RTC driver did not implement the range of RTC hardware device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	 * then we can not expand the RTC range by adding or subtracting one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (rtc->range_min == rtc->range_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	ret = device_property_read_u32(rtc->dev.parent, "start-year",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				       &start_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		rtc->set_start_time = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * If user did not implement the start time for RTC driver, then no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 * need to expand the RTC range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (!rtc->set_start_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	range_secs = rtc->range_max - rtc->range_min + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * If the start_secs is larger than the maximum seconds (rtc->range_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * supported by RTC hardware or the maximum seconds of new expanded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * range (start_secs + rtc->range_max - rtc->range_min) is less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * rtc->range_min, which means the minimum seconds (rtc->range_min) of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * RTC hardware will be mapped to start_secs by adding one offset, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * the offset seconds calculation formula should be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * rtc->offset_secs = rtc->start_secs - rtc->range_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * If the start_secs is larger than the minimum seconds (rtc->range_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 * supported by RTC hardware, then there is one region is overlapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * between the original RTC hardware range and the new expanded range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 * and this overlapped region do not need to be mapped into the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	 * expanded range due to it is valid for RTC device. So the minimum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 * seconds of RTC hardware (rtc->range_min) should be mapped to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 * rtc->range_max + 1, then the offset seconds formula should be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	 * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	 * If the start_secs is less than the minimum seconds (rtc->range_min),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 * which is similar to case 2. So the start_secs should be mapped to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 * start_secs + rtc->range_max - rtc->range_min + 1, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	 * offset seconds formula should be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	 * Otherwise the offset seconds should be 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (rtc->start_secs > rtc->range_max ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	    rtc->start_secs + range_secs - 1 < rtc->range_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		rtc->offset_secs = rtc->start_secs - rtc->range_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	else if (rtc->start_secs > rtc->range_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		rtc->offset_secs = range_secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	else if (rtc->start_secs < rtc->range_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		rtc->offset_secs = -range_secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		rtc->offset_secs = 0;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  * rtc_device_unregister - removes the previously registered RTC class device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * @rtc: the RTC class device to destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static void rtc_device_unregister(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	mutex_lock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	 * Remove innards of this RTC, then disable it, before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	 * letting any rtc_class_open() users access it again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	rtc_proc_del_device(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	cdev_device_del(&rtc->char_dev, &rtc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	rtc->ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	put_device(&rtc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static void devm_rtc_release_device(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct rtc_device *rtc = *(struct rtc_device **)res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	rtc_nvmem_unregister(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (rtc->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		rtc_device_unregister(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		put_device(&rtc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct rtc_device *devm_rtc_allocate_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct rtc_device **ptr, *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	int id, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	id = rtc_device_get_id(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		return ERR_PTR(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	ptr = devres_alloc(devm_rtc_release_device, sizeof(*ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		goto exit_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	rtc = rtc_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (!rtc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		goto exit_devres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	*ptr = rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	rtc->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	rtc->dev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	dev_set_name(&rtc->dev, "rtc%d", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) exit_devres:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) exit_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	ida_simple_remove(&rtc_ida, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct rtc_wkalrm alrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (!rtc->ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		dev_dbg(&rtc->dev, "no ops set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	rtc->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	rtc_device_get_offset(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/* Check to see if there is an ALARM already set in hw */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	err = __rtc_read_alarm(rtc, &alrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (!err && !rtc_valid_tm(&alrm.time))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		rtc_initialize_alarm(rtc, &alrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	rtc_dev_prepare(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	err = cdev_device_add(&rtc->char_dev, &rtc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			 MAJOR(rtc->dev.devt), rtc->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			MAJOR(rtc->dev.devt), rtc->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	rtc_proc_add_device(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	rtc->registered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	dev_info(rtc->dev.parent, "registered as %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		 dev_name(&rtc->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) #ifdef CONFIG_RTC_HCTOSYS_DEVICE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		rtc_hctosys(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) EXPORT_SYMBOL_GPL(__rtc_register_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  * devm_rtc_device_register - resource managed rtc_device_register()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  * @dev: the device to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  * @name: the name of the device (unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  * @ops: the rtc operations structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)  * @owner: the module owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  * @return a struct rtc on success, or an ERR_PTR on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  * Managed rtc_device_register(). The rtc_device returned from this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  * are automatically freed on driver detach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * This function is deprecated, use devm_rtc_allocate_device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  * rtc_register_device instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct rtc_device *devm_rtc_device_register(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 					    const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 					    const struct rtc_class_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 					    struct module *owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	rtc = devm_rtc_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (IS_ERR(rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		return rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	rtc->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	err = __rtc_register_device(owner, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	return rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) EXPORT_SYMBOL_GPL(devm_rtc_device_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static int __init rtc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	rtc_class = class_create(THIS_MODULE, "rtc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (IS_ERR(rtc_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		pr_err("couldn't create class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		return PTR_ERR(rtc_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	rtc_dev_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) subsys_initcall(rtc_init);