blob: 5143629dedbdcf9262240e98f6a9637d249c5679 [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 Fries4c24e292012-10-04 17:14:12 -070034#ifdef CONFIG_RTC_HCTOSYS_DEVICE
35/* Result of the last RTC to system clock attempt. */
36int rtc_hctosys_ret = -ENODEV;
37#endif
David Brownell7ca1d482007-05-08 00:33:42 -070038
David Fries4c24e292012-10-04 17:14:12 -070039#if defined(CONFIG_PM) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
David Brownell7ca1d482007-05-08 00:33:42 -070040/*
41 * On suspend(), measure the delta between one RTC and the
42 * system's wall clock; restore it on resume().
43 */
44
John Stultz3dcad5f2011-05-27 11:33:18 -070045static struct timespec old_rtc, old_system, old_delta;
46
David Brownell7ca1d482007-05-08 00:33:42 -070047
48static int rtc_suspend(struct device *dev, pm_message_t mesg)
49{
50 struct rtc_device *rtc = to_rtc_device(dev);
51 struct rtc_time tm;
John Stultz3dcad5f2011-05-27 11:33:18 -070052 struct timespec delta, delta_delta;
Kay Sieversd4afc762009-01-06 14:42:11 -080053 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -070054 return 0;
55
John Stultz3dcad5f2011-05-27 11:33:18 -070056 /* snapshot the current RTC and system time at suspend*/
David Brownell7ca1d482007-05-08 00:33:42 -070057 rtc_read_time(rtc, &tm);
John Stultz3dcad5f2011-05-27 11:33:18 -070058 getnstimeofday(&old_system);
59 rtc_tm_to_time(&tm, &old_rtc.tv_sec);
60
61
62 /*
63 * To avoid drift caused by repeated suspend/resumes,
64 * which each can add ~1 second drift error,
65 * try to compensate so the difference in system time
66 * and rtc time stays close to constant.
67 */
68 delta = timespec_sub(old_system, old_rtc);
69 delta_delta = timespec_sub(delta, old_delta);
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -080070 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
John Stultz3dcad5f2011-05-27 11:33:18 -070071 /*
72 * if delta_delta is too large, assume time correction
73 * has occured and set old_delta to the current delta.
74 */
75 old_delta = delta;
76 } else {
77 /* Otherwise try to adjust old_system to compensate */
78 old_system = timespec_sub(old_system, delta_delta);
79 }
David Brownell7ca1d482007-05-08 00:33:42 -070080
David Brownell7ca1d482007-05-08 00:33:42 -070081 return 0;
82}
83
84static int rtc_resume(struct device *dev)
85{
86 struct rtc_device *rtc = to_rtc_device(dev);
87 struct rtc_time tm;
John Stultz3dcad5f2011-05-27 11:33:18 -070088 struct timespec new_system, new_rtc;
89 struct timespec sleep_time;
David Brownell7ca1d482007-05-08 00:33:42 -070090
David Fries4c24e292012-10-04 17:14:12 -070091 rtc_hctosys_ret = -ENODEV;
Kay Sieversd4afc762009-01-06 14:42:11 -080092 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -070093 return 0;
94
John Stultz3dcad5f2011-05-27 11:33:18 -070095 /* snapshot the current rtc and system time at resume */
96 getnstimeofday(&new_system);
David Brownell7ca1d482007-05-08 00:33:42 -070097 rtc_read_time(rtc, &tm);
98 if (rtc_valid_tm(&tm) != 0) {
Kay Sieversd4afc762009-01-06 14:42:11 -080099 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700100 return 0;
101 }
John Stultz3dcad5f2011-05-27 11:33:18 -0700102 rtc_tm_to_time(&tm, &new_rtc.tv_sec);
103 new_rtc.tv_nsec = 0;
104
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800105 if (new_rtc.tv_sec < old_rtc.tv_sec) {
106 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700107 return 0;
108 }
109
John Stultz3dcad5f2011-05-27 11:33:18 -0700110 /* calculate the RTC time delta (sleep time)*/
111 sleep_time = timespec_sub(new_rtc, old_rtc);
David Brownell7ca1d482007-05-08 00:33:42 -0700112
John Stultz3dcad5f2011-05-27 11:33:18 -0700113 /*
114 * Since these RTC suspend/resume handlers are not called
115 * at the very end of suspend or the start of resume,
116 * some run-time may pass on either sides of the sleep time
117 * so subtract kernel run-time between rtc_suspend to rtc_resume
118 * to keep things accurate.
119 */
120 sleep_time = timespec_sub(sleep_time,
121 timespec_sub(new_system, old_system));
122
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800123 if (sleep_time.tv_sec >= 0)
124 timekeeping_inject_sleeptime(&sleep_time);
David Fries4c24e292012-10-04 17:14:12 -0700125 rtc_hctosys_ret = 0;
David Brownell7ca1d482007-05-08 00:33:42 -0700126 return 0;
127}
128
129#else
130#define rtc_suspend NULL
131#define rtc_resume NULL
132#endif
133
134
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800135/**
136 * rtc_device_register - register w/ RTC class
137 * @dev: the device to register
138 *
139 * rtc_device_unregister() must be called when the class device is no
140 * longer needed.
141 *
142 * Returns the pointer to the new struct class device.
143 */
144struct rtc_device *rtc_device_register(const char *name, struct device *dev,
David Brownellff8371a2006-09-30 23:28:17 -0700145 const struct rtc_class_ops *ops,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800146 struct module *owner)
147{
148 struct rtc_device *rtc;
John Stultzf44f7f92011-02-21 22:58:51 -0800149 struct rtc_wkalrm alrm;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800150 int id, err;
151
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700152 id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
153 if (id < 0) {
154 err = id;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800155 goto exit;
156 }
157
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800158 rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
159 if (rtc == NULL) {
160 err = -ENOMEM;
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700161 goto exit_ida;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800162 }
163
164 rtc->id = id;
165 rtc->ops = ops;
166 rtc->owner = owner;
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200167 rtc->irq_freq = 1;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700168 rtc->max_user_freq = 64;
David Brownellcd966202007-05-08 00:33:40 -0700169 rtc->dev.parent = dev;
170 rtc->dev.class = rtc_class;
171 rtc->dev.release = rtc_device_release;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800172
173 mutex_init(&rtc->ops_lock);
174 spin_lock_init(&rtc->irq_lock);
175 spin_lock_init(&rtc->irq_task_lock);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700176 init_waitqueue_head(&rtc->irq_queue);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800177
John Stultz6610e082010-09-23 15:07:34 -0700178 /* Init timerqueue */
179 timerqueue_init_head(&rtc->timerqueue);
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100180 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
John Stultz6610e082010-09-23 15:07:34 -0700181 /* Init aie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100182 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700183 /* Init uie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100184 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700185 /* Init pie timer */
186 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
187 rtc->pie_timer.function = rtc_pie_update_irq;
188 rtc->pie_enabled = 0;
189
John Stultzf44f7f92011-02-21 22:58:51 -0800190 /* Check to see if there is an ALARM already set in hw */
191 err = __rtc_read_alarm(rtc, &alrm);
192
193 if (!err && !rtc_valid_tm(&alrm.time))
John Stultzf6d5b332011-03-29 18:00:27 -0700194 rtc_initialize_alarm(rtc, &alrm);
John Stultzf44f7f92011-02-21 22:58:51 -0800195
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800196 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
Kay Sieversd4afc762009-01-06 14:42:11 -0800197 dev_set_name(&rtc->dev, "rtc%d", id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800198
David Brownellcb3a58d2007-05-08 00:33:46 -0700199 rtc_dev_prepare(rtc);
200
David Brownellcd966202007-05-08 00:33:40 -0700201 err = device_register(&rtc->dev);
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700202 if (err) {
203 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800204 goto exit_kfree;
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700205 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800206
David Brownell5726fb22007-05-08 00:33:27 -0700207 rtc_dev_add_device(rtc);
David Brownell446ecbd2007-05-08 00:33:33 -0700208 rtc_sysfs_add_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700209 rtc_proc_add_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700210
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800211 dev_info(dev, "rtc core: registered %s as %s\n",
Kay Sieversd4afc762009-01-06 14:42:11 -0800212 rtc->name, dev_name(&rtc->dev));
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800213
214 return rtc;
215
216exit_kfree:
217 kfree(rtc);
218
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700219exit_ida:
220 ida_simple_remove(&rtc_ida, id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800221
222exit:
Alessandro Zummod1d65b72006-04-10 22:54:45 -0700223 dev_err(dev, "rtc core: unable to register %s, err = %d\n",
224 name, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800225 return ERR_PTR(err);
226}
227EXPORT_SYMBOL_GPL(rtc_device_register);
228
229
230/**
231 * rtc_device_unregister - removes the previously registered RTC class device
232 *
233 * @rtc: the RTC class device to destroy
234 */
235void rtc_device_unregister(struct rtc_device *rtc)
236{
David Brownellcd966202007-05-08 00:33:40 -0700237 if (get_device(&rtc->dev) != NULL) {
David Brownelle109ebd2007-02-28 20:12:40 -0800238 mutex_lock(&rtc->ops_lock);
239 /* remove innards of this RTC, then disable it, before
240 * letting any rtc_class_open() users access it again
241 */
David Brownell446ecbd2007-05-08 00:33:33 -0700242 rtc_sysfs_del_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700243 rtc_dev_del_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700244 rtc_proc_del_device(rtc);
David Brownellcd966202007-05-08 00:33:40 -0700245 device_unregister(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800246 rtc->ops = NULL;
247 mutex_unlock(&rtc->ops_lock);
David Brownellcd966202007-05-08 00:33:40 -0700248 put_device(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800249 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800250}
251EXPORT_SYMBOL_GPL(rtc_device_unregister);
252
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800253static int __init rtc_init(void)
254{
255 rtc_class = class_create(THIS_MODULE, "rtc");
256 if (IS_ERR(rtc_class)) {
257 printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
258 return PTR_ERR(rtc_class);
259 }
David Brownell7ca1d482007-05-08 00:33:42 -0700260 rtc_class->suspend = rtc_suspend;
261 rtc_class->resume = rtc_resume;
David Brownell5726fb22007-05-08 00:33:27 -0700262 rtc_dev_init();
David Brownell446ecbd2007-05-08 00:33:33 -0700263 rtc_sysfs_init(rtc_class);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800264 return 0;
265}
266
267static void __exit rtc_exit(void)
268{
David Brownell5726fb22007-05-08 00:33:27 -0700269 rtc_dev_exit();
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800270 class_destroy(rtc_class);
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700271 ida_destroy(&rtc_ida);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800272}
273
David Brownell818a8672006-09-30 23:28:15 -0700274subsys_initcall(rtc_init);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800275module_exit(rtc_exit);
276
David Brownell818a8672006-09-30 23:28:15 -0700277MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800278MODULE_DESCRIPTION("RTC class support");
279MODULE_LICENSE("GPL");