rtc.h 6.4 KB

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