blob: b72b40bd780517dd8f6568ef65fb791277b5df7d [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
Jingoo Hanc100a5e2013-02-21 16:45:23 -080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080016#include <linux/module.h>
17#include <linux/rtc.h>
18#include <linux/kdev_t.h>
19#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
John Stultz6610e082010-09-23 15:07:34 -070021#include <linux/workqueue.h>
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080022
David Brownell5726fb22007-05-08 00:33:27 -070023#include "rtc-core.h"
24
25
Jonathan Cameron6d03d062011-11-02 13:37:49 -070026static DEFINE_IDA(rtc_ida);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080027struct class *rtc_class;
28
David Brownellcd966202007-05-08 00:33:40 -070029static void rtc_device_release(struct device *dev)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080030{
David Brownellcd966202007-05-08 00:33:40 -070031 struct rtc_device *rtc = to_rtc_device(dev);
Jonathan Cameron6d03d062011-11-02 13:37:49 -070032 ida_simple_remove(&rtc_ida, rtc->id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080033 kfree(rtc);
34}
35
David Fries4c24e292012-10-04 17:14:12 -070036#ifdef CONFIG_RTC_HCTOSYS_DEVICE
37/* Result of the last RTC to system clock attempt. */
38int rtc_hctosys_ret = -ENODEV;
39#endif
David Brownell7ca1d482007-05-08 00:33:42 -070040
David Fries4c24e292012-10-04 17:14:12 -070041#if defined(CONFIG_PM) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
David Brownell7ca1d482007-05-08 00:33:42 -070042/*
43 * On suspend(), measure the delta between one RTC and the
44 * system's wall clock; restore it on resume().
45 */
46
John Stultz3dcad5f2011-05-27 11:33:18 -070047static struct timespec old_rtc, old_system, old_delta;
48
David Brownell7ca1d482007-05-08 00:33:42 -070049
50static int rtc_suspend(struct device *dev, pm_message_t mesg)
51{
52 struct rtc_device *rtc = to_rtc_device(dev);
53 struct rtc_time tm;
John Stultz3dcad5f2011-05-27 11:33:18 -070054 struct timespec delta, delta_delta;
Feng Tang9ecf37e2013-01-16 00:09:48 +080055
56 if (has_persistent_clock())
57 return 0;
58
Kay Sieversd4afc762009-01-06 14:42:11 -080059 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -070060 return 0;
61
John Stultz3dcad5f2011-05-27 11:33:18 -070062 /* snapshot the current RTC and system time at suspend*/
David Brownell7ca1d482007-05-08 00:33:42 -070063 rtc_read_time(rtc, &tm);
John Stultz3dcad5f2011-05-27 11:33:18 -070064 getnstimeofday(&old_system);
65 rtc_tm_to_time(&tm, &old_rtc.tv_sec);
66
67
68 /*
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.
73 */
74 delta = timespec_sub(old_system, old_rtc);
75 delta_delta = timespec_sub(delta, old_delta);
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -080076 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
John Stultz3dcad5f2011-05-27 11:33:18 -070077 /*
78 * if delta_delta is too large, assume time correction
79 * has occured and set old_delta to the current delta.
80 */
81 old_delta = delta;
82 } else {
83 /* Otherwise try to adjust old_system to compensate */
84 old_system = timespec_sub(old_system, delta_delta);
85 }
David Brownell7ca1d482007-05-08 00:33:42 -070086
David Brownell7ca1d482007-05-08 00:33:42 -070087 return 0;
88}
89
90static int rtc_resume(struct device *dev)
91{
92 struct rtc_device *rtc = to_rtc_device(dev);
93 struct rtc_time tm;
John Stultz3dcad5f2011-05-27 11:33:18 -070094 struct timespec new_system, new_rtc;
95 struct timespec sleep_time;
David Brownell7ca1d482007-05-08 00:33:42 -070096
Feng Tang9ecf37e2013-01-16 00:09:48 +080097 if (has_persistent_clock())
98 return 0;
99
David Fries4c24e292012-10-04 17:14:12 -0700100 rtc_hctosys_ret = -ENODEV;
Kay Sieversd4afc762009-01-06 14:42:11 -0800101 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -0700102 return 0;
103
John Stultz3dcad5f2011-05-27 11:33:18 -0700104 /* snapshot the current rtc and system time at resume */
105 getnstimeofday(&new_system);
David Brownell7ca1d482007-05-08 00:33:42 -0700106 rtc_read_time(rtc, &tm);
107 if (rtc_valid_tm(&tm) != 0) {
Kay Sieversd4afc762009-01-06 14:42:11 -0800108 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700109 return 0;
110 }
John Stultz3dcad5f2011-05-27 11:33:18 -0700111 rtc_tm_to_time(&tm, &new_rtc.tv_sec);
112 new_rtc.tv_nsec = 0;
113
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800114 if (new_rtc.tv_sec < old_rtc.tv_sec) {
115 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700116 return 0;
117 }
118
John Stultz3dcad5f2011-05-27 11:33:18 -0700119 /* calculate the RTC time delta (sleep time)*/
120 sleep_time = timespec_sub(new_rtc, old_rtc);
David Brownell7ca1d482007-05-08 00:33:42 -0700121
John Stultz3dcad5f2011-05-27 11:33:18 -0700122 /*
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.
128 */
129 sleep_time = timespec_sub(sleep_time,
130 timespec_sub(new_system, old_system));
131
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800132 if (sleep_time.tv_sec >= 0)
133 timekeeping_inject_sleeptime(&sleep_time);
David Fries4c24e292012-10-04 17:14:12 -0700134 rtc_hctosys_ret = 0;
David Brownell7ca1d482007-05-08 00:33:42 -0700135 return 0;
136}
137
138#else
139#define rtc_suspend NULL
140#define rtc_resume NULL
141#endif
142
143
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800144/**
145 * rtc_device_register - register w/ RTC class
146 * @dev: the device to register
147 *
148 * rtc_device_unregister() must be called when the class device is no
149 * longer needed.
150 *
151 * Returns the pointer to the new struct class device.
152 */
153struct rtc_device *rtc_device_register(const char *name, struct device *dev,
David Brownellff8371a2006-09-30 23:28:17 -0700154 const struct rtc_class_ops *ops,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800155 struct module *owner)
156{
157 struct rtc_device *rtc;
John Stultzf44f7f92011-02-21 22:58:51 -0800158 struct rtc_wkalrm alrm;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800159 int id, err;
160
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700161 id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
162 if (id < 0) {
163 err = id;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800164 goto exit;
165 }
166
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800167 rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
168 if (rtc == NULL) {
169 err = -ENOMEM;
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700170 goto exit_ida;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800171 }
172
173 rtc->id = id;
174 rtc->ops = ops;
175 rtc->owner = owner;
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200176 rtc->irq_freq = 1;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700177 rtc->max_user_freq = 64;
David Brownellcd966202007-05-08 00:33:40 -0700178 rtc->dev.parent = dev;
179 rtc->dev.class = rtc_class;
180 rtc->dev.release = rtc_device_release;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800181
182 mutex_init(&rtc->ops_lock);
183 spin_lock_init(&rtc->irq_lock);
184 spin_lock_init(&rtc->irq_task_lock);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700185 init_waitqueue_head(&rtc->irq_queue);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800186
John Stultz6610e082010-09-23 15:07:34 -0700187 /* Init timerqueue */
188 timerqueue_init_head(&rtc->timerqueue);
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100189 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
John Stultz6610e082010-09-23 15:07:34 -0700190 /* Init aie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100191 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700192 /* Init uie timer */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100193 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
John Stultz6610e082010-09-23 15:07:34 -0700194 /* Init pie timer */
195 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
196 rtc->pie_timer.function = rtc_pie_update_irq;
197 rtc->pie_enabled = 0;
198
John Stultzf44f7f92011-02-21 22:58:51 -0800199 /* Check to see if there is an ALARM already set in hw */
200 err = __rtc_read_alarm(rtc, &alrm);
201
202 if (!err && !rtc_valid_tm(&alrm.time))
John Stultzf6d5b332011-03-29 18:00:27 -0700203 rtc_initialize_alarm(rtc, &alrm);
John Stultzf44f7f92011-02-21 22:58:51 -0800204
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800205 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
Kay Sieversd4afc762009-01-06 14:42:11 -0800206 dev_set_name(&rtc->dev, "rtc%d", id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800207
David Brownellcb3a58d2007-05-08 00:33:46 -0700208 rtc_dev_prepare(rtc);
209
David Brownellcd966202007-05-08 00:33:40 -0700210 err = device_register(&rtc->dev);
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700211 if (err) {
212 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800213 goto exit_kfree;
Vasiliy Kulikov59cca862010-10-27 15:33:04 -0700214 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800215
David Brownell5726fb22007-05-08 00:33:27 -0700216 rtc_dev_add_device(rtc);
David Brownell446ecbd2007-05-08 00:33:33 -0700217 rtc_sysfs_add_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700218 rtc_proc_add_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700219
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800220 dev_info(dev, "rtc core: registered %s as %s\n",
Kay Sieversd4afc762009-01-06 14:42:11 -0800221 rtc->name, dev_name(&rtc->dev));
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800222
223 return rtc;
224
225exit_kfree:
226 kfree(rtc);
227
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700228exit_ida:
229 ida_simple_remove(&rtc_ida, id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800230
231exit:
Alessandro Zummod1d65b72006-04-10 22:54:45 -0700232 dev_err(dev, "rtc core: unable to register %s, err = %d\n",
233 name, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800234 return ERR_PTR(err);
235}
236EXPORT_SYMBOL_GPL(rtc_device_register);
237
238
239/**
240 * rtc_device_unregister - removes the previously registered RTC class device
241 *
242 * @rtc: the RTC class device to destroy
243 */
244void rtc_device_unregister(struct rtc_device *rtc)
245{
David Brownellcd966202007-05-08 00:33:40 -0700246 if (get_device(&rtc->dev) != NULL) {
David Brownelle109ebd2007-02-28 20:12:40 -0800247 mutex_lock(&rtc->ops_lock);
248 /* remove innards of this RTC, then disable it, before
249 * letting any rtc_class_open() users access it again
250 */
David Brownell446ecbd2007-05-08 00:33:33 -0700251 rtc_sysfs_del_device(rtc);
David Brownell5726fb22007-05-08 00:33:27 -0700252 rtc_dev_del_device(rtc);
David Brownell7d9f99e2007-05-08 00:33:38 -0700253 rtc_proc_del_device(rtc);
David Brownellcd966202007-05-08 00:33:40 -0700254 device_unregister(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800255 rtc->ops = NULL;
256 mutex_unlock(&rtc->ops_lock);
David Brownellcd966202007-05-08 00:33:40 -0700257 put_device(&rtc->dev);
David Brownelle109ebd2007-02-28 20:12:40 -0800258 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800259}
260EXPORT_SYMBOL_GPL(rtc_device_unregister);
261
Jingoo Han3e217b62013-04-29 16:18:27 -0700262static void devm_rtc_device_release(struct device *dev, void *res)
263{
264 struct rtc_device *rtc = *(struct rtc_device **)res;
265
266 rtc_device_unregister(rtc);
267}
268
269static int devm_rtc_device_match(struct device *dev, void *res, void *data)
270{
271 struct rtc **r = res;
272
273 return *r == data;
274}
275
276/**
277 * devm_rtc_device_register - resource managed rtc_device_register()
278 * @name: the name of the device
279 * @dev: the device to register
280 * @ops: the rtc operations structure
281 * @owner: the module owner
282 *
283 * @return a struct rtc on success, or an ERR_PTR on error
284 *
285 * Managed rtc_device_register(). The rtc_device returned from this function
286 * are automatically freed on driver detach. See rtc_device_register()
287 * for more information.
288 */
289
290struct rtc_device *devm_rtc_device_register(const char *name,
291 struct device *dev,
292 const struct rtc_class_ops *ops,
293 struct module *owner)
294{
295 struct rtc_device **ptr, *rtc;
296
297 ptr = devres_alloc(devm_rtc_device_release, sizeof(*ptr), GFP_KERNEL);
298 if (!ptr)
299 return ERR_PTR(-ENOMEM);
300
301 rtc = rtc_device_register(name, dev, ops, owner);
302 if (!IS_ERR(rtc)) {
303 *ptr = rtc;
304 devres_add(dev, ptr);
305 } else {
306 devres_free(ptr);
307 }
308
309 return rtc;
310}
311EXPORT_SYMBOL_GPL(devm_rtc_device_register);
312
313/**
314 * devm_rtc_device_unregister - resource managed devm_rtc_device_unregister()
315 * @dev: the device to unregister
316 * @rtc: the RTC class device to unregister
317 *
318 * Deallocated a rtc allocated with devm_rtc_device_register(). Normally this
319 * function will not need to be called and the resource management code will
320 * ensure that the resource is freed.
321 */
322void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)
323{
324 int rc;
325
326 rc = devres_release(dev, devm_rtc_device_release,
327 devm_rtc_device_match, rtc);
328 WARN_ON(rc);
329}
330EXPORT_SYMBOL_GPL(devm_rtc_device_unregister);
331
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800332static int __init rtc_init(void)
333{
334 rtc_class = class_create(THIS_MODULE, "rtc");
335 if (IS_ERR(rtc_class)) {
Jingoo Hanc100a5e2013-02-21 16:45:23 -0800336 pr_err("couldn't create class\n");
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800337 return PTR_ERR(rtc_class);
338 }
David Brownell7ca1d482007-05-08 00:33:42 -0700339 rtc_class->suspend = rtc_suspend;
340 rtc_class->resume = rtc_resume;
David Brownell5726fb22007-05-08 00:33:27 -0700341 rtc_dev_init();
David Brownell446ecbd2007-05-08 00:33:33 -0700342 rtc_sysfs_init(rtc_class);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800343 return 0;
344}
345
346static void __exit rtc_exit(void)
347{
David Brownell5726fb22007-05-08 00:33:27 -0700348 rtc_dev_exit();
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800349 class_destroy(rtc_class);
Jonathan Cameron6d03d062011-11-02 13:37:49 -0700350 ida_destroy(&rtc_ida);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800351}
352
David Brownell818a8672006-09-30 23:28:15 -0700353subsys_initcall(rtc_init);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800354module_exit(rtc_exit);
355
David Brownell818a8672006-09-30 23:28:15 -0700356MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800357MODULE_DESCRIPTION("RTC class support");
358MODULE_LICENSE("GPL");