rtc.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. int (*read_offset)(struct device *, long *offset);
  83. int (*set_offset)(struct device *, long offset);
  84. int (*read_scratch)(struct device *, unsigned int, u32*);
  85. int (*write_scratch)(struct device *, unsigned int, u32);
  86. void (*power_off_program)(struct device *);
  87. unsigned int scratch_size;
  88. };
  89. #define RTC_DEVICE_NAME_SIZE 20
  90. typedef struct rtc_task {
  91. void (*func)(void *private_data);
  92. void *private_data;
  93. } rtc_task_t;
  94. struct rtc_timer {
  95. struct rtc_task task;
  96. struct timerqueue_node node;
  97. ktime_t period;
  98. int enabled;
  99. };
  100. /* flags */
  101. #define RTC_DEV_BUSY 0
  102. struct rtc_device {
  103. struct device dev;
  104. struct module *owner;
  105. int id;
  106. char name[RTC_DEVICE_NAME_SIZE];
  107. const struct rtc_class_ops *ops;
  108. struct mutex ops_lock;
  109. struct cdev char_dev;
  110. unsigned long flags;
  111. unsigned long irq_data;
  112. spinlock_t irq_lock;
  113. wait_queue_head_t irq_queue;
  114. struct fasync_struct *async_queue;
  115. struct rtc_task *irq_task;
  116. spinlock_t irq_task_lock;
  117. int irq_freq;
  118. int max_user_freq;
  119. struct timerqueue_head timerqueue;
  120. struct rtc_timer aie_timer;
  121. struct rtc_timer uie_rtctimer;
  122. struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
  123. int pie_enabled;
  124. struct work_struct irqwork;
  125. /* Some hardware can't support UIE mode */
  126. int uie_unsupported;
  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. extern struct rtc_device *rtc_device_register(const char *name,
  140. struct device *dev,
  141. const struct rtc_class_ops *ops,
  142. struct module *owner);
  143. extern struct rtc_device *devm_rtc_device_register(struct device *dev,
  144. const char *name,
  145. const struct rtc_class_ops *ops,
  146. struct module *owner);
  147. extern void rtc_device_unregister(struct rtc_device *rtc);
  148. extern void devm_rtc_device_unregister(struct device *dev,
  149. struct rtc_device *rtc);
  150. extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
  151. extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
  152. extern int rtc_set_ntp_time(struct timespec64 now);
  153. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
  154. extern int rtc_read_alarm(struct rtc_device *rtc,
  155. struct rtc_wkalrm *alrm);
  156. extern int rtc_set_alarm(struct rtc_device *rtc,
  157. struct rtc_wkalrm *alrm);
  158. extern int rtc_initialize_alarm(struct rtc_device *rtc,
  159. struct rtc_wkalrm *alrm);
  160. extern void rtc_update_irq(struct rtc_device *rtc,
  161. unsigned long num, unsigned long events);
  162. extern struct rtc_device *rtc_class_open(const char *name);
  163. extern void rtc_class_close(struct rtc_device *rtc);
  164. extern int rtc_irq_register(struct rtc_device *rtc,
  165. struct rtc_task *task);
  166. extern void rtc_irq_unregister(struct rtc_device *rtc,
  167. struct rtc_task *task);
  168. extern int rtc_irq_set_state(struct rtc_device *rtc,
  169. struct rtc_task *task, int enabled);
  170. extern int rtc_irq_set_freq(struct rtc_device *rtc,
  171. struct rtc_task *task, 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. int rtc_register(rtc_task_t *task);
  181. int rtc_unregister(rtc_task_t *task);
  182. int rtc_control(rtc_task_t *t, unsigned int cmd, unsigned long arg);
  183. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data);
  184. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  185. ktime_t expires, ktime_t period);
  186. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
  187. int rtc_read_offset(struct rtc_device *rtc, long *offset);
  188. int rtc_set_offset(struct rtc_device *rtc, long offset);
  189. void rtc_timer_do_work(struct work_struct *work);
  190. int rtc_read_scratch(struct rtc_device *rtc, unsigned int index, u32 *value);
  191. int rtc_write_scratch(struct rtc_device *rtc, unsigned int index, u32 value);
  192. void rtc_power_off_program(struct rtc_device *rtc);
  193. static inline bool is_leap_year(unsigned int year)
  194. {
  195. return (!(year % 4) && (year % 100)) || !(year % 400);
  196. }
  197. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  198. extern int rtc_hctosys_ret;
  199. #else
  200. #define rtc_hctosys_ret -ENODEV
  201. #endif
  202. #endif /* _LINUX_RTC_H_ */