xref: /openbmc/linux/include/linux/rtc.h (revision 781589e4)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Generic RTC interface.
41da177e4SLinus Torvalds  * This version contains the part of the user interface to the Real Time Clock
51da177e4SLinus Torvalds  * service. It is used with both the legacy mc146818 and also  EFI
61da177e4SLinus Torvalds  * Struct rtc_time and first 12 ioctl by Paul Gortmaker, 1996 - separated out
71da177e4SLinus Torvalds  * from <linux/mc146818rtc.h> to this file for 2.4 kernels.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Copyright (C) 1999 Hewlett-Packard Co.
101da177e4SLinus Torvalds  * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds #ifndef _LINUX_RTC_H_
131da177e4SLinus Torvalds #define _LINUX_RTC_H_
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds 
1678d89ef4SAndrew Morton #include <linux/types.h>
174024ce5eSJoe Korty #include <linux/interrupt.h>
18697e5a47SAlexandre Belloni #include <linux/nvmem-provider.h>
19607ca46eSDavid Howells #include <uapi/linux/rtc.h>
204024ce5eSJoe Korty 
21c58411e9SAlessandro Zummo extern int rtc_month_days(unsigned int month, unsigned int year);
228232212eSAndrew Victor extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
23c58411e9SAlessandro Zummo extern int rtc_valid_tm(struct rtc_time *tm);
24c2c11ae4Spang.xunlei extern time64_t rtc_tm_to_time64(struct rtc_time *tm);
25c2c11ae4Spang.xunlei extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm);
266610e089SJohn Stultz ktime_t rtc_tm_to_ktime(struct rtc_time tm);
276610e089SJohn Stultz struct rtc_time rtc_ktime_to_tm(ktime_t kt);
286610e089SJohn Stultz 
2992000257SXunlei Pang /*
3092000257SXunlei Pang  * rtc_tm_sub - Return the difference in seconds.
3192000257SXunlei Pang  */
rtc_tm_sub(struct rtc_time * lhs,struct rtc_time * rhs)3292000257SXunlei Pang static inline time64_t rtc_tm_sub(struct rtc_time *lhs, struct rtc_time *rhs)
3392000257SXunlei Pang {
3492000257SXunlei Pang 	return rtc_tm_to_time64(lhs) - rtc_tm_to_time64(rhs);
3592000257SXunlei Pang }
3692000257SXunlei Pang 
370c86edc0SAlessandro Zummo #include <linux/device.h>
380c86edc0SAlessandro Zummo #include <linux/seq_file.h>
390c86edc0SAlessandro Zummo #include <linux/cdev.h>
400c86edc0SAlessandro Zummo #include <linux/poll.h>
410c86edc0SAlessandro Zummo #include <linux/mutex.h>
426610e089SJohn Stultz #include <linux/timerqueue.h>
436610e089SJohn Stultz #include <linux/workqueue.h>
440c86edc0SAlessandro Zummo 
450c86edc0SAlessandro Zummo extern struct class *rtc_class;
460c86edc0SAlessandro Zummo 
475ad31a57SDavid Brownell /*
485ad31a57SDavid Brownell  * For these RTC methods the device parameter is the physical device
495ad31a57SDavid Brownell  * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
505ad31a57SDavid Brownell  * was passed to rtc_device_register().  Its driver_data normally holds
515ad31a57SDavid Brownell  * device state, including the rtc_device pointer for the RTC.
525ad31a57SDavid Brownell  *
535ad31a57SDavid Brownell  * Most of these methods are called with rtc_device.ops_lock held,
545ad31a57SDavid Brownell  * through the rtc_*(struct rtc_device *, ...) calls.
555ad31a57SDavid Brownell  *
565ad31a57SDavid Brownell  * The (current) exceptions are mostly filesystem hooks:
575ad31a57SDavid Brownell  *   - the proc() hook for procfs
585ad31a57SDavid Brownell  */
590c86edc0SAlessandro Zummo struct rtc_class_ops {
600c86edc0SAlessandro Zummo 	int (*ioctl)(struct device *, unsigned int, unsigned long);
610c86edc0SAlessandro Zummo 	int (*read_time)(struct device *, struct rtc_time *);
620c86edc0SAlessandro Zummo 	int (*set_time)(struct device *, struct rtc_time *);
630c86edc0SAlessandro Zummo 	int (*read_alarm)(struct device *, struct rtc_wkalrm *);
640c86edc0SAlessandro Zummo 	int (*set_alarm)(struct device *, struct rtc_wkalrm *);
650c86edc0SAlessandro Zummo 	int (*proc)(struct device *, struct seq_file *);
66099e6576SAlessandro Zummo 	int (*alarm_irq_enable)(struct device *, unsigned int enabled);
67b3967067SJoshua Clayton 	int (*read_offset)(struct device *, long *offset);
68b3967067SJoshua Clayton 	int (*set_offset)(struct device *, long offset);
690d20e9fbSAlexandre Belloni 	int (*param_get)(struct device *, struct rtc_param *param);
700d20e9fbSAlexandre Belloni 	int (*param_set)(struct device *, struct rtc_param *param);
710c86edc0SAlessandro Zummo };
720c86edc0SAlessandro Zummo 
739a032011SAlexandre Belloni struct rtc_device;
749a032011SAlexandre Belloni 
756610e089SJohn Stultz struct rtc_timer {
766610e089SJohn Stultz 	struct timerqueue_node node;
776610e089SJohn Stultz 	ktime_t period;
789a032011SAlexandre Belloni 	void (*func)(struct rtc_device *rtc);
799a032011SAlexandre Belloni 	struct rtc_device *rtc;
806610e089SJohn Stultz 	int enabled;
816610e089SJohn Stultz };
826610e089SJohn Stultz 
838853c202SJiri Kosina /* flags */
848853c202SJiri Kosina #define RTC_DEV_BUSY 0
85789c1093SYang Yingliang #define RTC_NO_CDEV  1
868853c202SJiri Kosina 
871796dcceSKrzysztof Kozlowski struct rtc_device {
88cd966209SDavid Brownell 	struct device dev;
890c86edc0SAlessandro Zummo 	struct module *owner;
900c86edc0SAlessandro Zummo 
910c86edc0SAlessandro Zummo 	int id;
920c86edc0SAlessandro Zummo 
93ff8371acSDavid Brownell 	const struct rtc_class_ops *ops;
940c86edc0SAlessandro Zummo 	struct mutex ops_lock;
950c86edc0SAlessandro Zummo 
960c86edc0SAlessandro Zummo 	struct cdev char_dev;
978853c202SJiri Kosina 	unsigned long flags;
980c86edc0SAlessandro Zummo 
990c86edc0SAlessandro Zummo 	unsigned long irq_data;
1000c86edc0SAlessandro Zummo 	spinlock_t irq_lock;
1010c86edc0SAlessandro Zummo 	wait_queue_head_t irq_queue;
1020c86edc0SAlessandro Zummo 	struct fasync_struct *async_queue;
1030c86edc0SAlessandro Zummo 
1040c86edc0SAlessandro Zummo 	int irq_freq;
105110d693dSAlessandro Zummo 	int max_user_freq;
1066610e089SJohn Stultz 
1076610e089SJohn Stultz 	struct timerqueue_head timerqueue;
1086610e089SJohn Stultz 	struct rtc_timer aie_timer;
1096610e089SJohn Stultz 	struct rtc_timer uie_rtctimer;
1106610e089SJohn Stultz 	struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
1116610e089SJohn Stultz 	int pie_enabled;
1126610e089SJohn Stultz 	struct work_struct irqwork;
1136e57b1d6SJohn Stultz 
11469eca258SThomas Gleixner 	/*
11569eca258SThomas Gleixner 	 * This offset specifies the update timing of the RTC.
11669eca258SThomas Gleixner 	 *
11769eca258SThomas Gleixner 	 * tsched     t1 write(t2.tv_sec - 1sec))  t2 RTC increments seconds
11869eca258SThomas Gleixner 	 *
11969eca258SThomas Gleixner 	 * The offset defines how tsched is computed so that the write to
12069eca258SThomas Gleixner 	 * the RTC (t2.tv_sec - 1sec) is correct versus the time required
12169eca258SThomas Gleixner 	 * for the transport of the write and the time which the RTC needs
12269eca258SThomas Gleixner 	 * to increment seconds the first time after the write (t2).
12369eca258SThomas Gleixner 	 *
12469eca258SThomas Gleixner 	 * For direct accessible RTCs tsched ~= t1 because the write time
12569eca258SThomas Gleixner 	 * is negligible. For RTCs behind slow busses the transport time is
12669eca258SThomas Gleixner 	 * significant and has to be taken into account.
12769eca258SThomas Gleixner 	 *
12869eca258SThomas Gleixner 	 * The time between the write (t1) and the first increment after
12969eca258SThomas Gleixner 	 * the write (t2) is RTC specific. For a MC146818 RTC it's 500ms,
13069eca258SThomas Gleixner 	 * for many others it's exactly 1 second. Consult the datasheet.
13169eca258SThomas Gleixner 	 *
13269eca258SThomas Gleixner 	 * The value of this offset is also used to calculate the to be
13369eca258SThomas Gleixner 	 * written value (t2.tv_sec - 1sec) at tsched.
13469eca258SThomas Gleixner 	 *
13569eca258SThomas Gleixner 	 * The default value for this is NSEC_PER_SEC + 10 msec default
13669eca258SThomas Gleixner 	 * transport time. The offset can be adjusted by drivers so the
13769eca258SThomas Gleixner 	 * calculation for the to be written value at tsched becomes
13869eca258SThomas Gleixner 	 * correct:
13969eca258SThomas Gleixner 	 *
14069eca258SThomas Gleixner 	 *	newval = tsched + set_offset_nsec - NSEC_PER_SEC
14169eca258SThomas Gleixner 	 * and  (tsched + set_offset_nsec) % NSEC_PER_SEC == 0
1420f295b06SJason Gunthorpe 	 */
14369eca258SThomas Gleixner 	unsigned long set_offset_nsec;
1440f295b06SJason Gunthorpe 
1457ae41220SAlexandre Belloni 	unsigned long features[BITS_TO_LONGS(RTC_FEATURE_CNT)];
1467ae41220SAlexandre Belloni 
14771db049eSAlexandre Belloni 	time64_t range_min;
14871db049eSAlexandre Belloni 	timeu64_t range_max;
149*781589e4SGuenter Roeck 	timeu64_t alarm_offset_max;
15098951564SBaolin Wang 	time64_t start_secs;
15198951564SBaolin Wang 	time64_t offset_secs;
15298951564SBaolin Wang 	bool set_start_time;
15371db049eSAlexandre Belloni 
1546e57b1d6SJohn Stultz #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
1556e57b1d6SJohn Stultz 	struct work_struct uie_task;
1566e57b1d6SJohn Stultz 	struct timer_list uie_timer;
1576e57b1d6SJohn Stultz 	/* Those fields are protected by rtc->irq_lock */
1586e57b1d6SJohn Stultz 	unsigned int oldsecs;
1596e57b1d6SJohn Stultz 	unsigned int uie_irq_active:1;
1606e57b1d6SJohn Stultz 	unsigned int stop_uie_polling:1;
1616e57b1d6SJohn Stultz 	unsigned int uie_task_active:1;
1626e57b1d6SJohn Stultz 	unsigned int uie_timer_active:1;
1636e57b1d6SJohn Stultz #endif
1640c86edc0SAlessandro Zummo };
165cd966209SDavid Brownell #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
1660c86edc0SAlessandro Zummo 
167ae486688SAlexandre Belloni #define rtc_lock(d) mutex_lock(&d->ops_lock)
168ae486688SAlexandre Belloni #define rtc_unlock(d) mutex_unlock(&d->ops_lock)
169ae486688SAlexandre Belloni 
17083bbc5acSAlexandre Belloni /* useful timestamps */
171f00eaa38SAlexandre Belloni #define RTC_TIMESTAMP_BEGIN_0000	-62167219200ULL /* 0000-01-01 00:00:00 */
172d3062d1dSGeert Uytterhoeven #define RTC_TIMESTAMP_BEGIN_1900	-2208988800LL /* 1900-01-01 00:00:00 */
17383bbc5acSAlexandre Belloni #define RTC_TIMESTAMP_BEGIN_2000	946684800LL /* 2000-01-01 00:00:00 */
1745ff404d1SAlexandre Belloni #define RTC_TIMESTAMP_END_2063		2966371199LL /* 2063-12-31 23:59:59 */
175cbc943ebSAlexandre Belloni #define RTC_TIMESTAMP_END_2079		3471292799LL /* 2079-12-31 23:59:59 */
17683bbc5acSAlexandre Belloni #define RTC_TIMESTAMP_END_2099		4102444799LL /* 2099-12-31 23:59:59 */
1778d6ac1ceSAlexandre Belloni #define RTC_TIMESTAMP_END_2199		7258118399LL /* 2199-12-31 23:59:59 */
178beee05dfSAlexandre Belloni #define RTC_TIMESTAMP_END_9999		253402300799LL /* 9999-12-31 23:59:59 */
17983bbc5acSAlexandre Belloni 
1806636a994SJingoo Han extern struct rtc_device *devm_rtc_device_register(struct device *dev,
1816636a994SJingoo Han 					const char *name,
1823e217b66SJingoo Han 					const struct rtc_class_ops *ops,
1833e217b66SJingoo Han 					struct module *owner);
1843068a254SAlexandre Belloni struct rtc_device *devm_rtc_allocate_device(struct device *dev);
185fdcfd854SBartosz Golaszewski int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc);
1860c86edc0SAlessandro Zummo 
187ab6a2d70SDavid Brownell extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
188ab6a2d70SDavid Brownell extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
189f44f7f96SJohn Stultz int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
190ab6a2d70SDavid Brownell extern int rtc_read_alarm(struct rtc_device *rtc,
1910c86edc0SAlessandro Zummo 			struct rtc_wkalrm *alrm);
192ab6a2d70SDavid Brownell extern int rtc_set_alarm(struct rtc_device *rtc,
1930c86edc0SAlessandro Zummo 				struct rtc_wkalrm *alrm);
194f6d5b331SJohn Stultz extern int rtc_initialize_alarm(struct rtc_device *rtc,
195f6d5b331SJohn Stultz 				struct rtc_wkalrm *alrm);
196ab6a2d70SDavid Brownell extern void rtc_update_irq(struct rtc_device *rtc,
1970c86edc0SAlessandro Zummo 			unsigned long num, unsigned long events);
1980c86edc0SAlessandro Zummo 
1999f3b795aSMichał Mirosław extern struct rtc_device *rtc_class_open(const char *name);
200ab6a2d70SDavid Brownell extern void rtc_class_close(struct rtc_device *rtc);
2010c86edc0SAlessandro Zummo 
2028719d3c9SAlexandre Belloni extern int rtc_irq_set_state(struct rtc_device *rtc, int enabled);
2038719d3c9SAlexandre Belloni extern int rtc_irq_set_freq(struct rtc_device *rtc, int freq);
204099e6576SAlessandro Zummo extern int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
205099e6576SAlessandro Zummo extern int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled);
2066e57b1d6SJohn Stultz extern int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc,
2076e57b1d6SJohn Stultz 						unsigned int enabled);
2080c86edc0SAlessandro Zummo 
209456d66ecSJohn Stultz void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode);
2109a032011SAlexandre Belloni void rtc_aie_update_irq(struct rtc_device *rtc);
2119a032011SAlexandre Belloni void rtc_uie_update_irq(struct rtc_device *rtc);
2126610e089SJohn Stultz enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer);
2131da177e4SLinus Torvalds 
2149a032011SAlexandre Belloni void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
2159a032011SAlexandre Belloni 		    struct rtc_device *rtc);
21696c8f06aSThomas Gleixner int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
2176610e089SJohn Stultz 		    ktime_t expires, ktime_t period);
21873744a64SKrzysztof Kozlowski void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer);
219b3967067SJoshua Clayton int rtc_read_offset(struct rtc_device *rtc, long *offset);
220b3967067SJoshua Clayton int rtc_set_offset(struct rtc_device *rtc, long offset);
22196c8f06aSThomas Gleixner void rtc_timer_do_work(struct work_struct *work);
2226610e089SJohn Stultz 
is_leap_year(unsigned int year)22378d89ef4SAndrew Morton static inline bool is_leap_year(unsigned int year)
22478d89ef4SAndrew Morton {
22578d89ef4SAndrew Morton 	return (!(year % 4) && (year % 100)) || !(year % 400);
22678d89ef4SAndrew Morton }
22778d89ef4SAndrew Morton 
228fdcfd854SBartosz Golaszewski #define devm_rtc_register_device(device) \
229fdcfd854SBartosz Golaszewski 	__devm_rtc_register_device(THIS_MODULE, device)
2303068a254SAlexandre Belloni 
2314c24e29eSDavid Fries #ifdef CONFIG_RTC_HCTOSYS_DEVICE
232d0ab4a4dSUwe Kleine-König extern int rtc_hctosys_ret;
233d0ab4a4dSUwe Kleine-König #else
234d0ab4a4dSUwe Kleine-König #define rtc_hctosys_ret -ENODEV
235d0ab4a4dSUwe Kleine-König #endif
236d0ab4a4dSUwe Kleine-König 
237fd5cd21dSAlexandre Belloni #ifdef CONFIG_RTC_NVMEM
2383a905c2dSBartosz Golaszewski int devm_rtc_nvmem_register(struct rtc_device *rtc,
239fd5cd21dSAlexandre Belloni 			    struct nvmem_config *nvmem_config);
240fd5cd21dSAlexandre Belloni #else
devm_rtc_nvmem_register(struct rtc_device * rtc,struct nvmem_config * nvmem_config)2413a905c2dSBartosz Golaszewski static inline int devm_rtc_nvmem_register(struct rtc_device *rtc,
242fd5cd21dSAlexandre Belloni 					  struct nvmem_config *nvmem_config)
243fd5cd21dSAlexandre Belloni {
244c59b3715SAlexandre Belloni 	return 0;
245fd5cd21dSAlexandre Belloni }
246fd5cd21dSAlexandre Belloni #endif
247fd5cd21dSAlexandre Belloni 
248eb2bccb7SAlexandre Belloni #ifdef CONFIG_RTC_INTF_SYSFS
249eb2bccb7SAlexandre Belloni int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp);
250eb2bccb7SAlexandre Belloni int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps);
251eb2bccb7SAlexandre Belloni #else
252eb2bccb7SAlexandre Belloni static inline
rtc_add_group(struct rtc_device * rtc,const struct attribute_group * grp)253eb2bccb7SAlexandre Belloni int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
254eb2bccb7SAlexandre Belloni {
255eb2bccb7SAlexandre Belloni 	return 0;
256eb2bccb7SAlexandre Belloni }
257eb2bccb7SAlexandre Belloni 
258eb2bccb7SAlexandre Belloni static inline
rtc_add_groups(struct rtc_device * rtc,const struct attribute_group ** grps)259eb2bccb7SAlexandre Belloni int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
260eb2bccb7SAlexandre Belloni {
261eb2bccb7SAlexandre Belloni 	return 0;
262eb2bccb7SAlexandre Belloni }
263eb2bccb7SAlexandre Belloni #endif
2641da177e4SLinus Torvalds #endif /* _LINUX_RTC_H_ */
265