Commit | Line | Data |
---|---|---|
7be2c7c9 DB |
1 | /* |
2 | * RTC class driver for "CMOS RTC": PCs, ACPI, etc | |
3 | * | |
4 | * Copyright (C) 1996 Paul Gortmaker (drivers/char/rtc.c) | |
5 | * Copyright (C) 2006 David Brownell (convert to new framework) | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation; either version | |
10 | * 2 of the License, or (at your option) any later version. | |
11 | */ | |
12 | ||
13 | /* | |
14 | * The original "cmos clock" chip was an MC146818 chip, now obsolete. | |
15 | * That defined the register interface now provided by all PCs, some | |
16 | * non-PC systems, and incorporated into ACPI. Modern PC chipsets | |
17 | * integrate an MC146818 clone in their southbridge, and boards use | |
18 | * that instead of discrete clones like the DS12887 or M48T86. There | |
19 | * are also clones that connect using the LPC bus. | |
20 | * | |
21 | * That register API is also used directly by various other drivers | |
22 | * (notably for integrated NVRAM), infrastructure (x86 has code to | |
23 | * bypass the RTC framework, directly reading the RTC during boot | |
24 | * and updating minutes/seconds for systems using NTP synch) and | |
25 | * utilities (like userspace 'hwclock', if no /dev node exists). | |
26 | * | |
27 | * So **ALL** calls to CMOS_READ and CMOS_WRITE must be done with | |
28 | * interrupts disabled, holding the global rtc_lock, to exclude those | |
29 | * other drivers and utilities on correctly configured systems. | |
30 | */ | |
31 | #include <linux/kernel.h> | |
32 | #include <linux/module.h> | |
33 | #include <linux/init.h> | |
34 | #include <linux/interrupt.h> | |
35 | #include <linux/spinlock.h> | |
36 | #include <linux/platform_device.h> | |
37 | #include <linux/mod_devicetable.h> | |
38 | ||
39 | /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */ | |
40 | #include <asm-generic/rtc.h> | |
41 | ||
42 | ||
43 | struct cmos_rtc { | |
44 | struct rtc_device *rtc; | |
45 | struct device *dev; | |
46 | int irq; | |
47 | struct resource *iomem; | |
48 | ||
87ac84f4 DB |
49 | void (*wake_on)(struct device *); |
50 | void (*wake_off)(struct device *); | |
51 | ||
52 | u8 enabled_wake; | |
7be2c7c9 DB |
53 | u8 suspend_ctrl; |
54 | ||
55 | /* newer hardware extends the original register set */ | |
56 | u8 day_alrm; | |
57 | u8 mon_alrm; | |
58 | u8 century; | |
59 | }; | |
60 | ||
61 | /* both platform and pnp busses use negative numbers for invalid irqs */ | |
62 | #define is_valid_irq(n) ((n) >= 0) | |
63 | ||
64 | static const char driver_name[] = "rtc_cmos"; | |
65 | ||
bcd9b89c DB |
66 | /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear; |
67 | * always mask it against the irq enable bits in RTC_CONTROL. Bit values | |
68 | * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both. | |
69 | */ | |
70 | #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF) | |
71 | ||
72 | static inline int is_intr(u8 rtc_intr) | |
73 | { | |
74 | if (!(rtc_intr & RTC_IRQF)) | |
75 | return 0; | |
76 | return rtc_intr & RTC_IRQMASK; | |
77 | } | |
78 | ||
7be2c7c9 DB |
79 | /*----------------------------------------------------------------*/ |
80 | ||
81 | static int cmos_read_time(struct device *dev, struct rtc_time *t) | |
82 | { | |
83 | /* REVISIT: if the clock has a "century" register, use | |
84 | * that instead of the heuristic in get_rtc_time(). | |
85 | * That'll make Y3K compatility (year > 2070) easy! | |
86 | */ | |
87 | get_rtc_time(t); | |
88 | return 0; | |
89 | } | |
90 | ||
91 | static int cmos_set_time(struct device *dev, struct rtc_time *t) | |
92 | { | |
93 | /* REVISIT: set the "century" register if available | |
94 | * | |
95 | * NOTE: this ignores the issue whereby updating the seconds | |
96 | * takes effect exactly 500ms after we write the register. | |
97 | * (Also queueing and other delays before we get this far.) | |
98 | */ | |
99 | return set_rtc_time(t); | |
100 | } | |
101 | ||
102 | static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t) | |
103 | { | |
104 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
105 | unsigned char rtc_control; | |
106 | ||
107 | if (!is_valid_irq(cmos->irq)) | |
108 | return -EIO; | |
109 | ||
110 | /* Basic alarms only support hour, minute, and seconds fields. | |
111 | * Some also support day and month, for alarms up to a year in | |
112 | * the future. | |
113 | */ | |
114 | t->time.tm_mday = -1; | |
115 | t->time.tm_mon = -1; | |
116 | ||
117 | spin_lock_irq(&rtc_lock); | |
118 | t->time.tm_sec = CMOS_READ(RTC_SECONDS_ALARM); | |
119 | t->time.tm_min = CMOS_READ(RTC_MINUTES_ALARM); | |
120 | t->time.tm_hour = CMOS_READ(RTC_HOURS_ALARM); | |
121 | ||
122 | if (cmos->day_alrm) { | |
123 | t->time.tm_mday = CMOS_READ(cmos->day_alrm); | |
124 | if (!t->time.tm_mday) | |
125 | t->time.tm_mday = -1; | |
126 | ||
127 | if (cmos->mon_alrm) { | |
128 | t->time.tm_mon = CMOS_READ(cmos->mon_alrm); | |
129 | if (!t->time.tm_mon) | |
130 | t->time.tm_mon = -1; | |
131 | } | |
132 | } | |
133 | ||
134 | rtc_control = CMOS_READ(RTC_CONTROL); | |
135 | spin_unlock_irq(&rtc_lock); | |
136 | ||
137 | /* REVISIT this assumes PC style usage: always BCD */ | |
138 | ||
139 | if (((unsigned)t->time.tm_sec) < 0x60) | |
140 | t->time.tm_sec = BCD2BIN(t->time.tm_sec); | |
141 | else | |
142 | t->time.tm_sec = -1; | |
143 | if (((unsigned)t->time.tm_min) < 0x60) | |
144 | t->time.tm_min = BCD2BIN(t->time.tm_min); | |
145 | else | |
146 | t->time.tm_min = -1; | |
147 | if (((unsigned)t->time.tm_hour) < 0x24) | |
148 | t->time.tm_hour = BCD2BIN(t->time.tm_hour); | |
149 | else | |
150 | t->time.tm_hour = -1; | |
151 | ||
152 | if (cmos->day_alrm) { | |
153 | if (((unsigned)t->time.tm_mday) <= 0x31) | |
154 | t->time.tm_mday = BCD2BIN(t->time.tm_mday); | |
155 | else | |
156 | t->time.tm_mday = -1; | |
157 | if (cmos->mon_alrm) { | |
158 | if (((unsigned)t->time.tm_mon) <= 0x12) | |
159 | t->time.tm_mon = BCD2BIN(t->time.tm_mon) - 1; | |
160 | else | |
161 | t->time.tm_mon = -1; | |
162 | } | |
163 | } | |
164 | t->time.tm_year = -1; | |
165 | ||
166 | t->enabled = !!(rtc_control & RTC_AIE); | |
167 | t->pending = 0; | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t) | |
173 | { | |
174 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
175 | unsigned char mon, mday, hrs, min, sec; | |
176 | unsigned char rtc_control, rtc_intr; | |
177 | ||
178 | if (!is_valid_irq(cmos->irq)) | |
179 | return -EIO; | |
180 | ||
181 | /* REVISIT this assumes PC style usage: always BCD */ | |
182 | ||
183 | /* Writing 0xff means "don't care" or "match all". */ | |
184 | ||
185 | mon = t->time.tm_mon; | |
186 | mon = (mon < 12) ? BIN2BCD(mon) : 0xff; | |
187 | mon++; | |
188 | ||
189 | mday = t->time.tm_mday; | |
190 | mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff; | |
191 | ||
192 | hrs = t->time.tm_hour; | |
193 | hrs = (hrs < 24) ? BIN2BCD(hrs) : 0xff; | |
194 | ||
195 | min = t->time.tm_min; | |
196 | min = (min < 60) ? BIN2BCD(min) : 0xff; | |
197 | ||
198 | sec = t->time.tm_sec; | |
199 | sec = (sec < 60) ? BIN2BCD(sec) : 0xff; | |
200 | ||
201 | spin_lock_irq(&rtc_lock); | |
202 | ||
203 | /* next rtc irq must not be from previous alarm setting */ | |
204 | rtc_control = CMOS_READ(RTC_CONTROL); | |
205 | rtc_control &= ~RTC_AIE; | |
206 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
207 | rtc_intr = CMOS_READ(RTC_INTR_FLAGS); | |
bcd9b89c DB |
208 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; |
209 | if (is_intr(rtc_intr)) | |
ab6a2d70 | 210 | rtc_update_irq(cmos->rtc, 1, rtc_intr); |
7be2c7c9 DB |
211 | |
212 | /* update alarm */ | |
213 | CMOS_WRITE(hrs, RTC_HOURS_ALARM); | |
214 | CMOS_WRITE(min, RTC_MINUTES_ALARM); | |
215 | CMOS_WRITE(sec, RTC_SECONDS_ALARM); | |
216 | ||
217 | /* the system may support an "enhanced" alarm */ | |
218 | if (cmos->day_alrm) { | |
219 | CMOS_WRITE(mday, cmos->day_alrm); | |
220 | if (cmos->mon_alrm) | |
221 | CMOS_WRITE(mon, cmos->mon_alrm); | |
222 | } | |
223 | ||
224 | if (t->enabled) { | |
225 | rtc_control |= RTC_AIE; | |
226 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
227 | rtc_intr = CMOS_READ(RTC_INTR_FLAGS); | |
bcd9b89c DB |
228 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; |
229 | if (is_intr(rtc_intr)) | |
ab6a2d70 | 230 | rtc_update_irq(cmos->rtc, 1, rtc_intr); |
7be2c7c9 DB |
231 | } |
232 | ||
233 | spin_unlock_irq(&rtc_lock); | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
57deb526 | 238 | static int cmos_irq_set_freq(struct device *dev, int freq) |
7be2c7c9 DB |
239 | { |
240 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
241 | int f; | |
242 | unsigned long flags; | |
243 | ||
244 | if (!is_valid_irq(cmos->irq)) | |
245 | return -ENXIO; | |
246 | ||
247 | /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */ | |
248 | f = ffs(freq); | |
97144c67 DB |
249 | if (f-- > 16) |
250 | return -EINVAL; | |
251 | f = 16 - f; | |
7be2c7c9 DB |
252 | |
253 | spin_lock_irqsave(&rtc_lock, flags); | |
254 | CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT); | |
255 | spin_unlock_irqrestore(&rtc_lock, flags); | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
57deb526 AZ |
260 | static int cmos_irq_set_state(struct device *dev, int enabled) |
261 | { | |
262 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
263 | unsigned char rtc_control, rtc_intr; | |
264 | unsigned long flags; | |
265 | ||
266 | if (!is_valid_irq(cmos->irq)) | |
267 | return -ENXIO; | |
268 | ||
269 | spin_lock_irqsave(&rtc_lock, flags); | |
270 | rtc_control = CMOS_READ(RTC_CONTROL); | |
271 | ||
272 | if (enabled) | |
273 | rtc_control |= RTC_PIE; | |
274 | else | |
275 | rtc_control &= ~RTC_PIE; | |
276 | ||
277 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
278 | ||
279 | rtc_intr = CMOS_READ(RTC_INTR_FLAGS); | |
280 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; | |
281 | if (is_intr(rtc_intr)) | |
282 | rtc_update_irq(cmos->rtc, 1, rtc_intr); | |
283 | ||
284 | spin_unlock_irqrestore(&rtc_lock, flags); | |
285 | return 0; | |
286 | } | |
287 | ||
7be2c7c9 DB |
288 | #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE) |
289 | ||
290 | static int | |
291 | cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) | |
292 | { | |
293 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
294 | unsigned char rtc_control, rtc_intr; | |
295 | unsigned long flags; | |
296 | ||
297 | switch (cmd) { | |
298 | case RTC_AIE_OFF: | |
299 | case RTC_AIE_ON: | |
300 | case RTC_UIE_OFF: | |
301 | case RTC_UIE_ON: | |
302 | case RTC_PIE_OFF: | |
303 | case RTC_PIE_ON: | |
304 | if (!is_valid_irq(cmos->irq)) | |
305 | return -EINVAL; | |
306 | break; | |
307 | default: | |
308 | return -ENOIOCTLCMD; | |
309 | } | |
310 | ||
311 | spin_lock_irqsave(&rtc_lock, flags); | |
312 | rtc_control = CMOS_READ(RTC_CONTROL); | |
313 | switch (cmd) { | |
314 | case RTC_AIE_OFF: /* alarm off */ | |
315 | rtc_control &= ~RTC_AIE; | |
316 | break; | |
317 | case RTC_AIE_ON: /* alarm on */ | |
318 | rtc_control |= RTC_AIE; | |
319 | break; | |
320 | case RTC_UIE_OFF: /* update off */ | |
321 | rtc_control &= ~RTC_UIE; | |
322 | break; | |
323 | case RTC_UIE_ON: /* update on */ | |
324 | rtc_control |= RTC_UIE; | |
325 | break; | |
326 | case RTC_PIE_OFF: /* periodic off */ | |
327 | rtc_control &= ~RTC_PIE; | |
328 | break; | |
329 | case RTC_PIE_ON: /* periodic on */ | |
330 | rtc_control |= RTC_PIE; | |
331 | break; | |
332 | } | |
333 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
334 | rtc_intr = CMOS_READ(RTC_INTR_FLAGS); | |
bcd9b89c DB |
335 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; |
336 | if (is_intr(rtc_intr)) | |
ab6a2d70 | 337 | rtc_update_irq(cmos->rtc, 1, rtc_intr); |
7be2c7c9 DB |
338 | spin_unlock_irqrestore(&rtc_lock, flags); |
339 | return 0; | |
340 | } | |
341 | ||
342 | #else | |
343 | #define cmos_rtc_ioctl NULL | |
344 | #endif | |
345 | ||
346 | #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE) | |
347 | ||
348 | static int cmos_procfs(struct device *dev, struct seq_file *seq) | |
349 | { | |
350 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
351 | unsigned char rtc_control, valid; | |
352 | ||
353 | spin_lock_irq(&rtc_lock); | |
354 | rtc_control = CMOS_READ(RTC_CONTROL); | |
355 | valid = CMOS_READ(RTC_VALID); | |
356 | spin_unlock_irq(&rtc_lock); | |
357 | ||
358 | /* NOTE: at least ICH6 reports battery status using a different | |
359 | * (non-RTC) bit; and SQWE is ignored on many current systems. | |
360 | */ | |
361 | return seq_printf(seq, | |
362 | "periodic_IRQ\t: %s\n" | |
363 | "update_IRQ\t: %s\n" | |
364 | // "square_wave\t: %s\n" | |
365 | // "BCD\t\t: %s\n" | |
366 | "DST_enable\t: %s\n" | |
367 | "periodic_freq\t: %d\n" | |
368 | "batt_status\t: %s\n", | |
369 | (rtc_control & RTC_PIE) ? "yes" : "no", | |
370 | (rtc_control & RTC_UIE) ? "yes" : "no", | |
371 | // (rtc_control & RTC_SQWE) ? "yes" : "no", | |
372 | // (rtc_control & RTC_DM_BINARY) ? "no" : "yes", | |
373 | (rtc_control & RTC_DST_EN) ? "yes" : "no", | |
374 | cmos->rtc->irq_freq, | |
375 | (valid & RTC_VRT) ? "okay" : "dead"); | |
376 | } | |
377 | ||
378 | #else | |
379 | #define cmos_procfs NULL | |
380 | #endif | |
381 | ||
382 | static const struct rtc_class_ops cmos_rtc_ops = { | |
383 | .ioctl = cmos_rtc_ioctl, | |
384 | .read_time = cmos_read_time, | |
385 | .set_time = cmos_set_time, | |
386 | .read_alarm = cmos_read_alarm, | |
387 | .set_alarm = cmos_set_alarm, | |
388 | .proc = cmos_procfs, | |
57deb526 AZ |
389 | .irq_set_freq = cmos_irq_set_freq, |
390 | .irq_set_state = cmos_irq_set_state, | |
7be2c7c9 DB |
391 | }; |
392 | ||
393 | /*----------------------------------------------------------------*/ | |
394 | ||
395 | static struct cmos_rtc cmos_rtc; | |
396 | ||
397 | static irqreturn_t cmos_interrupt(int irq, void *p) | |
398 | { | |
399 | u8 irqstat; | |
400 | ||
401 | spin_lock(&rtc_lock); | |
402 | irqstat = CMOS_READ(RTC_INTR_FLAGS); | |
bcd9b89c | 403 | irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF; |
7be2c7c9 DB |
404 | spin_unlock(&rtc_lock); |
405 | ||
bcd9b89c | 406 | if (is_intr(irqstat)) { |
7be2c7c9 DB |
407 | rtc_update_irq(p, 1, irqstat); |
408 | return IRQ_HANDLED; | |
409 | } else | |
410 | return IRQ_NONE; | |
411 | } | |
412 | ||
41ac8df9 MV |
413 | #ifdef CONFIG_PNP |
414 | #define is_pnp() 1 | |
7be2c7c9 DB |
415 | #define INITSECTION |
416 | ||
417 | #else | |
41ac8df9 | 418 | #define is_pnp() 0 |
7be2c7c9 DB |
419 | #define INITSECTION __init |
420 | #endif | |
421 | ||
422 | static int INITSECTION | |
423 | cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) | |
424 | { | |
425 | struct cmos_rtc_board_info *info = dev->platform_data; | |
426 | int retval = 0; | |
427 | unsigned char rtc_control; | |
428 | ||
429 | /* there can be only one ... */ | |
430 | if (cmos_rtc.dev) | |
431 | return -EBUSY; | |
432 | ||
433 | if (!ports) | |
434 | return -ENODEV; | |
435 | ||
436 | cmos_rtc.irq = rtc_irq; | |
437 | cmos_rtc.iomem = ports; | |
438 | ||
87ac84f4 DB |
439 | /* For ACPI systems extension info comes from the FADT. On others, |
440 | * board specific setup provides it as appropriate. Systems where | |
441 | * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and | |
442 | * some almost-clones) can provide hooks to make that behave. | |
7be2c7c9 DB |
443 | */ |
444 | if (info) { | |
445 | cmos_rtc.day_alrm = info->rtc_day_alarm; | |
446 | cmos_rtc.mon_alrm = info->rtc_mon_alarm; | |
447 | cmos_rtc.century = info->rtc_century; | |
87ac84f4 DB |
448 | |
449 | if (info->wake_on && info->wake_off) { | |
450 | cmos_rtc.wake_on = info->wake_on; | |
451 | cmos_rtc.wake_off = info->wake_off; | |
452 | } | |
7be2c7c9 DB |
453 | } |
454 | ||
455 | cmos_rtc.rtc = rtc_device_register(driver_name, dev, | |
456 | &cmos_rtc_ops, THIS_MODULE); | |
457 | if (IS_ERR(cmos_rtc.rtc)) | |
458 | return PTR_ERR(cmos_rtc.rtc); | |
459 | ||
460 | cmos_rtc.dev = dev; | |
461 | dev_set_drvdata(dev, &cmos_rtc); | |
462 | ||
463 | /* platform and pnp busses handle resources incompatibly. | |
464 | * | |
465 | * REVISIT for non-x86 systems we may need to handle io memory | |
466 | * resources: ioremap them, and request_mem_region(). | |
467 | */ | |
41ac8df9 | 468 | if (is_pnp()) { |
7be2c7c9 DB |
469 | retval = request_resource(&ioport_resource, ports); |
470 | if (retval < 0) { | |
471 | dev_dbg(dev, "i/o registers already in use\n"); | |
472 | goto cleanup0; | |
473 | } | |
474 | } | |
cd966209 | 475 | rename_region(ports, cmos_rtc.rtc->dev.bus_id); |
7be2c7c9 DB |
476 | |
477 | spin_lock_irq(&rtc_lock); | |
478 | ||
479 | /* force periodic irq to CMOS reset default of 1024Hz; | |
480 | * | |
481 | * REVISIT it's been reported that at least one x86_64 ALI mobo | |
482 | * doesn't use 32KHz here ... for portability we might need to | |
483 | * do something about other clock frequencies. | |
484 | */ | |
485 | CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT); | |
486 | cmos_rtc.rtc->irq_freq = 1024; | |
487 | ||
488 | /* disable irqs. | |
489 | * | |
490 | * NOTE after changing RTC_xIE bits we always read INTR_FLAGS; | |
491 | * allegedly some older rtcs need that to handle irqs properly | |
492 | */ | |
493 | rtc_control = CMOS_READ(RTC_CONTROL); | |
494 | rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE); | |
495 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
496 | CMOS_READ(RTC_INTR_FLAGS); | |
497 | ||
498 | spin_unlock_irq(&rtc_lock); | |
499 | ||
500 | /* FIXME teach the alarm code how to handle binary mode; | |
501 | * <asm-generic/rtc.h> doesn't know 12-hour mode either. | |
502 | */ | |
503 | if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) { | |
504 | dev_dbg(dev, "only 24-hr BCD mode supported\n"); | |
505 | retval = -ENXIO; | |
506 | goto cleanup1; | |
507 | } | |
508 | ||
509 | if (is_valid_irq(rtc_irq)) | |
510 | retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED, | |
cd966209 | 511 | cmos_rtc.rtc->dev.bus_id, |
ab6a2d70 | 512 | cmos_rtc.rtc); |
7be2c7c9 DB |
513 | if (retval < 0) { |
514 | dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq); | |
515 | goto cleanup1; | |
516 | } | |
517 | ||
518 | /* REVISIT optionally make 50 or 114 bytes NVRAM available, | |
519 | * like rtc-ds1553, rtc-ds1742 ... this will often include | |
520 | * registers for century, and day/month alarm. | |
521 | */ | |
522 | ||
523 | pr_info("%s: alarms up to one %s%s\n", | |
cd966209 | 524 | cmos_rtc.rtc->dev.bus_id, |
7be2c7c9 DB |
525 | is_valid_irq(rtc_irq) |
526 | ? (cmos_rtc.mon_alrm | |
527 | ? "year" | |
528 | : (cmos_rtc.day_alrm | |
529 | ? "month" : "day")) | |
530 | : "no", | |
531 | cmos_rtc.century ? ", y3k" : "" | |
532 | ); | |
533 | ||
534 | return 0; | |
535 | ||
536 | cleanup1: | |
537 | rename_region(ports, NULL); | |
538 | cleanup0: | |
539 | rtc_device_unregister(cmos_rtc.rtc); | |
540 | return retval; | |
541 | } | |
542 | ||
543 | static void cmos_do_shutdown(void) | |
544 | { | |
545 | unsigned char rtc_control; | |
546 | ||
547 | spin_lock_irq(&rtc_lock); | |
548 | rtc_control = CMOS_READ(RTC_CONTROL); | |
549 | rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE); | |
550 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
551 | CMOS_READ(RTC_INTR_FLAGS); | |
552 | spin_unlock_irq(&rtc_lock); | |
553 | } | |
554 | ||
555 | static void __exit cmos_do_remove(struct device *dev) | |
556 | { | |
557 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
558 | ||
559 | cmos_do_shutdown(); | |
560 | ||
41ac8df9 | 561 | if (is_pnp()) |
7be2c7c9 DB |
562 | release_resource(cmos->iomem); |
563 | rename_region(cmos->iomem, NULL); | |
564 | ||
565 | if (is_valid_irq(cmos->irq)) | |
cd966209 | 566 | free_irq(cmos->irq, cmos_rtc.rtc); |
7be2c7c9 DB |
567 | |
568 | rtc_device_unregister(cmos_rtc.rtc); | |
569 | ||
570 | cmos_rtc.dev = NULL; | |
571 | dev_set_drvdata(dev, NULL); | |
572 | } | |
573 | ||
574 | #ifdef CONFIG_PM | |
575 | ||
576 | static int cmos_suspend(struct device *dev, pm_message_t mesg) | |
577 | { | |
578 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
579 | int do_wake = device_may_wakeup(dev); | |
bcd9b89c | 580 | unsigned char tmp; |
7be2c7c9 DB |
581 | |
582 | /* only the alarm might be a wakeup event source */ | |
583 | spin_lock_irq(&rtc_lock); | |
584 | cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL); | |
585 | if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) { | |
bcd9b89c DB |
586 | unsigned char irqstat; |
587 | ||
7be2c7c9 DB |
588 | if (do_wake) |
589 | tmp &= ~(RTC_PIE|RTC_UIE); | |
590 | else | |
591 | tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE); | |
592 | CMOS_WRITE(tmp, RTC_CONTROL); | |
593 | irqstat = CMOS_READ(RTC_INTR_FLAGS); | |
bcd9b89c DB |
594 | irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF; |
595 | if (is_intr(irqstat)) | |
ab6a2d70 | 596 | rtc_update_irq(cmos->rtc, 1, irqstat); |
bcd9b89c | 597 | } |
7be2c7c9 DB |
598 | spin_unlock_irq(&rtc_lock); |
599 | ||
87ac84f4 DB |
600 | if (tmp & RTC_AIE) { |
601 | cmos->enabled_wake = 1; | |
602 | if (cmos->wake_on) | |
603 | cmos->wake_on(dev); | |
604 | else | |
605 | enable_irq_wake(cmos->irq); | |
606 | } | |
7be2c7c9 DB |
607 | |
608 | pr_debug("%s: suspend%s, ctrl %02x\n", | |
cd966209 | 609 | cmos_rtc.rtc->dev.bus_id, |
7be2c7c9 DB |
610 | (tmp & RTC_AIE) ? ", alarm may wake" : "", |
611 | tmp); | |
612 | ||
613 | return 0; | |
614 | } | |
615 | ||
616 | static int cmos_resume(struct device *dev) | |
617 | { | |
618 | struct cmos_rtc *cmos = dev_get_drvdata(dev); | |
619 | unsigned char tmp = cmos->suspend_ctrl; | |
620 | ||
7be2c7c9 DB |
621 | /* re-enable any irqs previously active */ |
622 | if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) { | |
623 | ||
87ac84f4 DB |
624 | if (cmos->enabled_wake) { |
625 | if (cmos->wake_off) | |
626 | cmos->wake_off(dev); | |
627 | else | |
628 | disable_irq_wake(cmos->irq); | |
629 | cmos->enabled_wake = 0; | |
630 | } | |
7be2c7c9 DB |
631 | |
632 | spin_lock_irq(&rtc_lock); | |
633 | CMOS_WRITE(tmp, RTC_CONTROL); | |
634 | tmp = CMOS_READ(RTC_INTR_FLAGS); | |
bcd9b89c DB |
635 | tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF; |
636 | if (is_intr(tmp)) | |
ab6a2d70 | 637 | rtc_update_irq(cmos->rtc, 1, tmp); |
bcd9b89c | 638 | spin_unlock_irq(&rtc_lock); |
7be2c7c9 DB |
639 | } |
640 | ||
641 | pr_debug("%s: resume, ctrl %02x\n", | |
cd966209 | 642 | cmos_rtc.rtc->dev.bus_id, |
7be2c7c9 DB |
643 | cmos->suspend_ctrl); |
644 | ||
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
649 | #else | |
650 | #define cmos_suspend NULL | |
651 | #define cmos_resume NULL | |
652 | #endif | |
653 | ||
654 | /*----------------------------------------------------------------*/ | |
655 | ||
656 | /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems, | |
bcd9b89c | 657 | * the device node will always be created as a PNPACPI device. |
7be2c7c9 DB |
658 | */ |
659 | ||
41ac8df9 | 660 | #ifdef CONFIG_PNP |
7be2c7c9 DB |
661 | |
662 | #include <linux/pnp.h> | |
663 | ||
664 | static int __devinit | |
665 | cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) | |
666 | { | |
667 | /* REVISIT paranoia argues for a shutdown notifier, since PNP | |
668 | * drivers can't provide shutdown() methods to disable IRQs. | |
669 | * Or better yet, fix PNP to allow those methods... | |
670 | */ | |
6cd8fa87 MG |
671 | if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0)) |
672 | /* Some machines contain a PNP entry for the RTC, but | |
673 | * don't define the IRQ. It should always be safe to | |
674 | * hardcode it in these cases | |
675 | */ | |
676 | return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8); | |
677 | else | |
678 | return cmos_do_probe(&pnp->dev, | |
679 | &pnp->res.port_resource[0], | |
680 | pnp->res.irq_resource[0].start); | |
7be2c7c9 DB |
681 | } |
682 | ||
683 | static void __exit cmos_pnp_remove(struct pnp_dev *pnp) | |
684 | { | |
685 | cmos_do_remove(&pnp->dev); | |
686 | } | |
687 | ||
688 | #ifdef CONFIG_PM | |
689 | ||
690 | static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg) | |
691 | { | |
692 | return cmos_suspend(&pnp->dev, mesg); | |
693 | } | |
694 | ||
695 | static int cmos_pnp_resume(struct pnp_dev *pnp) | |
696 | { | |
697 | return cmos_resume(&pnp->dev); | |
698 | } | |
699 | ||
700 | #else | |
701 | #define cmos_pnp_suspend NULL | |
702 | #define cmos_pnp_resume NULL | |
703 | #endif | |
704 | ||
705 | ||
706 | static const struct pnp_device_id rtc_ids[] = { | |
707 | { .id = "PNP0b00", }, | |
708 | { .id = "PNP0b01", }, | |
709 | { .id = "PNP0b02", }, | |
710 | { }, | |
711 | }; | |
712 | MODULE_DEVICE_TABLE(pnp, rtc_ids); | |
713 | ||
714 | static struct pnp_driver cmos_pnp_driver = { | |
715 | .name = (char *) driver_name, | |
716 | .id_table = rtc_ids, | |
717 | .probe = cmos_pnp_probe, | |
718 | .remove = __exit_p(cmos_pnp_remove), | |
719 | ||
720 | /* flag ensures resume() gets called, and stops syslog spam */ | |
721 | .flags = PNP_DRIVER_RES_DO_NOT_CHANGE, | |
722 | .suspend = cmos_pnp_suspend, | |
723 | .resume = cmos_pnp_resume, | |
724 | }; | |
725 | ||
726 | static int __init cmos_init(void) | |
727 | { | |
728 | return pnp_register_driver(&cmos_pnp_driver); | |
729 | } | |
730 | module_init(cmos_init); | |
731 | ||
732 | static void __exit cmos_exit(void) | |
733 | { | |
734 | pnp_unregister_driver(&cmos_pnp_driver); | |
735 | } | |
736 | module_exit(cmos_exit); | |
737 | ||
41ac8df9 | 738 | #else /* no PNP */ |
7be2c7c9 DB |
739 | |
740 | /*----------------------------------------------------------------*/ | |
741 | ||
41ac8df9 | 742 | /* Platform setup should have set up an RTC device, when PNP is |
bcd9b89c | 743 | * unavailable ... this could happen even on (older) PCs. |
7be2c7c9 DB |
744 | */ |
745 | ||
746 | static int __init cmos_platform_probe(struct platform_device *pdev) | |
747 | { | |
748 | return cmos_do_probe(&pdev->dev, | |
749 | platform_get_resource(pdev, IORESOURCE_IO, 0), | |
750 | platform_get_irq(pdev, 0)); | |
751 | } | |
752 | ||
753 | static int __exit cmos_platform_remove(struct platform_device *pdev) | |
754 | { | |
755 | cmos_do_remove(&pdev->dev); | |
756 | return 0; | |
757 | } | |
758 | ||
759 | static void cmos_platform_shutdown(struct platform_device *pdev) | |
760 | { | |
761 | cmos_do_shutdown(); | |
762 | } | |
763 | ||
764 | static struct platform_driver cmos_platform_driver = { | |
765 | .remove = __exit_p(cmos_platform_remove), | |
766 | .shutdown = cmos_platform_shutdown, | |
767 | .driver = { | |
768 | .name = (char *) driver_name, | |
769 | .suspend = cmos_suspend, | |
770 | .resume = cmos_resume, | |
771 | } | |
772 | }; | |
773 | ||
774 | static int __init cmos_init(void) | |
775 | { | |
776 | return platform_driver_probe(&cmos_platform_driver, | |
777 | cmos_platform_probe); | |
778 | } | |
779 | module_init(cmos_init); | |
780 | ||
781 | static void __exit cmos_exit(void) | |
782 | { | |
783 | platform_driver_unregister(&cmos_platform_driver); | |
784 | } | |
785 | module_exit(cmos_exit); | |
786 | ||
787 | ||
41ac8df9 | 788 | #endif /* !PNP */ |
7be2c7c9 DB |
789 | |
790 | MODULE_AUTHOR("David Brownell"); | |
791 | MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs"); | |
792 | MODULE_LICENSE("GPL"); |