0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef _LINUX_RTC_H_
0013 #define _LINUX_RTC_H_
0014
0015
0016 #include <linux/types.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/nvmem-provider.h>
0019 #include <uapi/linux/rtc.h>
0020
0021 extern int rtc_month_days(unsigned int month, unsigned int year);
0022 extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
0023 extern int rtc_valid_tm(struct rtc_time *tm);
0024 extern time64_t rtc_tm_to_time64(struct rtc_time *tm);
0025 extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm);
0026 ktime_t rtc_tm_to_ktime(struct rtc_time tm);
0027 struct rtc_time rtc_ktime_to_tm(ktime_t kt);
0028
0029
0030
0031
0032 static inline time64_t rtc_tm_sub(struct rtc_time *lhs, struct rtc_time *rhs)
0033 {
0034 return rtc_tm_to_time64(lhs) - rtc_tm_to_time64(rhs);
0035 }
0036
0037 #include <linux/device.h>
0038 #include <linux/seq_file.h>
0039 #include <linux/cdev.h>
0040 #include <linux/poll.h>
0041 #include <linux/mutex.h>
0042 #include <linux/timerqueue.h>
0043 #include <linux/workqueue.h>
0044
0045 extern struct class *rtc_class;
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 struct rtc_class_ops {
0060 int (*ioctl)(struct device *, unsigned int, unsigned long);
0061 int (*read_time)(struct device *, struct rtc_time *);
0062 int (*set_time)(struct device *, struct rtc_time *);
0063 int (*read_alarm)(struct device *, struct rtc_wkalrm *);
0064 int (*set_alarm)(struct device *, struct rtc_wkalrm *);
0065 int (*proc)(struct device *, struct seq_file *);
0066 int (*alarm_irq_enable)(struct device *, unsigned int enabled);
0067 int (*read_offset)(struct device *, long *offset);
0068 int (*set_offset)(struct device *, long offset);
0069 int (*param_get)(struct device *, struct rtc_param *param);
0070 int (*param_set)(struct device *, struct rtc_param *param);
0071 };
0072
0073 struct rtc_device;
0074
0075 struct rtc_timer {
0076 struct timerqueue_node node;
0077 ktime_t period;
0078 void (*func)(struct rtc_device *rtc);
0079 struct rtc_device *rtc;
0080 int enabled;
0081 };
0082
0083
0084 #define RTC_DEV_BUSY 0
0085 #define RTC_NO_CDEV 1
0086
0087 struct rtc_device {
0088 struct device dev;
0089 struct module *owner;
0090
0091 int id;
0092
0093 const struct rtc_class_ops *ops;
0094 struct mutex ops_lock;
0095
0096 struct cdev char_dev;
0097 unsigned long flags;
0098
0099 unsigned long irq_data;
0100 spinlock_t irq_lock;
0101 wait_queue_head_t irq_queue;
0102 struct fasync_struct *async_queue;
0103
0104 int irq_freq;
0105 int max_user_freq;
0106
0107 struct timerqueue_head timerqueue;
0108 struct rtc_timer aie_timer;
0109 struct rtc_timer uie_rtctimer;
0110 struct hrtimer pie_timer;
0111 int pie_enabled;
0112 struct work_struct irqwork;
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 unsigned long set_offset_nsec;
0144
0145 unsigned long features[BITS_TO_LONGS(RTC_FEATURE_CNT)];
0146
0147 time64_t range_min;
0148 timeu64_t range_max;
0149 time64_t start_secs;
0150 time64_t offset_secs;
0151 bool set_start_time;
0152
0153 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
0154 struct work_struct uie_task;
0155 struct timer_list uie_timer;
0156
0157 unsigned int oldsecs;
0158 unsigned int uie_irq_active:1;
0159 unsigned int stop_uie_polling:1;
0160 unsigned int uie_task_active:1;
0161 unsigned int uie_timer_active:1;
0162 #endif
0163 };
0164 #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
0165
0166 #define rtc_lock(d) mutex_lock(&d->ops_lock)
0167 #define rtc_unlock(d) mutex_unlock(&d->ops_lock)
0168
0169
0170 #define RTC_TIMESTAMP_BEGIN_0000 -62167219200ULL
0171 #define RTC_TIMESTAMP_BEGIN_1900 -2208988800LL
0172 #define RTC_TIMESTAMP_BEGIN_2000 946684800LL
0173 #define RTC_TIMESTAMP_END_2063 2966371199LL
0174 #define RTC_TIMESTAMP_END_2079 3471292799LL
0175 #define RTC_TIMESTAMP_END_2099 4102444799LL
0176 #define RTC_TIMESTAMP_END_2199 7258118399LL
0177 #define RTC_TIMESTAMP_END_9999 253402300799LL
0178
0179 extern struct rtc_device *devm_rtc_device_register(struct device *dev,
0180 const char *name,
0181 const struct rtc_class_ops *ops,
0182 struct module *owner);
0183 struct rtc_device *devm_rtc_allocate_device(struct device *dev);
0184 int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc);
0185
0186 extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
0187 extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
0188 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
0189 extern int rtc_read_alarm(struct rtc_device *rtc,
0190 struct rtc_wkalrm *alrm);
0191 extern int rtc_set_alarm(struct rtc_device *rtc,
0192 struct rtc_wkalrm *alrm);
0193 extern int rtc_initialize_alarm(struct rtc_device *rtc,
0194 struct rtc_wkalrm *alrm);
0195 extern void rtc_update_irq(struct rtc_device *rtc,
0196 unsigned long num, unsigned long events);
0197
0198 extern struct rtc_device *rtc_class_open(const char *name);
0199 extern void rtc_class_close(struct rtc_device *rtc);
0200
0201 extern int rtc_irq_set_state(struct rtc_device *rtc, int enabled);
0202 extern int rtc_irq_set_freq(struct rtc_device *rtc, int freq);
0203 extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
0204 extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
0205 extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
0206 unsigned int enabled);
0207
0208 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode);
0209 void rtc_aie_update_irq(struct rtc_device *rtc);
0210 void rtc_uie_update_irq(struct rtc_device *rtc);
0211 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer);
0212
0213 void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
0214 struct rtc_device *rtc);
0215 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
0216 ktime_t expires, ktime_t period);
0217 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
0218 int rtc_read_offset(struct rtc_device *rtc, long *offset);
0219 int rtc_set_offset(struct rtc_device *rtc, long offset);
0220 void rtc_timer_do_work(struct work_struct *work);
0221
0222 static inline bool is_leap_year(unsigned int year)
0223 {
0224 return (!(year % 4) && (year % 100)) || !(year % 400);
0225 }
0226
0227 #define devm_rtc_register_device(device) \
0228 __devm_rtc_register_device(THIS_MODULE, device)
0229
0230 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
0231 extern int rtc_hctosys_ret;
0232 #else
0233 #define rtc_hctosys_ret -ENODEV
0234 #endif
0235
0236 #ifdef CONFIG_RTC_NVMEM
0237 int devm_rtc_nvmem_register(struct rtc_device *rtc,
0238 struct nvmem_config *nvmem_config);
0239 #else
0240 static inline int devm_rtc_nvmem_register(struct rtc_device *rtc,
0241 struct nvmem_config *nvmem_config)
0242 {
0243 return 0;
0244 }
0245 #endif
0246
0247 #ifdef CONFIG_RTC_INTF_SYSFS
0248 int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp);
0249 int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps);
0250 #else
0251 static inline
0252 int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
0253 {
0254 return 0;
0255 }
0256
0257 static inline
0258 int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
0259 {
0260 return 0;
0261 }
0262 #endif
0263 #endif