rtc.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. * Deprecated. Use rtc_time64_to_tm().
  25. */
  26. static inline void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
  27. {
  28. rtc_time64_to_tm(time, tm);
  29. }
  30. /**
  31. * Deprecated. Use rtc_tm_to_time64().
  32. */
  33. static inline int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
  34. {
  35. *time = rtc_tm_to_time64(tm);
  36. return 0;
  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. extern struct class *rtc_class;
  46. /*
  47. * For these RTC methods the device parameter is the physical device
  48. * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
  49. * was passed to rtc_device_register(). Its driver_data normally holds
  50. * device state, including the rtc_device pointer for the RTC.
  51. *
  52. * Most of these methods are called with rtc_device.ops_lock held,
  53. * through the rtc_*(struct rtc_device *, ...) calls.
  54. *
  55. * The (current) exceptions are mostly filesystem hooks:
  56. * - the proc() hook for procfs
  57. * - non-ioctl() chardev hooks: open(), release(), read_callback()
  58. *
  59. * REVISIT those periodic irq calls *do* have ops_lock when they're
  60. * issued through ioctl() ...
  61. */
  62. struct rtc_class_ops {
  63. int (*open)(struct device *);
  64. void (*release)(struct device *);
  65. int (*ioctl)(struct device *, unsigned int, unsigned long);
  66. int (*read_time)(struct device *, struct rtc_time *);
  67. int (*set_time)(struct device *, struct rtc_time *);
  68. int (*read_alarm)(struct device *, struct rtc_wkalrm *);
  69. int (*set_alarm)(struct device *, struct rtc_wkalrm *);
  70. int (*proc)(struct device *, struct seq_file *);
  71. int (*set_mmss)(struct device *, unsigned long secs);
  72. int (*read_callback)(struct device *, int data);
  73. int (*alarm_irq_enable)(struct device *, unsigned int enabled);
  74. };
  75. #define RTC_DEVICE_NAME_SIZE 20
  76. typedef struct rtc_task {
  77. void (*func)(void *private_data);
  78. void *private_data;
  79. } rtc_task_t;
  80. struct rtc_timer {
  81. struct rtc_task task;
  82. struct timerqueue_node node;
  83. ktime_t period;
  84. int enabled;
  85. };
  86. /* flags */
  87. #define RTC_DEV_BUSY 0
  88. struct rtc_device
  89. {
  90. struct device dev;
  91. struct module *owner;
  92. int id;
  93. char name[RTC_DEVICE_NAME_SIZE];
  94. const struct rtc_class_ops *ops;
  95. struct mutex ops_lock;
  96. struct cdev char_dev;
  97. unsigned long flags;
  98. unsigned long irq_data;
  99. spinlock_t irq_lock;
  100. wait_queue_head_t irq_queue;
  101. struct fasync_struct *async_queue;
  102. struct rtc_task *irq_task;
  103. spinlock_t irq_task_lock;
  104. int irq_freq;
  105. int max_user_freq;
  106. struct timerqueue_head timerqueue;
  107. struct rtc_timer aie_timer;
  108. struct rtc_timer uie_rtctimer;
  109. struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
  110. int pie_enabled;
  111. struct work_struct irqwork;
  112. /* Some hardware can't support UIE mode */
  113. int uie_unsupported;
  114. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  115. struct work_struct uie_task;
  116. struct timer_list uie_timer;
  117. /* Those fields are protected by rtc->irq_lock */
  118. unsigned int oldsecs;
  119. unsigned int uie_irq_active:1;
  120. unsigned int stop_uie_polling:1;
  121. unsigned int uie_task_active:1;
  122. unsigned int uie_timer_active:1;
  123. #endif
  124. };
  125. #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
  126. extern struct rtc_device *rtc_device_register(const char *name,
  127. struct device *dev,
  128. const struct rtc_class_ops *ops,
  129. struct module *owner);
  130. extern struct rtc_device *devm_rtc_device_register(struct device *dev,
  131. const char *name,
  132. const struct rtc_class_ops *ops,
  133. struct module *owner);
  134. extern void rtc_device_unregister(struct rtc_device *rtc);
  135. extern void devm_rtc_device_unregister(struct device *dev,
  136. struct rtc_device *rtc);
  137. extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
  138. extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
  139. extern int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs);
  140. extern int rtc_set_ntp_time(struct timespec64 now);
  141. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
  142. extern int rtc_read_alarm(struct rtc_device *rtc,
  143. struct rtc_wkalrm *alrm);
  144. extern int rtc_set_alarm(struct rtc_device *rtc,
  145. struct rtc_wkalrm *alrm);
  146. extern int rtc_initialize_alarm(struct rtc_device *rtc,
  147. struct rtc_wkalrm *alrm);
  148. extern void rtc_update_irq(struct rtc_device *rtc,
  149. unsigned long num, unsigned long events);
  150. extern struct rtc_device *rtc_class_open(const char *name);
  151. extern void rtc_class_close(struct rtc_device *rtc);
  152. extern int rtc_irq_register(struct rtc_device *rtc,
  153. struct rtc_task *task);
  154. extern void rtc_irq_unregister(struct rtc_device *rtc,
  155. struct rtc_task *task);
  156. extern int rtc_irq_set_state(struct rtc_device *rtc,
  157. struct rtc_task *task, int enabled);
  158. extern int rtc_irq_set_freq(struct rtc_device *rtc,
  159. struct rtc_task *task, int freq);
  160. extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  161. extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  162. extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
  163. unsigned int enabled);
  164. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode);
  165. void rtc_aie_update_irq(void *private);
  166. void rtc_uie_update_irq(void *private);
  167. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer);
  168. int rtc_register(rtc_task_t *task);
  169. int rtc_unregister(rtc_task_t *task);
  170. int rtc_control(rtc_task_t *t, unsigned int cmd, unsigned long arg);
  171. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data);
  172. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
  173. ktime_t expires, ktime_t period);
  174. int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer);
  175. void rtc_timer_do_work(struct work_struct *work);
  176. static inline bool is_leap_year(unsigned int year)
  177. {
  178. return (!(year % 4) && (year % 100)) || !(year % 400);
  179. }
  180. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  181. extern int rtc_hctosys_ret;
  182. #else
  183. #define rtc_hctosys_ret -ENODEV
  184. #endif
  185. #endif /* _LINUX_RTC_H_ */