2 * RTC subsystem, base class
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * class skeleton from drivers/hwmon/hwmon.c
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/rtc.h>
18 #include <linux/kdev_t.h>
19 #include <linux/idr.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
26 static DEFINE_IDA(rtc_ida);
27 struct class *rtc_class;
29 static void rtc_device_release(struct device *dev)
31 struct rtc_device *rtc = to_rtc_device(dev);
32 ida_simple_remove(&rtc_ida, rtc->id);
36 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
37 /* Result of the last RTC to system clock attempt. */
38 int rtc_hctosys_ret = -ENODEV;
41 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
43 * On suspend(), measure the delta between one RTC and the
44 * system's wall clock; restore it on resume().
47 static struct timespec old_rtc, old_system, old_delta;
50 static int rtc_suspend(struct device *dev)
52 struct rtc_device *rtc = to_rtc_device(dev);
54 struct timespec delta, delta_delta;
56 if (has_persistent_clock())
59 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
62 /* snapshot the current RTC and system time at suspend*/
63 rtc_read_time(rtc, &tm);
64 getnstimeofday(&old_system);
65 rtc_tm_to_time(&tm, &old_rtc.tv_sec);
69 * To avoid drift caused by repeated suspend/resumes,
70 * which each can add ~1 second drift error,
71 * try to compensate so the difference in system time
72 * and rtc time stays close to constant.
74 delta = timespec_sub(old_system, old_rtc);
75 delta_delta = timespec_sub(delta, old_delta);
76 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
78 * if delta_delta is too large, assume time correction
79 * has occured and set old_delta to the current delta.
83 /* Otherwise try to adjust old_system to compensate */
84 old_system = timespec_sub(old_system, delta_delta);
90 static int rtc_resume(struct device *dev)
92 struct rtc_device *rtc = to_rtc_device(dev);
94 struct timespec new_system, new_rtc;
95 struct timespec sleep_time;
97 if (has_persistent_clock())
100 rtc_hctosys_ret = -ENODEV;
101 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
104 /* snapshot the current rtc and system time at resume */
105 getnstimeofday(&new_system);
106 rtc_read_time(rtc, &tm);
107 if (rtc_valid_tm(&tm) != 0) {
108 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
111 rtc_tm_to_time(&tm, &new_rtc.tv_sec);
114 if (new_rtc.tv_sec < old_rtc.tv_sec) {
115 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
119 /* calculate the RTC time delta (sleep time)*/
120 sleep_time = timespec_sub(new_rtc, old_rtc);
123 * Since these RTC suspend/resume handlers are not called
124 * at the very end of suspend or the start of resume,
125 * some run-time may pass on either sides of the sleep time
126 * so subtract kernel run-time between rtc_suspend to rtc_resume
127 * to keep things accurate.
129 sleep_time = timespec_sub(sleep_time,
130 timespec_sub(new_system, old_system));
132 if (sleep_time.tv_sec >= 0)
133 timekeeping_inject_sleeptime(&sleep_time);
138 static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
139 #define RTC_CLASS_DEV_PM_OPS (&rtc_class_dev_pm_ops)
141 #define RTC_CLASS_DEV_PM_OPS NULL
146 * rtc_device_register - register w/ RTC class
147 * @dev: the device to register
149 * rtc_device_unregister() must be called when the class device is no
152 * Returns the pointer to the new struct class device.
154 struct rtc_device *rtc_device_register(const char *name, struct device *dev,
155 const struct rtc_class_ops *ops,
156 struct module *owner)
158 struct rtc_device *rtc;
159 struct rtc_wkalrm alrm;
162 id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
168 rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
178 rtc->max_user_freq = 64;
179 rtc->dev.parent = dev;
180 rtc->dev.class = rtc_class;
181 rtc->dev.release = rtc_device_release;
183 mutex_init(&rtc->ops_lock);
184 spin_lock_init(&rtc->irq_lock);
185 spin_lock_init(&rtc->irq_task_lock);
186 init_waitqueue_head(&rtc->irq_queue);
188 /* Init timerqueue */
189 timerqueue_init_head(&rtc->timerqueue);
190 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
192 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
194 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
196 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
197 rtc->pie_timer.function = rtc_pie_update_irq;
198 rtc->pie_enabled = 0;
200 /* Check to see if there is an ALARM already set in hw */
201 err = __rtc_read_alarm(rtc, &alrm);
203 if (!err && !rtc_valid_tm(&alrm.time))
204 rtc_initialize_alarm(rtc, &alrm);
206 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
207 dev_set_name(&rtc->dev, "rtc%d", id);
209 rtc_dev_prepare(rtc);
211 err = device_register(&rtc->dev);
213 put_device(&rtc->dev);
217 rtc_dev_add_device(rtc);
218 rtc_sysfs_add_device(rtc);
219 rtc_proc_add_device(rtc);
221 dev_info(dev, "rtc core: registered %s as %s\n",
222 rtc->name, dev_name(&rtc->dev));
230 ida_simple_remove(&rtc_ida, id);
233 dev_err(dev, "rtc core: unable to register %s, err = %d\n",
237 EXPORT_SYMBOL_GPL(rtc_device_register);
241 * rtc_device_unregister - removes the previously registered RTC class device
243 * @rtc: the RTC class device to destroy
245 void rtc_device_unregister(struct rtc_device *rtc)
247 if (get_device(&rtc->dev) != NULL) {
248 mutex_lock(&rtc->ops_lock);
249 /* remove innards of this RTC, then disable it, before
250 * letting any rtc_class_open() users access it again
252 rtc_sysfs_del_device(rtc);
253 rtc_dev_del_device(rtc);
254 rtc_proc_del_device(rtc);
255 device_unregister(&rtc->dev);
257 mutex_unlock(&rtc->ops_lock);
258 put_device(&rtc->dev);
261 EXPORT_SYMBOL_GPL(rtc_device_unregister);
263 static void devm_rtc_device_release(struct device *dev, void *res)
265 struct rtc_device *rtc = *(struct rtc_device **)res;
267 rtc_device_unregister(rtc);
270 static int devm_rtc_device_match(struct device *dev, void *res, void *data)
272 struct rtc **r = res;
278 * devm_rtc_device_register - resource managed rtc_device_register()
279 * @dev: the device to register
280 * @name: the name of the device
281 * @ops: the rtc operations structure
282 * @owner: the module owner
284 * @return a struct rtc on success, or an ERR_PTR on error
286 * Managed rtc_device_register(). The rtc_device returned from this function
287 * are automatically freed on driver detach. See rtc_device_register()
288 * for more information.
291 struct rtc_device *devm_rtc_device_register(struct device *dev,
293 const struct rtc_class_ops *ops,
294 struct module *owner)
296 struct rtc_device **ptr, *rtc;
298 ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL);
300 return ERR_PTR(-ENOMEM);
302 rtc = rtc_device_register(name, dev, ops, owner);
305 devres_add(dev, ptr);
312 EXPORT_SYMBOL_GPL(devm_rtc_device_register);
315 * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister()
316 * @dev: the device to unregister
317 * @rtc: the RTC class device to unregister
319 * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this
320 * function will not need to be called and the resource management code will
321 * ensure that the resource is freed.
323 void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
327 rc = devres_release(dev, devm_rtc_device_release,
328 devm_rtc_device_match, rtc);
331 EXPORT_SYMBOL_GPL(devm_rtc_device_unregister);
333 static int __init rtc_init(void)
335 rtc_class = class_create(THIS_MODULE, "rtc");
336 if (IS_ERR(rtc_class)) {
337 pr_err("couldn't create class\n");
338 return PTR_ERR(rtc_class);
340 rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
342 rtc_sysfs_init(rtc_class);
346 static void __exit rtc_exit(void)
349 class_destroy(rtc_class);
350 ida_destroy(&rtc_ida);
353 subsys_initcall(rtc_init);
354 module_exit(rtc_exit);
356 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
357 MODULE_DESCRIPTION("RTC class support");
358 MODULE_LICENSE("GPL");