rtc.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Generic RTC interface.
  3. * This version contains the part of the user interface to the Real Time Clock
  4. * service. It is used with both the legacy mc146818 and also EFI
  5. * Struct rtc_time and first 12 ioctl by Paul Gortmaker, 1996 - separated out
  6. * from <linux/mc146818rtc.h> to this file for 2.4 kernels.
  7. *
  8. * Copyright (C) 1999 Hewlett-Packard Co.
  9. * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
  10. */
  11. #ifndef _LINUX_RTC_H_
  12. #define _LINUX_RTC_H_
  13. #include <linux/types.h>
  14. #include <linux/interrupt.h>
  15. #include <uapi/linux/rtc.h>
  16. extern int rtc_month_days(unsigned int month, unsigned int year);
  17. extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
  18. extern int rtc_valid_tm(struct rtc_time *tm);
  19. extern time64_t rtc_tm_to_time64(struct rtc_time *tm);
  20. extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm);
  21. ktime_t rtc_tm_to_ktime(struct rtc_time tm);
  22. struct rtc_time rtc_ktime_to_tm(ktime_t kt);
  23. /*
  24. * rtc_tm_sub - Return the difference in seconds.
  25. */
  26. static inline time64_t rtc_tm_sub(struct rtc_time *lhs, struct rtc_time *rhs)
  27. {
  28. return rtc_tm_to_time64(lhs) - rtc_tm_to_time64(rhs);
  29. }
  30. /**
  31. * Deprecated. Use rtc_time64_to_tm().
  32. */
  33. static inline void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
  34. {
  35. rtc_time64_to_tm(time, tm);
  36. }
  37. /**
  38. * Deprecated. Use rtc_tm_to_time64().
  39. */
  40. static inline int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
  41. {
  42. *time = rtc_tm_to_time64(tm);
  43. return 0;
  44. }
  45. #include <linux/device.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/cdev.h>
  48. #include <linux/poll.h>
  49. #include <linux/mutex.h>
  50. #include <linux/timerqueue.h>
  51. #include <linux/workqueue.h>
  52. extern struct class *rtc_class;
  53. /*
  54. * For these RTC methods the device parameter is the physical device
  55. * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
  56. * was passed to rtc_device_register(). Its driver_data normally holds
  57. * device state, including the rtc_device pointer for the RTC.
  58. *
  59. * Most of these methods are called with rtc_device.ops_lock held,
  60. * through the rtc_*(struct rtc_device *, ...) calls.
  61. *
  62. * The (current) exceptions are mostly filesystem hooks:
  63. * - the proc() hook for procfs
  64. * - non-ioctl() chardev hooks: open(), release(), read_callback()
  65. *
  66. * REVISIT those periodic irq calls *do* have ops_lock when they're
  67. * issued through ioctl() ...
  68. */
  69. struct rtc_class_ops {
  70. int (*open)(struct device *);
  71. void (*release)(struct device *);
  72. int (*ioctl)(struct device *, unsigned int, unsigned long);
  73. int (*read_time)(struct device *, struct rtc_time *);
  74. int (*set_time)(struct device *, struct rtc_time *);
  75. int (*read_alarm)(struct device *, struct rtc_wkalrm *);
  76. int (*set_alarm)(struct device *, struct rtc_wkalrm *);
  77. int (*proc)(struct device *, struct seq_file *);
  78. int (*set_mmss64)(struct device *, time64_t secs);
  79. int (*set_mmss)(struct device *, unsigned long secs);
  80. int (*read_callback)(struct device *, int data);
  81. int (*alarm_irq_enable)(struct device *, unsigned int enabled);
  82. };
  83. #define RTC_DEVICE_NAME_SIZE 20
  84. typedef struct rtc_task {
  85. void (*func)(void *private_data);
  86. void *private_data;
  87. } rtc_task_t;
  88. struct rtc_timer {
  89. struct rtc_task task;
  90. struct timerqueue_node node;
  91. ktime_t period;
  92. int enabled;
  93. };
  94. /* flags */
  95. #define RTC_DEV_BUSY 0
  96. struct rtc_device {
  97. struct device dev;
  98. struct module *owner;
  99. int id;
  100. char name[RTC_DEVICE_NAME_SIZE];
  101. const struct rtc_class_ops *ops;
  102. struct mutex ops_lock;
  103. struct cdev char_dev;
  104. unsigned long flags;
  105. unsigned long irq_data;
  106. spinlock_t irq_lock;
  107. wait_queue_head_t irq_queue;
  108. struct fasync_struct *async_queue;
  109. struct rtc_task *irq_task;
  110. spinlock_t irq_task_lock;
  111. int irq_freq;
  112. int max_user_freq;
  113. struct timerqueue_head timerqueue;
  114. struct rtc_timer aie_timer;
  115. struct rtc_timer uie_rtctimer;
  116. struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
  117. int pie_enabled;
  118. struct work_struct irqwork;
  119. /* Some hardware can't support UIE mode */
  120. int uie_unsupported;
  121. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  122. struct work_struct uie_task;
  123. struct timer_list uie_timer;
  124. /* Those fields are protected by rtc->irq_lock */
  125. unsigned int oldsecs;
  126. unsigned int uie_irq_active:1;
  127. unsigned int stop_uie_polling:1;
  128. unsigned int uie_task_active:1;
  129. unsigned int uie_timer_active:1;
  130. #endif
  131. };
  132. #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
  133. extern struct rtc_device *rtc_device_register(const char *name,
  134. struct device *dev,
  135. const struct rtc_class_ops *ops,
  136. struct module *owner);
  137. extern struct rtc_device *devm_rtc_device_register(struct device *dev,
  138. const char *name,
  139. const struct rtc_class_ops *ops,
  140. struct module *owner);
  141. extern void rtc_device_unregister(struct rtc_device *rtc);
  142. extern void devm_rtc_device_unregister(struct device *dev,
  143. struct rtc_device *rtc);
  144. extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
  145. extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
  146. extern int rtc_set_ntp_time(struct timespec64 now);
  147. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
  148. extern int rtc_read_alarm(struct rtc_device *rtc,
  149. struct rtc_wkalrm *alrm);
  150. extern int rtc_set_alarm(struct rtc_device *rtc,
  151. struct rtc_wkalrm *alrm);
  152. extern int rtc_initialize_alarm(struct rtc_device *rtc,
  153. struct rtc_wkalrm *alrm);
  154. extern void rtc_update_irq(struct rtc_device *rtc,
  155. unsigned long num, unsigned long events);
  156. extern struct rtc_device *rtc_class_open(const char *name);
  157. extern void rtc_class_close(struct rtc_device *rtc);
  158. extern int rtc_irq_register(struct rtc_device *rtc,
  159. struct rtc_task *task);
  160. extern void rtc_irq_unregister(struct rtc_device *rtc,
  161. struct rtc_task *task);
  162. extern int rtc_irq_set_state(struct rtc_device *rtc,
  163. struct rtc_task *task, int enabled);
  164. extern int rtc_irq_set_freq(struct rtc_device *rtc,
  165. struct rtc_task *task, int freq);
  166. extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  167. extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  168. extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
  169. unsigned int enabled);
  170. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode);
  171. void rtc_aie_update_irq(void *private);
  172. void rtc_uie_update_irq(void *private);
  173. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer);
  174. int rtc_register(rtc_task_t *task);
  175. int rtc_unregister(rtc_task_t *task);
  176. int rtc_control(rtc_task_t *t, unsigned int cmd, unsigned long arg);
  177. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data);
  178. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  179. ktime_t expires, ktime_t period);
  180. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
  181. void rtc_timer_do_work(struct work_struct *work);
  182. static inline bool is_leap_year(unsigned int year)
  183. {
  184. return (!(year % 4) && (year % 100)) || !(year % 400);
  185. }
  186. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  187. extern int rtc_hctosys_ret;
  188. #else
  189. #define rtc_hctosys_ret -ENODEV
  190. #endif
  191. #endif /* _LINUX_RTC_H_ */