blob: c01773f564357ee28395ace9d6b89cf45970e4ac [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;
Feng Tang9ecf37e2013-01-16 00:09:48 +080053
54 if (has_persistent_clock())
55 return 0;
56
Kay Sieversd4afc762009-01-06 14:42:11 -080057 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -070058 return 0;
59
John Stultz3dcad5f2011-05-27 11:33:18 -070060 /* snapshot the current RTC and system time at suspend*/
David Brownell7ca1d482007-05-08 00:33:42 -070061 rtc_read_time(rtc, &tm);
John Stultz3dcad5f2011-05-27 11:33:18 -070062 getnstimeofday(&old_system);
63 rtc_tm_to_time(&tm, &old_rtc.tv_sec);
64
65
66 /*
67 * To avoid drift caused by repeated suspend/resumes,
68 * which each can add ~1 second drift error,
69 * try to compensate so the difference in system time
70 * and rtc time stays close to constant.
71 */
72 delta = timespec_sub(old_system, old_rtc);
73 delta_delta = timespec_sub(delta, old_delta);
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -080074 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
John Stultz3dcad5f2011-05-27 11:33:18 -070075 /*
76 * if delta_delta is too large, assume time correction
77 * has occured and set old_delta to the current delta.
78 */
79 old_delta = delta;
80 } else {
81 /* Otherwise try to adjust old_system to compensate */
82 old_system = timespec_sub(old_system, delta_delta);
83 }
David Brownell7ca1d482007-05-08 00:33:42 -070084
David Brownell7ca1d482007-05-08 00:33:42 -070085 return 0;
86}
87
88static int rtc_resume(struct device *dev)
89{
90 struct rtc_device *rtc = to_rtc_device(dev);
91 struct rtc_time tm;
John Stultz3dcad5f2011-05-27 11:33:18 -070092 struct timespec new_system, new_rtc;
93 struct timespec sleep_time;
David Brownell7ca1d482007-05-08 00:33:42 -070094
Feng Tang9ecf37e2013-01-16 00:09:48 +080095 if (has_persistent_clock())
96 return 0;
97
David Fries4c24e292012-10-04 17:14:12 -070098 rtc_hctosys_ret = -ENODEV;
Kay Sieversd4afc762009-01-06 14:42:11 -080099 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -0700100 return 0;
101
John Stultz3dcad5f2011-05-27 11:33:18 -0700102 /* snapshot the current rtc and system time at resume */
103 getnstimeofday(&new_system);
David Brownell7ca1d482007-05-08 00:33:42 -0700104 rtc_read_time(rtc, &tm);
105 if (rtc_valid_tm(&tm) != 0) {
Kay Sieversd4afc762009-01-06 14:42:11 -0800106 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700107 return 0;
108 }
John Stultz3dcad5f2011-05-27 11:33:18 -0700109 rtc_tm_to_time(&tm, &new_rtc.tv_sec);
110 new_rtc.tv_nsec = 0;
111
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800112 if (new_rtc.tv_sec < old_rtc.tv_sec) {
113 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700114 return 0;
115 }
116
John Stultz3dcad5f2011-05-27 11:33:18 -0700117 /* calculate the RTC time delta (sleep time)*/
118 sleep_time = timespec_sub(new_rtc, old_rtc);
David Brownell7ca1d482007-05-08 00:33:42 -0700119
John Stultz3dcad5f2011-05-27 11:33:18 -0700120 /*
121 * Since these RTC suspend/resume handlers are not called
122 * at the very end of suspend or the start of resume,
123 * some run-time may pass on either sides of the sleep time
124 * so subtract kernel run-time between rtc_suspend to rtc_resume
125 * to keep things accurate.
126 */
127 sleep_time = timespec_sub(sleep_time,
128 timespec_sub(new_system, old_system));
129
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800130 if (sleep_time.tv_sec >= 0)
131 timekeeping_inject_sleeptime(&sleep_time);
David Fries4c24e292012-10-04 17:14:12 -0700132 rtc_hctosys_ret = 0;
David Brownell7ca1d482007-05-08 00:33:42 -0700133 return 0;
134}
135
136#else
137#define rtc_suspend NULL
138#define rtc_resume NULL
139#endif
140
141
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800142/**
143 * rtc_device_register - register w/ RTC class
144 * @dev: the device to register
145 *
146 * rtc_device_unregister() must be called when the class device is no
147 * longer needed.
148 *
149 * Returns the pointer to the new struct class device.
150 */
151struct rtc_device *rtc_device_register(const char *name, struct device *dev,
David Brownellff8371a2006-09-30 23:28:17 -0700152 const struct rtc_class_ops *ops,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800153 struct module *owner)
154{
155 struct rtc_device *rtc;
John Stultzf44f7f92011-02-21 22:58:51 -0800156 struct rtc_wkalrm alrm;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800157 int id, err;
158
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700159 id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
160 if (id < 0) {
161 err = id;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800162 goto exit;
163 }
164
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800165 rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
166 if (rtc == NULL) {
167 err = -ENOMEM;
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700168 goto exit_ida;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800169 }
170
171 rtc->id = id;
172 rtc->ops = ops;
173 rtc->owner = owner;
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200174 rtc->irq_freq = 1;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700175 rtc->max_user_freq = 64;
David Brownellcd966202007-05-08 00:33:40 -0700176 rtc->dev.parent = dev;
177 rtc->dev.class = rtc_class;
178 rtc->dev.release = rtc_device_release;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800179
180 mutex_init(&rtc->ops_lock);
181 spin_lock_init(&rtc->irq_lock);
182 spin_lock_init(&rtc->irq_task_lock);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700183 init_waitqueue_head(&rtc->irq_queue);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800184
John Stultz6610e082010-09-23 15:07:34 -0700185 /* Init timerqueue */
186 timerqueue_init_head(&rtc->timerqueue);
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100187 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
John Stultz6610e082010-09-23 15:07:34 -0700188 /* Init aie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100189 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700190 /* Init uie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100191 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700192 /* Init pie timer */
193 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
194 rtc->pie_timer.function = rtc_pie_update_irq;
195 rtc->pie_enabled = 0;
196
John Stultzf44f7f92011-02-21 22:58:51 -0800197 /* Check to see if there is an ALARM already set in hw */
198 err = __rtc_read_alarm(rtc, &alrm);
199
200 if (!err && !rtc_valid_tm(&alrm.time))
John Stultzf6d5b332011-03-29 18:00:27 -0700201 rtc_initialize_alarm(rtc, &alrm);
John Stultzf44f7f92011-02-21 22:58:51 -0800202
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800203 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
Kay Sieversd4afc762009-01-06 14:42:11 -0800204 dev_set_name(&rtc->dev, "rtc%d", id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800205
David Brownellcb3a58d2007-05-08 00:33:46 -0700206 rtc_dev_prepare(rtc);
207
David Brownellcd966202007-05-08 00:33:40 -0700208 err = device_register(&rtc->dev);
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700209 if (err) {
210 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800211 goto exit_kfree;
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700212 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800213
David Brownell5726fb22007-05-08 00:33:27 -0700214 rtc_dev_add_device(rtc);
David Brownell446ecbd2007-05-08 00:33:33 -0700215 rtc_sysfs_add_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700216 rtc_proc_add_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700217
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800218 dev_info(dev, "rtc core: registered %s as %s\n",
Kay Sieversd4afc762009-01-06 14:42:11 -0800219 rtc->name, dev_name(&rtc->dev));
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800220
221 return rtc;
222
223exit_kfree:
224 kfree(rtc);
225
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700226exit_ida:
227 ida_simple_remove(&rtc_ida, id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800228
229exit:
Alessandro Zummod1d65b72006-04-10 22:54:45 -0700230 dev_err(dev, "rtc core: unable to register %s, err = %d\n",
231 name, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800232 return ERR_PTR(err);
233}
234EXPORT_SYMBOL_GPL(rtc_device_register);
235
236
237/**
238 * rtc_device_unregister - removes the previously registered RTC class device
239 *
240 * @rtc: the RTC class device to destroy
241 */
242void rtc_device_unregister(struct rtc_device *rtc)
243{
David Brownellcd966202007-05-08 00:33:40 -0700244 if (get_device(&rtc->dev) != NULL) {
David Brownelle109ebd2007-02-28 20:12:40 -0800245 mutex_lock(&rtc->ops_lock);
246 /* remove innards of this RTC, then disable it, before
247 * letting any rtc_class_open() users access it again
248 */
David Brownell446ecbd2007-05-08 00:33:33 -0700249 rtc_sysfs_del_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700250 rtc_dev_del_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700251 rtc_proc_del_device(rtc);
David Brownellcd966202007-05-08 00:33:40 -0700252 device_unregister(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800253 rtc->ops = NULL;
Vincent Palatin2830a6d2012-10-04 17:13:52 -0700254 ida_simple_remove(&rtc_ida, rtc->id);
David Brownelle109ebd2007-02-28 20:12:40 -0800255 mutex_unlock(&rtc->ops_lock);
David Brownellcd966202007-05-08 00:33:40 -0700256 put_device(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800257 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800258}
259EXPORT_SYMBOL_GPL(rtc_device_unregister);
260
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800261static int __init rtc_init(void)
262{
263 rtc_class = class_create(THIS_MODULE, "rtc");
264 if (IS_ERR(rtc_class)) {
265 printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
266 return PTR_ERR(rtc_class);
267 }
David Brownell7ca1d482007-05-08 00:33:42 -0700268 rtc_class->suspend = rtc_suspend;
269 rtc_class->resume = rtc_resume;
David Brownell5726fb22007-05-08 00:33:27 -0700270 rtc_dev_init();
David Brownell446ecbd2007-05-08 00:33:33 -0700271 rtc_sysfs_init(rtc_class);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800272 return 0;
273}
274
275static void __exit rtc_exit(void)
276{
David Brownell5726fb22007-05-08 00:33:27 -0700277 rtc_dev_exit();
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800278 class_destroy(rtc_class);
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700279 ida_destroy(&rtc_ida);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800280}
281
David Brownell818a8672006-09-30 23:28:15 -0700282subsys_initcall(rtc_init);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800283module_exit(rtc_exit);
284
David Brownell818a8672006-09-30 23:28:15 -0700285MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800286MODULE_DESCRIPTION("RTC class support");
287MODULE_LICENSE("GPL");