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