blob: e8326f26fa2f5c5ca11d26901f5e70052a1ffea6 [file] [log] [blame]
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08001/*
2 * RTC subsystem, base class
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * class skeleton from drivers/hwmon/hwmon.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12*/
13
14#include <linux/module.h>
15#include <linux/rtc.h>
16#include <linux/kdev_t.h>
17#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
John Stultz6610e082010-09-23 15:07:34 -070019#include <linux/workqueue.h>
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080020
David Brownell5726fb22007-05-08 00:33:27 -070021#include "rtc-core.h"
22
23
Jonathan Cameron6d03d062011-11-02 13:37:49 -070024static DEFINE_IDA(rtc_ida);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080025struct class *rtc_class;
26
David Brownellcd966202007-05-08 00:33:40 -070027static void rtc_device_release(struct device *dev)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080028{
David Brownellcd966202007-05-08 00:33:40 -070029 struct rtc_device *rtc = to_rtc_device(dev);
Jonathan Cameron6d03d062011-11-02 13:37:49 -070030 ida_simple_remove(&rtc_ida, rtc->id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080031 kfree(rtc);
32}
33
David Brownell7ca1d482007-05-08 00:33:42 -070034#if defined(CONFIG_PM) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
35
36/*
37 * On suspend(), measure the delta between one RTC and the
38 * system's wall clock; restore it on resume().
39 */
40
John Stultz3dcad5f2011-05-27 11:33:18 -070041static struct timespec old_rtc, old_system, old_delta;
42
David Brownell7ca1d482007-05-08 00:33:42 -070043
44static int rtc_suspend(struct device *dev, pm_message_t mesg)
45{
46 struct rtc_device *rtc = to_rtc_device(dev);
47 struct rtc_time tm;
John Stultz3dcad5f2011-05-27 11:33:18 -070048 struct timespec delta, delta_delta;
Kay Sieversd4afc762009-01-06 14:42:11 -080049 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -070050 return 0;
51
John Stultz3dcad5f2011-05-27 11:33:18 -070052 /* snapshot the current RTC and system time at suspend*/
David Brownell7ca1d482007-05-08 00:33:42 -070053 rtc_read_time(rtc, &tm);
John Stultz3dcad5f2011-05-27 11:33:18 -070054 getnstimeofday(&old_system);
55 rtc_tm_to_time(&tm, &old_rtc.tv_sec);
56
57
58 /*
59 * To avoid drift caused by repeated suspend/resumes,
60 * which each can add ~1 second drift error,
61 * try to compensate so the difference in system time
62 * and rtc time stays close to constant.
63 */
64 delta = timespec_sub(old_system, old_rtc);
65 delta_delta = timespec_sub(delta, old_delta);
66 if (abs(delta_delta.tv_sec) >= 2) {
67 /*
68 * if delta_delta is too large, assume time correction
69 * has occured and set old_delta to the current delta.
70 */
71 old_delta = delta;
72 } else {
73 /* Otherwise try to adjust old_system to compensate */
74 old_system = timespec_sub(old_system, delta_delta);
75 }
David Brownell7ca1d482007-05-08 00:33:42 -070076
David Brownell7ca1d482007-05-08 00:33:42 -070077 return 0;
78}
79
80static int rtc_resume(struct device *dev)
81{
82 struct rtc_device *rtc = to_rtc_device(dev);
83 struct rtc_time tm;
John Stultz3dcad5f2011-05-27 11:33:18 -070084 struct timespec new_system, new_rtc;
85 struct timespec sleep_time;
David Brownell7ca1d482007-05-08 00:33:42 -070086
Kay Sieversd4afc762009-01-06 14:42:11 -080087 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -070088 return 0;
89
John Stultz3dcad5f2011-05-27 11:33:18 -070090 /* snapshot the current rtc and system time at resume */
91 getnstimeofday(&new_system);
David Brownell7ca1d482007-05-08 00:33:42 -070092 rtc_read_time(rtc, &tm);
93 if (rtc_valid_tm(&tm) != 0) {
Kay Sieversd4afc762009-01-06 14:42:11 -080094 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -070095 return 0;
96 }
John Stultz3dcad5f2011-05-27 11:33:18 -070097 rtc_tm_to_time(&tm, &new_rtc.tv_sec);
98 new_rtc.tv_nsec = 0;
99
100 if (new_rtc.tv_sec <= old_rtc.tv_sec) {
101 if (new_rtc.tv_sec < old_rtc.tv_sec)
Kay Sieversd4afc762009-01-06 14:42:11 -0800102 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700103 return 0;
104 }
105
John Stultz3dcad5f2011-05-27 11:33:18 -0700106 /* calculate the RTC time delta (sleep time)*/
107 sleep_time = timespec_sub(new_rtc, old_rtc);
David Brownell7ca1d482007-05-08 00:33:42 -0700108
John Stultz3dcad5f2011-05-27 11:33:18 -0700109 /*
110 * Since these RTC suspend/resume handlers are not called
111 * at the very end of suspend or the start of resume,
112 * some run-time may pass on either sides of the sleep time
113 * so subtract kernel run-time between rtc_suspend to rtc_resume
114 * to keep things accurate.
115 */
116 sleep_time = timespec_sub(sleep_time,
117 timespec_sub(new_system, old_system));
118
119 timekeeping_inject_sleeptime(&sleep_time);
David Brownell7ca1d482007-05-08 00:33:42 -0700120 return 0;
121}
122
123#else
124#define rtc_suspend NULL
125#define rtc_resume NULL
126#endif
127
128
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800129/**
130 * rtc_device_register - register w/ RTC class
131 * @dev: the device to register
132 *
133 * rtc_device_unregister() must be called when the class device is no
134 * longer needed.
135 *
136 * Returns the pointer to the new struct class device.
137 */
138struct rtc_device *rtc_device_register(const char *name, struct device *dev,
David Brownellff8371a2006-09-30 23:28:17 -0700139 const struct rtc_class_ops *ops,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800140 struct module *owner)
141{
142 struct rtc_device *rtc;
John Stultzf44f7f92011-02-21 22:58:51 -0800143 struct rtc_wkalrm alrm;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800144 int id, err;
145
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700146 id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
147 if (id < 0) {
148 err = id;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800149 goto exit;
150 }
151
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800152 rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
153 if (rtc == NULL) {
154 err = -ENOMEM;
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700155 goto exit_ida;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800156 }
157
158 rtc->id = id;
159 rtc->ops = ops;
160 rtc->owner = owner;
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200161 rtc->irq_freq = 1;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700162 rtc->max_user_freq = 64;
David Brownellcd966202007-05-08 00:33:40 -0700163 rtc->dev.parent = dev;
164 rtc->dev.class = rtc_class;
165 rtc->dev.release = rtc_device_release;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800166
167 mutex_init(&rtc->ops_lock);
168 spin_lock_init(&rtc->irq_lock);
169 spin_lock_init(&rtc->irq_task_lock);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700170 init_waitqueue_head(&rtc->irq_queue);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800171
John Stultz6610e082010-09-23 15:07:34 -0700172 /* Init timerqueue */
173 timerqueue_init_head(&rtc->timerqueue);
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100174 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
John Stultz6610e082010-09-23 15:07:34 -0700175 /* Init aie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100176 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700177 /* Init uie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100178 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700179 /* Init pie timer */
180 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
181 rtc->pie_timer.function = rtc_pie_update_irq;
182 rtc->pie_enabled = 0;
183
John Stultzf44f7f92011-02-21 22:58:51 -0800184 /* Check to see if there is an ALARM already set in hw */
185 err = __rtc_read_alarm(rtc, &alrm);
186
187 if (!err && !rtc_valid_tm(&alrm.time))
John Stultzf6d5b332011-03-29 18:00:27 -0700188 rtc_initialize_alarm(rtc, &alrm);
John Stultzf44f7f92011-02-21 22:58:51 -0800189
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800190 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
Kay Sieversd4afc762009-01-06 14:42:11 -0800191 dev_set_name(&rtc->dev, "rtc%d", id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800192
David Brownellcb3a58d2007-05-08 00:33:46 -0700193 rtc_dev_prepare(rtc);
194
David Brownellcd966202007-05-08 00:33:40 -0700195 err = device_register(&rtc->dev);
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700196 if (err) {
197 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800198 goto exit_kfree;
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700199 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800200
David Brownell5726fb22007-05-08 00:33:27 -0700201 rtc_dev_add_device(rtc);
David Brownell446ecbd2007-05-08 00:33:33 -0700202 rtc_sysfs_add_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700203 rtc_proc_add_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700204
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800205 dev_info(dev, "rtc core: registered %s as %s\n",
Kay Sieversd4afc762009-01-06 14:42:11 -0800206 rtc->name, dev_name(&rtc->dev));
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800207
208 return rtc;
209
210exit_kfree:
211 kfree(rtc);
212
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700213exit_ida:
214 ida_simple_remove(&rtc_ida, id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800215
216exit:
Alessandro Zummod1d65b72006-04-10 22:54:45 -0700217 dev_err(dev, "rtc core: unable to register %s, err = %d\n",
218 name, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800219 return ERR_PTR(err);
220}
221EXPORT_SYMBOL_GPL(rtc_device_register);
222
223
224/**
225 * rtc_device_unregister - removes the previously registered RTC class device
226 *
227 * @rtc: the RTC class device to destroy
228 */
229void rtc_device_unregister(struct rtc_device *rtc)
230{
David Brownellcd966202007-05-08 00:33:40 -0700231 if (get_device(&rtc->dev) != NULL) {
David Brownelle109ebd2007-02-28 20:12:40 -0800232 mutex_lock(&rtc->ops_lock);
233 /* remove innards of this RTC, then disable it, before
234 * letting any rtc_class_open() users access it again
235 */
David Brownell446ecbd2007-05-08 00:33:33 -0700236 rtc_sysfs_del_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700237 rtc_dev_del_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700238 rtc_proc_del_device(rtc);
David Brownellcd966202007-05-08 00:33:40 -0700239 device_unregister(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800240 rtc->ops = NULL;
241 mutex_unlock(&rtc->ops_lock);
David Brownellcd966202007-05-08 00:33:40 -0700242 put_device(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800243 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800244}
245EXPORT_SYMBOL_GPL(rtc_device_unregister);
246
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800247static int __init rtc_init(void)
248{
249 rtc_class = class_create(THIS_MODULE, "rtc");
250 if (IS_ERR(rtc_class)) {
251 printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
252 return PTR_ERR(rtc_class);
253 }
David Brownell7ca1d482007-05-08 00:33:42 -0700254 rtc_class->suspend = rtc_suspend;
255 rtc_class->resume = rtc_resume;
David Brownell5726fb22007-05-08 00:33:27 -0700256 rtc_dev_init();
David Brownell446ecbd2007-05-08 00:33:33 -0700257 rtc_sysfs_init(rtc_class);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800258 return 0;
259}
260
261static void __exit rtc_exit(void)
262{
David Brownell5726fb22007-05-08 00:33:27 -0700263 rtc_dev_exit();
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800264 class_destroy(rtc_class);
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700265 ida_destroy(&rtc_ida);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800266}
267
David Brownell818a8672006-09-30 23:28:15 -0700268subsys_initcall(rtc_init);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800269module_exit(rtc_exit);
270
David Brownell818a8672006-09-30 23:28:15 -0700271MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800272MODULE_DESCRIPTION("RTC class support");
273MODULE_LICENSE("GPL");