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