rtc.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #include <linux/types.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/nvmem-provider.h>
  17. #include <uapi/linux/rtc.h>
  18. extern int rtc_month_days(unsigned int month, unsigned int year);
  19. extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
  20. extern int rtc_valid_tm(struct rtc_time *tm);
  21. extern time64_t rtc_tm_to_time64(struct rtc_time *tm);
  22. extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm);
  23. ktime_t rtc_tm_to_ktime(struct rtc_time tm);
  24. struct rtc_time rtc_ktime_to_tm(ktime_t kt);
  25. /*
  26. * rtc_tm_sub - Return the difference in seconds.
  27. */
  28. static inline time64_t rtc_tm_sub(struct rtc_time *lhs, struct rtc_time *rhs)
  29. {
  30. return rtc_tm_to_time64(lhs) - rtc_tm_to_time64(rhs);
  31. }
  32. static inline void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
  33. {
  34. rtc_time64_to_tm(time, tm);
  35. }
  36. static inline int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
  37. {
  38. *time = rtc_tm_to_time64(tm);
  39. return 0;
  40. }
  41. #include <linux/device.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/cdev.h>
  44. #include <linux/poll.h>
  45. #include <linux/mutex.h>
  46. #include <linux/timerqueue.h>
  47. #include <linux/workqueue.h>
  48. extern struct class *rtc_class;
  49. /*
  50. * For these RTC methods the device parameter is the physical device
  51. * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
  52. * was passed to rtc_device_register(). Its driver_data normally holds
  53. * device state, including the rtc_device pointer for the RTC.
  54. *
  55. * Most of these methods are called with rtc_device.ops_lock held,
  56. * through the rtc_*(struct rtc_device *, ...) calls.
  57. *
  58. * The (current) exceptions are mostly filesystem hooks:
  59. * - the proc() hook for procfs
  60. * - non-ioctl() chardev hooks: open(), release(), read_callback()
  61. *
  62. * REVISIT those periodic irq calls *do* have ops_lock when they're
  63. * issued through ioctl() ...
  64. */
  65. struct rtc_class_ops {
  66. int (*ioctl)(struct device *, unsigned int, unsigned long);
  67. int (*read_time)(struct device *, struct rtc_time *);
  68. int (*set_time)(struct device *, struct rtc_time *);
  69. int (*read_alarm)(struct device *, struct rtc_wkalrm *);
  70. int (*set_alarm)(struct device *, struct rtc_wkalrm *);
  71. int (*proc)(struct device *, struct seq_file *);
  72. int (*set_mmss64)(struct device *, time64_t secs);
  73. int (*set_mmss)(struct device *, unsigned long secs);
  74. int (*read_callback)(struct device *, int data);
  75. int (*alarm_irq_enable)(struct device *, unsigned int enabled);
  76. int (*read_offset)(struct device *, long *offset);
  77. int (*set_offset)(struct device *, long offset);
  78. };
  79. struct rtc_timer {
  80. struct timerqueue_node node;
  81. ktime_t period;
  82. void (*func)(void *private_data);
  83. void *private_data;
  84. int enabled;
  85. };
  86. /* flags */
  87. #define RTC_DEV_BUSY 0
  88. struct rtc_device {
  89. struct device dev;
  90. struct module *owner;
  91. int id;
  92. const struct rtc_class_ops *ops;
  93. struct mutex ops_lock;
  94. struct cdev char_dev;
  95. unsigned long flags;
  96. unsigned long irq_data;
  97. spinlock_t irq_lock;
  98. wait_queue_head_t irq_queue;
  99. struct fasync_struct *async_queue;
  100. int irq_freq;
  101. int max_user_freq;
  102. struct timerqueue_head timerqueue;
  103. struct rtc_timer aie_timer;
  104. struct rtc_timer uie_rtctimer;
  105. struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
  106. int pie_enabled;
  107. struct work_struct irqwork;
  108. /* Some hardware can't support UIE mode */
  109. int uie_unsupported;
  110. /* Number of nsec it takes to set the RTC clock. This influences when
  111. * the set ops are called. An offset:
  112. * - of 0.5 s will call RTC set for wall clock time 10.0 s at 9.5 s
  113. * - of 1.5 s will call RTC set for wall clock time 10.0 s at 8.5 s
  114. * - of -0.5 s will call RTC set for wall clock time 10.0 s at 10.5 s
  115. */
  116. long set_offset_nsec;
  117. bool registered;
  118. struct nvmem_device *nvmem;
  119. /* Old ABI support */
  120. bool nvram_old_abi;
  121. struct bin_attribute *nvram;
  122. time64_t range_min;
  123. timeu64_t range_max;
  124. time64_t start_secs;
  125. time64_t offset_secs;
  126. bool set_start_time;
  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. /* useful timestamps */
  140. #define RTC_TIMESTAMP_BEGIN_1900 -2208988800LL /* 1900-01-01 00:00:00 */
  141. #define RTC_TIMESTAMP_BEGIN_2000 946684800LL /* 2000-01-01 00:00:00 */
  142. #define RTC_TIMESTAMP_END_2099 4102444799LL /* 2099-12-31 23:59:59 */
  143. extern struct rtc_device *rtc_device_register(const char *name,
  144. struct device *dev,
  145. const struct rtc_class_ops *ops,
  146. struct module *owner);
  147. extern struct rtc_device *devm_rtc_device_register(struct device *dev,
  148. const char *name,
  149. const struct rtc_class_ops *ops,
  150. struct module *owner);
  151. struct rtc_device *devm_rtc_allocate_device(struct device *dev);
  152. int __rtc_register_device(struct module *owner, struct rtc_device *rtc);
  153. extern void rtc_device_unregister(struct rtc_device *rtc);
  154. extern void devm_rtc_device_unregister(struct device *dev,
  155. struct rtc_device *rtc);
  156. extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
  157. extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
  158. extern int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec);
  159. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
  160. extern int rtc_read_alarm(struct rtc_device *rtc,
  161. struct rtc_wkalrm *alrm);
  162. extern int rtc_set_alarm(struct rtc_device *rtc,
  163. struct rtc_wkalrm *alrm);
  164. extern int rtc_initialize_alarm(struct rtc_device *rtc,
  165. struct rtc_wkalrm *alrm);
  166. extern void rtc_update_irq(struct rtc_device *rtc,
  167. unsigned long num, unsigned long events);
  168. extern struct rtc_device *rtc_class_open(const char *name);
  169. extern void rtc_class_close(struct rtc_device *rtc);
  170. extern int rtc_irq_set_state(struct rtc_device *rtc, int enabled);
  171. extern int rtc_irq_set_freq(struct rtc_device *rtc, int freq);
  172. extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  173. extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  174. extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
  175. unsigned int enabled);
  176. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode);
  177. void rtc_aie_update_irq(void *private);
  178. void rtc_uie_update_irq(void *private);
  179. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer);
  180. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data);
  181. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  182. ktime_t expires, ktime_t period);
  183. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
  184. int rtc_read_offset(struct rtc_device *rtc, long *offset);
  185. int rtc_set_offset(struct rtc_device *rtc, long offset);
  186. void rtc_timer_do_work(struct work_struct *work);
  187. static inline bool is_leap_year(unsigned int year)
  188. {
  189. return (!(year % 4) && (year % 100)) || !(year % 400);
  190. }
  191. /* Determine if we can call to driver to set the time. Drivers can only be
  192. * called to set a second aligned time value, and the field set_offset_nsec
  193. * specifies how far away from the second aligned time to call the driver.
  194. *
  195. * This also computes 'to_set' which is the time we are trying to set, and has
  196. * a zero in tv_nsecs, such that:
  197. * to_set - set_delay_nsec == now +/- FUZZ
  198. *
  199. */
  200. static inline bool rtc_tv_nsec_ok(s64 set_offset_nsec,
  201. struct timespec64 *to_set,
  202. const struct timespec64 *now)
  203. {
  204. /* Allowed error in tv_nsec, arbitarily set to 5 jiffies in ns. */
  205. const unsigned long TIME_SET_NSEC_FUZZ = TICK_NSEC * 5;
  206. struct timespec64 delay = {.tv_sec = 0,
  207. .tv_nsec = set_offset_nsec};
  208. *to_set = timespec64_add(*now, delay);
  209. if (to_set->tv_nsec < TIME_SET_NSEC_FUZZ) {
  210. to_set->tv_nsec = 0;
  211. return true;
  212. }
  213. if (to_set->tv_nsec > NSEC_PER_SEC - TIME_SET_NSEC_FUZZ) {
  214. to_set->tv_sec++;
  215. to_set->tv_nsec = 0;
  216. return true;
  217. }
  218. return false;
  219. }
  220. #define rtc_register_device(device) \
  221. __rtc_register_device(THIS_MODULE, device)
  222. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  223. extern int rtc_hctosys_ret;
  224. #else
  225. #define rtc_hctosys_ret -ENODEV
  226. #endif
  227. #ifdef CONFIG_RTC_NVMEM
  228. int rtc_nvmem_register(struct rtc_device *rtc,
  229. struct nvmem_config *nvmem_config);
  230. void rtc_nvmem_unregister(struct rtc_device *rtc);
  231. #else
  232. static inline int rtc_nvmem_register(struct rtc_device *rtc,
  233. struct nvmem_config *nvmem_config)
  234. {
  235. return 0;
  236. }
  237. static inline void rtc_nvmem_unregister(struct rtc_device *rtc) {}
  238. #endif
  239. #endif /* _LINUX_RTC_H_ */