rtc.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <linux/nvmem-provider.h>
  16. #include <uapi/linux/rtc.h>
  17. extern int rtc_month_days(unsigned int month, unsigned int year);
  18. extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
  19. extern int rtc_valid_tm(struct rtc_time *tm);
  20. extern time64_t rtc_tm_to_time64(struct rtc_time *tm);
  21. extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm);
  22. ktime_t rtc_tm_to_ktime(struct rtc_time tm);
  23. struct rtc_time rtc_ktime_to_tm(ktime_t kt);
  24. /*
  25. * rtc_tm_sub - Return the difference in seconds.
  26. */
  27. static inline time64_t rtc_tm_sub(struct rtc_time *lhs, struct rtc_time *rhs)
  28. {
  29. return rtc_tm_to_time64(lhs) - rtc_tm_to_time64(rhs);
  30. }
  31. static inline void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
  32. {
  33. rtc_time64_to_tm(time, tm);
  34. }
  35. static inline int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
  36. {
  37. *time = rtc_tm_to_time64(tm);
  38. return 0;
  39. }
  40. #include <linux/device.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/cdev.h>
  43. #include <linux/poll.h>
  44. #include <linux/mutex.h>
  45. #include <linux/timerqueue.h>
  46. #include <linux/workqueue.h>
  47. extern struct class *rtc_class;
  48. /*
  49. * For these RTC methods the device parameter is the physical device
  50. * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
  51. * was passed to rtc_device_register(). Its driver_data normally holds
  52. * device state, including the rtc_device pointer for the RTC.
  53. *
  54. * Most of these methods are called with rtc_device.ops_lock held,
  55. * through the rtc_*(struct rtc_device *, ...) calls.
  56. *
  57. * The (current) exceptions are mostly filesystem hooks:
  58. * - the proc() hook for procfs
  59. * - non-ioctl() chardev hooks: open(), release(), read_callback()
  60. *
  61. * REVISIT those periodic irq calls *do* have ops_lock when they're
  62. * issued through ioctl() ...
  63. */
  64. struct rtc_class_ops {
  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_mmss64)(struct device *, time64_t secs);
  72. int (*set_mmss)(struct device *, unsigned long secs);
  73. int (*read_callback)(struct device *, int data);
  74. int (*alarm_irq_enable)(struct device *, unsigned int enabled);
  75. int (*read_offset)(struct device *, long *offset);
  76. int (*set_offset)(struct device *, long offset);
  77. };
  78. #define RTC_DEVICE_NAME_SIZE 20
  79. typedef struct rtc_task {
  80. void (*func)(void *private_data);
  81. void *private_data;
  82. } rtc_task_t;
  83. struct rtc_timer {
  84. struct rtc_task task;
  85. struct timerqueue_node node;
  86. ktime_t period;
  87. int enabled;
  88. };
  89. /* flags */
  90. #define RTC_DEV_BUSY 0
  91. struct rtc_device {
  92. struct device dev;
  93. struct module *owner;
  94. int id;
  95. const struct rtc_class_ops *ops;
  96. struct mutex ops_lock;
  97. struct cdev char_dev;
  98. unsigned long flags;
  99. unsigned long irq_data;
  100. spinlock_t irq_lock;
  101. wait_queue_head_t irq_queue;
  102. struct fasync_struct *async_queue;
  103. struct rtc_task *irq_task;
  104. spinlock_t irq_task_lock;
  105. int irq_freq;
  106. int max_user_freq;
  107. struct timerqueue_head timerqueue;
  108. struct rtc_timer aie_timer;
  109. struct rtc_timer uie_rtctimer;
  110. struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
  111. int pie_enabled;
  112. struct work_struct irqwork;
  113. /* Some hardware can't support UIE mode */
  114. int uie_unsupported;
  115. bool registered;
  116. struct nvmem_config *nvmem_config;
  117. struct nvmem_device *nvmem;
  118. /* Old ABI support */
  119. bool nvram_old_abi;
  120. struct bin_attribute *nvram;
  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. struct rtc_device *devm_rtc_allocate_device(struct device *dev);
  142. int __rtc_register_device(struct module *owner, struct rtc_device *rtc);
  143. extern void rtc_device_unregister(struct rtc_device *rtc);
  144. extern void devm_rtc_device_unregister(struct device *dev,
  145. struct rtc_device *rtc);
  146. extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
  147. extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
  148. extern int rtc_set_ntp_time(struct timespec64 now);
  149. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
  150. extern int rtc_read_alarm(struct rtc_device *rtc,
  151. struct rtc_wkalrm *alrm);
  152. extern int rtc_set_alarm(struct rtc_device *rtc,
  153. struct rtc_wkalrm *alrm);
  154. extern int rtc_initialize_alarm(struct rtc_device *rtc,
  155. struct rtc_wkalrm *alrm);
  156. extern void rtc_update_irq(struct rtc_device *rtc,
  157. unsigned long num, unsigned long events);
  158. extern struct rtc_device *rtc_class_open(const char *name);
  159. extern void rtc_class_close(struct rtc_device *rtc);
  160. extern int rtc_irq_register(struct rtc_device *rtc,
  161. struct rtc_task *task);
  162. extern void rtc_irq_unregister(struct rtc_device *rtc,
  163. struct rtc_task *task);
  164. extern int rtc_irq_set_state(struct rtc_device *rtc,
  165. struct rtc_task *task, int enabled);
  166. extern int rtc_irq_set_freq(struct rtc_device *rtc,
  167. struct rtc_task *task, int freq);
  168. extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  169. extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
  170. extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
  171. unsigned int enabled);
  172. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode);
  173. void rtc_aie_update_irq(void *private);
  174. void rtc_uie_update_irq(void *private);
  175. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer);
  176. int rtc_register(rtc_task_t *task);
  177. int rtc_unregister(rtc_task_t *task);
  178. int rtc_control(rtc_task_t *t, unsigned int cmd, unsigned long arg);
  179. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data);
  180. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  181. ktime_t expires, ktime_t period);
  182. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
  183. int rtc_read_offset(struct rtc_device *rtc, long *offset);
  184. int rtc_set_offset(struct rtc_device *rtc, long offset);
  185. void rtc_timer_do_work(struct work_struct *work);
  186. static inline bool is_leap_year(unsigned int year)
  187. {
  188. return (!(year % 4) && (year % 100)) || !(year % 400);
  189. }
  190. #define rtc_register_device(device) \
  191. __rtc_register_device(THIS_MODULE, device)
  192. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  193. extern int rtc_hctosys_ret;
  194. #else
  195. #define rtc_hctosys_ret -ENODEV
  196. #endif
  197. #endif /* _LINUX_RTC_H_ */