xref: /linux-6.15/include/linux/rtc.h (revision fdcfd854)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Generic RTC interface.
4  * This version contains the part of the user interface to the Real Time Clock
5  * service. It is used with both the legacy mc146818 and also  EFI
6  * Struct rtc_time and first 12 ioctl by Paul Gortmaker, 1996 - separated out
7  * from <linux/mc146818rtc.h> to this file for 2.4 kernels.
8  *
9  * Copyright (C) 1999 Hewlett-Packard Co.
10  * Copyright (C) 1999 Stephane Eranian <[email protected]>
11  */
12 #ifndef _LINUX_RTC_H_
13 #define _LINUX_RTC_H_
14 
15 
16 #include <linux/types.h>
17 #include <linux/interrupt.h>
18 #include <linux/nvmem-provider.h>
19 #include <uapi/linux/rtc.h>
20 
21 extern int rtc_month_days(unsigned int month, unsigned int year);
22 extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
23 extern int rtc_valid_tm(struct rtc_time *tm);
24 extern time64_t rtc_tm_to_time64(struct rtc_time *tm);
25 extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm);
26 ktime_t rtc_tm_to_ktime(struct rtc_time tm);
27 struct rtc_time rtc_ktime_to_tm(ktime_t kt);
28 
29 /*
30  * rtc_tm_sub - Return the difference in seconds.
31  */
32 static inline time64_t rtc_tm_sub(struct rtc_time *lhs, struct rtc_time *rhs)
33 {
34 	return rtc_tm_to_time64(lhs) - rtc_tm_to_time64(rhs);
35 }
36 
37 #include <linux/device.h>
38 #include <linux/seq_file.h>
39 #include <linux/cdev.h>
40 #include <linux/poll.h>
41 #include <linux/mutex.h>
42 #include <linux/timerqueue.h>
43 #include <linux/workqueue.h>
44 
45 extern struct class *rtc_class;
46 
47 /*
48  * For these RTC methods the device parameter is the physical device
49  * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
50  * was passed to rtc_device_register().  Its driver_data normally holds
51  * device state, including the rtc_device pointer for the RTC.
52  *
53  * Most of these methods are called with rtc_device.ops_lock held,
54  * through the rtc_*(struct rtc_device *, ...) calls.
55  *
56  * The (current) exceptions are mostly filesystem hooks:
57  *   - the proc() hook for procfs
58  */
59 struct rtc_class_ops {
60 	int (*ioctl)(struct device *, unsigned int, unsigned long);
61 	int (*read_time)(struct device *, struct rtc_time *);
62 	int (*set_time)(struct device *, struct rtc_time *);
63 	int (*read_alarm)(struct device *, struct rtc_wkalrm *);
64 	int (*set_alarm)(struct device *, struct rtc_wkalrm *);
65 	int (*proc)(struct device *, struct seq_file *);
66 	int (*alarm_irq_enable)(struct device *, unsigned int enabled);
67 	int (*read_offset)(struct device *, long *offset);
68 	int (*set_offset)(struct device *, long offset);
69 };
70 
71 struct rtc_device;
72 
73 struct rtc_timer {
74 	struct timerqueue_node node;
75 	ktime_t period;
76 	void (*func)(struct rtc_device *rtc);
77 	struct rtc_device *rtc;
78 	int enabled;
79 };
80 
81 /* flags */
82 #define RTC_DEV_BUSY 0
83 
84 struct rtc_device {
85 	struct device dev;
86 	struct module *owner;
87 
88 	int id;
89 
90 	const struct rtc_class_ops *ops;
91 	struct mutex ops_lock;
92 
93 	struct cdev char_dev;
94 	unsigned long flags;
95 
96 	unsigned long irq_data;
97 	spinlock_t irq_lock;
98 	wait_queue_head_t irq_queue;
99 	struct fasync_struct *async_queue;
100 
101 	int irq_freq;
102 	int max_user_freq;
103 
104 	struct timerqueue_head timerqueue;
105 	struct rtc_timer aie_timer;
106 	struct rtc_timer uie_rtctimer;
107 	struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
108 	int pie_enabled;
109 	struct work_struct irqwork;
110 	/* Some hardware can't support UIE mode */
111 	int uie_unsupported;
112 
113 	/* Number of nsec it takes to set the RTC clock. This influences when
114 	 * the set ops are called. An offset:
115 	 *   - of 0.5 s will call RTC set for wall clock time 10.0 s at 9.5 s
116 	 *   - of 1.5 s will call RTC set for wall clock time 10.0 s at 8.5 s
117 	 *   - of -0.5 s will call RTC set for wall clock time 10.0 s at 10.5 s
118 	 */
119 	long set_offset_nsec;
120 
121 	time64_t range_min;
122 	timeu64_t range_max;
123 	time64_t start_secs;
124 	time64_t offset_secs;
125 	bool set_start_time;
126 
127 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
128 	struct work_struct uie_task;
129 	struct timer_list uie_timer;
130 	/* Those fields are protected by rtc->irq_lock */
131 	unsigned int oldsecs;
132 	unsigned int uie_irq_active:1;
133 	unsigned int stop_uie_polling:1;
134 	unsigned int uie_task_active:1;
135 	unsigned int uie_timer_active:1;
136 #endif
137 };
138 #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
139 
140 #define rtc_lock(d) mutex_lock(&d->ops_lock)
141 #define rtc_unlock(d) mutex_unlock(&d->ops_lock)
142 
143 /* useful timestamps */
144 #define RTC_TIMESTAMP_BEGIN_0000	-62167219200ULL /* 0000-01-01 00:00:00 */
145 #define RTC_TIMESTAMP_BEGIN_1900	-2208988800LL /* 1900-01-01 00:00:00 */
146 #define RTC_TIMESTAMP_BEGIN_2000	946684800LL /* 2000-01-01 00:00:00 */
147 #define RTC_TIMESTAMP_END_2063		2966371199LL /* 2063-12-31 23:59:59 */
148 #define RTC_TIMESTAMP_END_2079		3471292799LL /* 2079-12-31 23:59:59 */
149 #define RTC_TIMESTAMP_END_2099		4102444799LL /* 2099-12-31 23:59:59 */
150 #define RTC_TIMESTAMP_END_2199		7258118399LL /* 2199-12-31 23:59:59 */
151 #define RTC_TIMESTAMP_END_9999		253402300799LL /* 9999-12-31 23:59:59 */
152 
153 extern struct rtc_device *devm_rtc_device_register(struct device *dev,
154 					const char *name,
155 					const struct rtc_class_ops *ops,
156 					struct module *owner);
157 struct rtc_device *devm_rtc_allocate_device(struct device *dev);
158 int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc);
159 
160 extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
161 extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
162 extern int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec);
163 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
164 extern int rtc_read_alarm(struct rtc_device *rtc,
165 			struct rtc_wkalrm *alrm);
166 extern int rtc_set_alarm(struct rtc_device *rtc,
167 				struct rtc_wkalrm *alrm);
168 extern int rtc_initialize_alarm(struct rtc_device *rtc,
169 				struct rtc_wkalrm *alrm);
170 extern void rtc_update_irq(struct rtc_device *rtc,
171 			unsigned long num, unsigned long events);
172 
173 extern struct rtc_device *rtc_class_open(const char *name);
174 extern void rtc_class_close(struct rtc_device *rtc);
175 
176 extern int rtc_irq_set_state(struct rtc_device *rtc, int enabled);
177 extern int rtc_irq_set_freq(struct rtc_device *rtc, int freq);
178 extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
179 extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
180 extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
181 						unsigned int enabled);
182 
183 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode);
184 void rtc_aie_update_irq(struct rtc_device *rtc);
185 void rtc_uie_update_irq(struct rtc_device *rtc);
186 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer);
187 
188 void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
189 		    struct rtc_device *rtc);
190 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
191 		    ktime_t expires, ktime_t period);
192 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
193 int rtc_read_offset(struct rtc_device *rtc, long *offset);
194 int rtc_set_offset(struct rtc_device *rtc, long offset);
195 void rtc_timer_do_work(struct work_struct *work);
196 
197 static inline bool is_leap_year(unsigned int year)
198 {
199 	return (!(year % 4) && (year % 100)) || !(year % 400);
200 }
201 
202 /* Determine if we can call to driver to set the time. Drivers can only be
203  * called to set a second aligned time value, and the field set_offset_nsec
204  * specifies how far away from the second aligned time to call the driver.
205  *
206  * This also computes 'to_set' which is the time we are trying to set, and has
207  * a zero in tv_nsecs, such that:
208  *    to_set - set_delay_nsec == now +/- FUZZ
209  *
210  */
211 static inline bool rtc_tv_nsec_ok(s64 set_offset_nsec,
212 				  struct timespec64 *to_set,
213 				  const struct timespec64 *now)
214 {
215 	/* Allowed error in tv_nsec, arbitarily set to 5 jiffies in ns. */
216 	const unsigned long TIME_SET_NSEC_FUZZ = TICK_NSEC * 5;
217 	struct timespec64 delay = {.tv_sec = 0,
218 				   .tv_nsec = set_offset_nsec};
219 
220 	*to_set = timespec64_add(*now, delay);
221 
222 	if (to_set->tv_nsec < TIME_SET_NSEC_FUZZ) {
223 		to_set->tv_nsec = 0;
224 		return true;
225 	}
226 
227 	if (to_set->tv_nsec > NSEC_PER_SEC - TIME_SET_NSEC_FUZZ) {
228 		to_set->tv_sec++;
229 		to_set->tv_nsec = 0;
230 		return true;
231 	}
232 	return false;
233 }
234 
235 #define devm_rtc_register_device(device) \
236 	__devm_rtc_register_device(THIS_MODULE, device)
237 
238 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
239 extern int rtc_hctosys_ret;
240 #else
241 #define rtc_hctosys_ret -ENODEV
242 #endif
243 
244 #ifdef CONFIG_RTC_NVMEM
245 int devm_rtc_nvmem_register(struct rtc_device *rtc,
246 			    struct nvmem_config *nvmem_config);
247 #else
248 static inline int devm_rtc_nvmem_register(struct rtc_device *rtc,
249 					  struct nvmem_config *nvmem_config)
250 {
251 	return 0;
252 }
253 #endif
254 
255 #ifdef CONFIG_RTC_INTF_SYSFS
256 int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp);
257 int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps);
258 #else
259 static inline
260 int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
261 {
262 	return 0;
263 }
264 
265 static inline
266 int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
267 {
268 	return 0;
269 }
270 #endif
271 #endif /* _LINUX_RTC_H_ */
272