rtc.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #include <linux/types.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/nvmem-provider.h>
  17. #include <linux/android_kabi.h>
  18. #include <uapi/linux/rtc.h>
  19. extern int rtc_month_days(unsigned int month, unsigned int year);
  20. extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
  21. extern int rtc_valid_tm(struct rtc_time *tm);
  22. extern time64_t rtc_tm_to_time64(struct rtc_time *tm);
  23. extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm);
  24. ktime_t rtc_tm_to_ktime(struct rtc_time tm);
  25. struct rtc_time rtc_ktime_to_tm(ktime_t kt);
  26. /*
  27. * rtc_tm_sub - Return the difference in seconds.
  28. */
  29. static inline time64_t rtc_tm_sub(struct rtc_time *lhs, struct rtc_time *rhs)
  30. {
  31. return rtc_tm_to_time64(lhs) - rtc_tm_to_time64(rhs);
  32. }
  33. #include <linux/device.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/cdev.h>
  36. #include <linux/poll.h>
  37. #include <linux/mutex.h>
  38. #include <linux/timerqueue.h>
  39. #include <linux/workqueue.h>
  40. extern struct class *rtc_class;
  41. /*
  42. * For these RTC methods the device parameter is the physical device
  43. * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
  44. * was passed to rtc_device_register(). Its driver_data normally holds
  45. * device state, including the rtc_device pointer for the RTC.
  46. *
  47. * Most of these methods are called with rtc_device.ops_lock held,
  48. * through the rtc_*(struct rtc_device *, ...) calls.
  49. *
  50. * The (current) exceptions are mostly filesystem hooks:
  51. * - the proc() hook for procfs
  52. */
  53. struct rtc_class_ops {
  54. int (*ioctl)(struct device *, unsigned int, unsigned long);
  55. int (*read_time)(struct device *, struct rtc_time *);
  56. int (*set_time)(struct device *, struct rtc_time *);
  57. int (*read_alarm)(struct device *, struct rtc_wkalrm *);
  58. int (*set_alarm)(struct device *, struct rtc_wkalrm *);
  59. int (*proc)(struct device *, struct seq_file *);
  60. int (*alarm_irq_enable)(struct device *, unsigned int enabled);
  61. int (*read_offset)(struct device *, long *offset);
  62. int (*set_offset)(struct device *, long offset);
  63. int (*param_get)(struct device *, struct rtc_param *param);
  64. int (*param_set)(struct device *, struct rtc_param *param);
  65. ANDROID_KABI_RESERVE(1);
  66. };
  67. struct rtc_device;
  68. struct rtc_timer {
  69. struct timerqueue_node node;
  70. ktime_t period;
  71. void (*func)(struct rtc_device *rtc);
  72. struct rtc_device *rtc;
  73. int enabled;
  74. };
  75. /* flags */
  76. #define RTC_DEV_BUSY 0
  77. #define RTC_NO_CDEV 1
  78. struct rtc_device {
  79. struct device dev;
  80. struct module *owner;
  81. int id;
  82. const struct rtc_class_ops *ops;
  83. struct mutex ops_lock;
  84. struct cdev char_dev;
  85. unsigned long flags;
  86. unsigned long irq_data;
  87. spinlock_t irq_lock;
  88. wait_queue_head_t irq_queue;
  89. struct fasync_struct *async_queue;
  90. int irq_freq;
  91. int max_user_freq;
  92. struct timerqueue_head timerqueue;
  93. struct rtc_timer aie_timer;
  94. struct rtc_timer uie_rtctimer;
  95. struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
  96. int pie_enabled;
  97. struct work_struct irqwork;
  98. /*
  99. * This offset specifies the update timing of the RTC.
  100. *
  101. * tsched t1 write(t2.tv_sec - 1sec)) t2 RTC increments seconds
  102. *
  103. * The offset defines how tsched is computed so that the write to
  104. * the RTC (t2.tv_sec - 1sec) is correct versus the time required
  105. * for the transport of the write and the time which the RTC needs
  106. * to increment seconds the first time after the write (t2).
  107. *
  108. * For direct accessible RTCs tsched ~= t1 because the write time
  109. * is negligible. For RTCs behind slow busses the transport time is
  110. * significant and has to be taken into account.
  111. *
  112. * The time between the write (t1) and the first increment after
  113. * the write (t2) is RTC specific. For a MC146818 RTC it's 500ms,
  114. * for many others it's exactly 1 second. Consult the datasheet.
  115. *
  116. * The value of this offset is also used to calculate the to be
  117. * written value (t2.tv_sec - 1sec) at tsched.
  118. *
  119. * The default value for this is NSEC_PER_SEC + 10 msec default
  120. * transport time. The offset can be adjusted by drivers so the
  121. * calculation for the to be written value at tsched becomes
  122. * correct:
  123. *
  124. * newval = tsched + set_offset_nsec - NSEC_PER_SEC
  125. * and (tsched + set_offset_nsec) % NSEC_PER_SEC == 0
  126. */
  127. unsigned long set_offset_nsec;
  128. unsigned long features[BITS_TO_LONGS(RTC_FEATURE_CNT)];
  129. time64_t range_min;
  130. timeu64_t range_max;
  131. time64_t start_secs;
  132. time64_t offset_secs;
  133. bool set_start_time;
  134. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  135. struct work_struct uie_task;
  136. struct timer_list uie_timer;
  137. /* Those fields are protected by rtc->irq_lock */
  138. unsigned int oldsecs;
  139. unsigned int uie_irq_active:1;
  140. unsigned int stop_uie_polling:1;
  141. unsigned int uie_task_active:1;
  142. unsigned int uie_timer_active:1;
  143. #endif
  144. ANDROID_KABI_RESERVE(1);
  145. };
  146. #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
  147. #define rtc_lock(d) mutex_lock(&d->ops_lock)
  148. #define rtc_unlock(d) mutex_unlock(&d->ops_lock)
  149. /* useful timestamps */
  150. #define RTC_TIMESTAMP_BEGIN_0000 -62167219200ULL /* 0000-01-01 00:00:00 */
  151. #define RTC_TIMESTAMP_BEGIN_1900 -2208988800LL /* 1900-01-01 00:00:00 */
  152. #define RTC_TIMESTAMP_BEGIN_2000 946684800LL /* 2000-01-01 00:00:00 */
  153. #define RTC_TIMESTAMP_END_2063 2966371199LL /* 2063-12-31 23:59:59 */
  154. #define RTC_TIMESTAMP_END_2079 3471292799LL /* 2079-12-31 23:59:59 */
  155. #define RTC_TIMESTAMP_END_2099 4102444799LL /* 2099-12-31 23:59:59 */
  156. #define RTC_TIMESTAMP_END_2199 7258118399LL /* 2199-12-31 23:59:59 */
  157. #define RTC_TIMESTAMP_END_9999 253402300799LL /* 9999-12-31 23:59:59 */
  158. extern struct rtc_device *devm_rtc_device_register(struct device *dev,
  159. const char *name,
  160. const struct rtc_class_ops *ops,
  161. struct module *owner);
  162. struct rtc_device *devm_rtc_allocate_device(struct device *dev);
  163. int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc);
  164. extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
  165. extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
  166. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
  167. extern int rtc_read_alarm(struct rtc_device *rtc,
  168. struct rtc_wkalrm *alrm);
  169. extern int rtc_set_alarm(struct rtc_device *rtc,
  170. struct rtc_wkalrm *alrm);
  171. extern int rtc_initialize_alarm(struct rtc_device *rtc,
  172. struct rtc_wkalrm *alrm);
  173. extern void rtc_update_irq(struct rtc_device *rtc,
  174. unsigned long num, unsigned long events);
  175. extern struct rtc_device *rtc_class_open(const char *name);
  176. extern void rtc_class_close(struct rtc_device *rtc);
  177. extern int rtc_irq_set_state(struct rtc_device *rtc, int enabled);
  178. extern int rtc_irq_set_freq(struct rtc_device *rtc, int freq);
  179. extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  180. extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  181. extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
  182. unsigned int enabled);
  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. void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
  188. struct rtc_device *rtc);
  189. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  190. ktime_t expires, ktime_t period);
  191. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
  192. int rtc_read_offset(struct rtc_device *rtc, long *offset);
  193. int rtc_set_offset(struct rtc_device *rtc, long offset);
  194. void rtc_timer_do_work(struct work_struct *work);
  195. static inline bool is_leap_year(unsigned int year)
  196. {
  197. return (!(year % 4) && (year % 100)) || !(year % 400);
  198. }
  199. #define devm_rtc_register_device(device) \
  200. __devm_rtc_register_device(THIS_MODULE, device)
  201. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  202. extern int rtc_hctosys_ret;
  203. #else
  204. #define rtc_hctosys_ret -ENODEV
  205. #endif
  206. #ifdef CONFIG_RTC_NVMEM
  207. int devm_rtc_nvmem_register(struct rtc_device *rtc,
  208. struct nvmem_config *nvmem_config);
  209. #else
  210. static inline int devm_rtc_nvmem_register(struct rtc_device *rtc,
  211. struct nvmem_config *nvmem_config)
  212. {
  213. return 0;
  214. }
  215. #endif
  216. #ifdef CONFIG_RTC_INTF_SYSFS
  217. int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp);
  218. int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps);
  219. #else
  220. static inline
  221. int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
  222. {
  223. return 0;
  224. }
  225. static inline
  226. int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
  227. {
  228. return 0;
  229. }
  230. #endif
  231. #endif /* _LINUX_RTC_H_ */