Root/
1 | /* |
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 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
15 | |
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> |
22 | |
23 | #include "rtc-core.h" |
24 | |
25 | |
26 | static DEFINE_IDA(rtc_ida); |
27 | struct class *rtc_class; |
28 | |
29 | static void rtc_device_release(struct device *dev) |
30 | { |
31 | struct rtc_device *rtc = to_rtc_device(dev); |
32 | ida_simple_remove(&rtc_ida, rtc->id); |
33 | kfree(rtc); |
34 | } |
35 | |
36 | #ifdef CONFIG_RTC_HCTOSYS_DEVICE |
37 | /* Result of the last RTC to system clock attempt. */ |
38 | int rtc_hctosys_ret = -ENODEV; |
39 | #endif |
40 | |
41 | #if defined(CONFIG_PM) && defined(CONFIG_RTC_HCTOSYS_DEVICE) |
42 | /* |
43 | * On suspend(), measure the delta between one RTC and the |
44 | * system's wall clock; restore it on resume(). |
45 | */ |
46 | |
47 | static struct timespec old_rtc, old_system, old_delta; |
48 | |
49 | |
50 | static 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; |
54 | struct timespec delta, delta_delta; |
55 | |
56 | if (has_persistent_clock()) |
57 | return 0; |
58 | |
59 | if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) |
60 | return 0; |
61 | |
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); |
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); |
76 | if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) { |
77 | /* |
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 | } |
86 | |
87 | return 0; |
88 | } |
89 | |
90 | static int rtc_resume(struct device *dev) |
91 | { |
92 | struct rtc_device *rtc = to_rtc_device(dev); |
93 | struct rtc_time tm; |
94 | struct timespec new_system, new_rtc; |
95 | struct timespec sleep_time; |
96 | |
97 | if (has_persistent_clock()) |
98 | return 0; |
99 | |
100 | rtc_hctosys_ret = -ENODEV; |
101 | if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) |
102 | return 0; |
103 | |
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)); |
109 | return 0; |
110 | } |
111 | rtc_tm_to_time(&tm, &new_rtc.tv_sec); |
112 | new_rtc.tv_nsec = 0; |
113 | |
114 | if (new_rtc.tv_sec < old_rtc.tv_sec) { |
115 | pr_debug("%s: time travel!\n", dev_name(&rtc->dev)); |
116 | return 0; |
117 | } |
118 | |
119 | /* calculate the RTC time delta (sleep time)*/ |
120 | sleep_time = timespec_sub(new_rtc, old_rtc); |
121 | |
122 | /* |
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 | |
132 | if (sleep_time.tv_sec >= 0) |
133 | timekeeping_inject_sleeptime(&sleep_time); |
134 | rtc_hctosys_ret = 0; |
135 | return 0; |
136 | } |
137 | |
138 | #else |
139 | #define rtc_suspend NULL |
140 | #define rtc_resume NULL |
141 | #endif |
142 | |
143 | |
144 | /** |
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 | */ |
153 | struct rtc_device *rtc_device_register(const char *name, struct device *dev, |
154 | const struct rtc_class_ops *ops, |
155 | struct module *owner) |
156 | { |
157 | struct rtc_device *rtc; |
158 | struct rtc_wkalrm alrm; |
159 | int id, err; |
160 | |
161 | id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL); |
162 | if (id < 0) { |
163 | err = id; |
164 | goto exit; |
165 | } |
166 | |
167 | rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL); |
168 | if (rtc == NULL) { |
169 | err = -ENOMEM; |
170 | goto exit_ida; |
171 | } |
172 | |
173 | rtc->id = id; |
174 | rtc->ops = ops; |
175 | rtc->owner = owner; |
176 | rtc->irq_freq = 1; |
177 | rtc->max_user_freq = 64; |
178 | rtc->dev.parent = dev; |
179 | rtc->dev.class = rtc_class; |
180 | rtc->dev.release = rtc_device_release; |
181 | |
182 | mutex_init(&rtc->ops_lock); |
183 | spin_lock_init(&rtc->irq_lock); |
184 | spin_lock_init(&rtc->irq_task_lock); |
185 | init_waitqueue_head(&rtc->irq_queue); |
186 | |
187 | /* Init timerqueue */ |
188 | timerqueue_init_head(&rtc->timerqueue); |
189 | INIT_WORK(&rtc->irqwork, rtc_timer_do_work); |
190 | /* Init aie timer */ |
191 | rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc); |
192 | /* Init uie timer */ |
193 | rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc); |
194 | /* 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 | |
199 | /* 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)) |
203 | rtc_initialize_alarm(rtc, &alrm); |
204 | |
205 | strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE); |
206 | dev_set_name(&rtc->dev, "rtc%d", id); |
207 | |
208 | rtc_dev_prepare(rtc); |
209 | |
210 | err = device_register(&rtc->dev); |
211 | if (err) { |
212 | put_device(&rtc->dev); |
213 | goto exit_kfree; |
214 | } |
215 | |
216 | rtc_dev_add_device(rtc); |
217 | rtc_sysfs_add_device(rtc); |
218 | rtc_proc_add_device(rtc); |
219 | |
220 | dev_info(dev, "rtc core: registered %s as %s\n", |
221 | rtc->name, dev_name(&rtc->dev)); |
222 | |
223 | return rtc; |
224 | |
225 | exit_kfree: |
226 | kfree(rtc); |
227 | |
228 | exit_ida: |
229 | ida_simple_remove(&rtc_ida, id); |
230 | |
231 | exit: |
232 | dev_err(dev, "rtc core: unable to register %s, err = %d\n", |
233 | name, err); |
234 | return ERR_PTR(err); |
235 | } |
236 | EXPORT_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 | */ |
244 | void rtc_device_unregister(struct rtc_device *rtc) |
245 | { |
246 | if (get_device(&rtc->dev) != NULL) { |
247 | 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 | */ |
251 | rtc_sysfs_del_device(rtc); |
252 | rtc_dev_del_device(rtc); |
253 | rtc_proc_del_device(rtc); |
254 | device_unregister(&rtc->dev); |
255 | rtc->ops = NULL; |
256 | mutex_unlock(&rtc->ops_lock); |
257 | put_device(&rtc->dev); |
258 | } |
259 | } |
260 | EXPORT_SYMBOL_GPL(rtc_device_unregister); |
261 | |
262 | static int __init rtc_init(void) |
263 | { |
264 | rtc_class = class_create(THIS_MODULE, "rtc"); |
265 | if (IS_ERR(rtc_class)) { |
266 | pr_err("couldn't create class\n"); |
267 | return PTR_ERR(rtc_class); |
268 | } |
269 | rtc_class->suspend = rtc_suspend; |
270 | rtc_class->resume = rtc_resume; |
271 | rtc_dev_init(); |
272 | rtc_sysfs_init(rtc_class); |
273 | return 0; |
274 | } |
275 | |
276 | static void __exit rtc_exit(void) |
277 | { |
278 | rtc_dev_exit(); |
279 | class_destroy(rtc_class); |
280 | ida_destroy(&rtc_ida); |
281 | } |
282 | |
283 | subsys_initcall(rtc_init); |
284 | module_exit(rtc_exit); |
285 | |
286 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
287 | MODULE_DESCRIPTION("RTC class support"); |
288 | MODULE_LICENSE("GPL"); |
289 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9