blob: e24ea82dc35b83b1036a2b5cf15d9e54afd2ee96 [file] [log] [blame]
David Brownell7be2c7c2007-02-10 01:46:02 -08001/*
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
43struct cmos_rtc {
44 struct rtc_device *rtc;
45 struct device *dev;
46 int irq;
47 struct resource *iomem;
48
David Brownell87ac84f2007-05-08 00:34:00 -070049 void (*wake_on)(struct device *);
50 void (*wake_off)(struct device *);
51
52 u8 enabled_wake;
David Brownell7be2c7c2007-02-10 01:46:02 -080053 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
64static const char driver_name[] = "rtc_cmos";
65
David Brownellbcd9b892007-04-01 23:49:47 -070066/* 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
72static 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
David Brownell7be2c7c2007-02-10 01:46:02 -080079/*----------------------------------------------------------------*/
80
81static 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
91static 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
102static 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
172static 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);
David Brownellbcd9b892007-04-01 23:49:47 -0700208 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
209 if (is_intr(rtc_intr))
David Brownellab6a2d72007-05-08 00:33:30 -0700210 rtc_update_irq(cmos->rtc, 1, rtc_intr);
David Brownell7be2c7c2007-02-10 01:46:02 -0800211
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);
David Brownellbcd9b892007-04-01 23:49:47 -0700228 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
229 if (is_intr(rtc_intr))
David Brownellab6a2d72007-05-08 00:33:30 -0700230 rtc_update_irq(cmos->rtc, 1, rtc_intr);
David Brownell7be2c7c2007-02-10 01:46:02 -0800231 }
232
233 spin_unlock_irq(&rtc_lock);
234
235 return 0;
236}
237
238static int cmos_set_freq(struct device *dev, int freq)
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);
249 if (f != 0) {
250 if (f-- > 16 || freq != (1 << f))
251 return -EINVAL;
252 f = 16 - f;
253 }
254
255 spin_lock_irqsave(&rtc_lock, flags);
256 CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
257 spin_unlock_irqrestore(&rtc_lock, flags);
258
259 return 0;
260}
261
262#if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
263
264static int
265cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
266{
267 struct cmos_rtc *cmos = dev_get_drvdata(dev);
268 unsigned char rtc_control, rtc_intr;
269 unsigned long flags;
270
271 switch (cmd) {
272 case RTC_AIE_OFF:
273 case RTC_AIE_ON:
274 case RTC_UIE_OFF:
275 case RTC_UIE_ON:
276 case RTC_PIE_OFF:
277 case RTC_PIE_ON:
278 if (!is_valid_irq(cmos->irq))
279 return -EINVAL;
280 break;
281 default:
282 return -ENOIOCTLCMD;
283 }
284
285 spin_lock_irqsave(&rtc_lock, flags);
286 rtc_control = CMOS_READ(RTC_CONTROL);
287 switch (cmd) {
288 case RTC_AIE_OFF: /* alarm off */
289 rtc_control &= ~RTC_AIE;
290 break;
291 case RTC_AIE_ON: /* alarm on */
292 rtc_control |= RTC_AIE;
293 break;
294 case RTC_UIE_OFF: /* update off */
295 rtc_control &= ~RTC_UIE;
296 break;
297 case RTC_UIE_ON: /* update on */
298 rtc_control |= RTC_UIE;
299 break;
300 case RTC_PIE_OFF: /* periodic off */
301 rtc_control &= ~RTC_PIE;
302 break;
303 case RTC_PIE_ON: /* periodic on */
304 rtc_control |= RTC_PIE;
305 break;
306 }
307 CMOS_WRITE(rtc_control, RTC_CONTROL);
308 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
David Brownellbcd9b892007-04-01 23:49:47 -0700309 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
310 if (is_intr(rtc_intr))
David Brownellab6a2d72007-05-08 00:33:30 -0700311 rtc_update_irq(cmos->rtc, 1, rtc_intr);
David Brownell7be2c7c2007-02-10 01:46:02 -0800312 spin_unlock_irqrestore(&rtc_lock, flags);
313 return 0;
314}
315
316#else
317#define cmos_rtc_ioctl NULL
318#endif
319
320#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
321
322static int cmos_procfs(struct device *dev, struct seq_file *seq)
323{
324 struct cmos_rtc *cmos = dev_get_drvdata(dev);
325 unsigned char rtc_control, valid;
326
327 spin_lock_irq(&rtc_lock);
328 rtc_control = CMOS_READ(RTC_CONTROL);
329 valid = CMOS_READ(RTC_VALID);
330 spin_unlock_irq(&rtc_lock);
331
332 /* NOTE: at least ICH6 reports battery status using a different
333 * (non-RTC) bit; and SQWE is ignored on many current systems.
334 */
335 return seq_printf(seq,
336 "periodic_IRQ\t: %s\n"
337 "update_IRQ\t: %s\n"
338 // "square_wave\t: %s\n"
339 // "BCD\t\t: %s\n"
340 "DST_enable\t: %s\n"
341 "periodic_freq\t: %d\n"
342 "batt_status\t: %s\n",
343 (rtc_control & RTC_PIE) ? "yes" : "no",
344 (rtc_control & RTC_UIE) ? "yes" : "no",
345 // (rtc_control & RTC_SQWE) ? "yes" : "no",
346 // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
347 (rtc_control & RTC_DST_EN) ? "yes" : "no",
348 cmos->rtc->irq_freq,
349 (valid & RTC_VRT) ? "okay" : "dead");
350}
351
352#else
353#define cmos_procfs NULL
354#endif
355
356static const struct rtc_class_ops cmos_rtc_ops = {
357 .ioctl = cmos_rtc_ioctl,
358 .read_time = cmos_read_time,
359 .set_time = cmos_set_time,
360 .read_alarm = cmos_read_alarm,
361 .set_alarm = cmos_set_alarm,
362 .proc = cmos_procfs,
363 .irq_set_freq = cmos_set_freq,
364};
365
366/*----------------------------------------------------------------*/
367
368static struct cmos_rtc cmos_rtc;
369
370static irqreturn_t cmos_interrupt(int irq, void *p)
371{
372 u8 irqstat;
373
374 spin_lock(&rtc_lock);
375 irqstat = CMOS_READ(RTC_INTR_FLAGS);
David Brownellbcd9b892007-04-01 23:49:47 -0700376 irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF;
David Brownell7be2c7c2007-02-10 01:46:02 -0800377 spin_unlock(&rtc_lock);
378
David Brownellbcd9b892007-04-01 23:49:47 -0700379 if (is_intr(irqstat)) {
David Brownell7be2c7c2007-02-10 01:46:02 -0800380 rtc_update_irq(p, 1, irqstat);
381 return IRQ_HANDLED;
382 } else
383 return IRQ_NONE;
384}
385
Marko Vrh41ac8df2007-05-08 00:34:09 -0700386#ifdef CONFIG_PNP
387#define is_pnp() 1
David Brownell7be2c7c2007-02-10 01:46:02 -0800388#define INITSECTION
389
390#else
Marko Vrh41ac8df2007-05-08 00:34:09 -0700391#define is_pnp() 0
David Brownell7be2c7c2007-02-10 01:46:02 -0800392#define INITSECTION __init
393#endif
394
395static int INITSECTION
396cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
397{
398 struct cmos_rtc_board_info *info = dev->platform_data;
399 int retval = 0;
400 unsigned char rtc_control;
401
402 /* there can be only one ... */
403 if (cmos_rtc.dev)
404 return -EBUSY;
405
406 if (!ports)
407 return -ENODEV;
408
409 cmos_rtc.irq = rtc_irq;
410 cmos_rtc.iomem = ports;
411
David Brownell87ac84f2007-05-08 00:34:00 -0700412 /* For ACPI systems extension info comes from the FADT. On others,
413 * board specific setup provides it as appropriate. Systems where
414 * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
415 * some almost-clones) can provide hooks to make that behave.
David Brownell7be2c7c2007-02-10 01:46:02 -0800416 */
417 if (info) {
418 cmos_rtc.day_alrm = info->rtc_day_alarm;
419 cmos_rtc.mon_alrm = info->rtc_mon_alarm;
420 cmos_rtc.century = info->rtc_century;
David Brownell87ac84f2007-05-08 00:34:00 -0700421
422 if (info->wake_on && info->wake_off) {
423 cmos_rtc.wake_on = info->wake_on;
424 cmos_rtc.wake_off = info->wake_off;
425 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800426 }
427
428 cmos_rtc.rtc = rtc_device_register(driver_name, dev,
429 &cmos_rtc_ops, THIS_MODULE);
430 if (IS_ERR(cmos_rtc.rtc))
431 return PTR_ERR(cmos_rtc.rtc);
432
433 cmos_rtc.dev = dev;
434 dev_set_drvdata(dev, &cmos_rtc);
435
436 /* platform and pnp busses handle resources incompatibly.
437 *
438 * REVISIT for non-x86 systems we may need to handle io memory
439 * resources: ioremap them, and request_mem_region().
440 */
Marko Vrh41ac8df2007-05-08 00:34:09 -0700441 if (is_pnp()) {
David Brownell7be2c7c2007-02-10 01:46:02 -0800442 retval = request_resource(&ioport_resource, ports);
443 if (retval < 0) {
444 dev_dbg(dev, "i/o registers already in use\n");
445 goto cleanup0;
446 }
447 }
David Brownellcd966202007-05-08 00:33:40 -0700448 rename_region(ports, cmos_rtc.rtc->dev.bus_id);
David Brownell7be2c7c2007-02-10 01:46:02 -0800449
450 spin_lock_irq(&rtc_lock);
451
452 /* force periodic irq to CMOS reset default of 1024Hz;
453 *
454 * REVISIT it's been reported that at least one x86_64 ALI mobo
455 * doesn't use 32KHz here ... for portability we might need to
456 * do something about other clock frequencies.
457 */
458 CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
459 cmos_rtc.rtc->irq_freq = 1024;
460
461 /* disable irqs.
462 *
463 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
464 * allegedly some older rtcs need that to handle irqs properly
465 */
466 rtc_control = CMOS_READ(RTC_CONTROL);
467 rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE);
468 CMOS_WRITE(rtc_control, RTC_CONTROL);
469 CMOS_READ(RTC_INTR_FLAGS);
470
471 spin_unlock_irq(&rtc_lock);
472
473 /* FIXME teach the alarm code how to handle binary mode;
474 * <asm-generic/rtc.h> doesn't know 12-hour mode either.
475 */
476 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) {
477 dev_dbg(dev, "only 24-hr BCD mode supported\n");
478 retval = -ENXIO;
479 goto cleanup1;
480 }
481
482 if (is_valid_irq(rtc_irq))
483 retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED,
David Brownellcd966202007-05-08 00:33:40 -0700484 cmos_rtc.rtc->dev.bus_id,
David Brownellab6a2d72007-05-08 00:33:30 -0700485 cmos_rtc.rtc);
David Brownell7be2c7c2007-02-10 01:46:02 -0800486 if (retval < 0) {
487 dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
488 goto cleanup1;
489 }
490
491 /* REVISIT optionally make 50 or 114 bytes NVRAM available,
492 * like rtc-ds1553, rtc-ds1742 ... this will often include
493 * registers for century, and day/month alarm.
494 */
495
496 pr_info("%s: alarms up to one %s%s\n",
David Brownellcd966202007-05-08 00:33:40 -0700497 cmos_rtc.rtc->dev.bus_id,
David Brownell7be2c7c2007-02-10 01:46:02 -0800498 is_valid_irq(rtc_irq)
499 ? (cmos_rtc.mon_alrm
500 ? "year"
501 : (cmos_rtc.day_alrm
502 ? "month" : "day"))
503 : "no",
504 cmos_rtc.century ? ", y3k" : ""
505 );
506
507 return 0;
508
509cleanup1:
510 rename_region(ports, NULL);
511cleanup0:
512 rtc_device_unregister(cmos_rtc.rtc);
513 return retval;
514}
515
516static void cmos_do_shutdown(void)
517{
518 unsigned char rtc_control;
519
520 spin_lock_irq(&rtc_lock);
521 rtc_control = CMOS_READ(RTC_CONTROL);
522 rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
523 CMOS_WRITE(rtc_control, RTC_CONTROL);
524 CMOS_READ(RTC_INTR_FLAGS);
525 spin_unlock_irq(&rtc_lock);
526}
527
528static void __exit cmos_do_remove(struct device *dev)
529{
530 struct cmos_rtc *cmos = dev_get_drvdata(dev);
531
532 cmos_do_shutdown();
533
Marko Vrh41ac8df2007-05-08 00:34:09 -0700534 if (is_pnp())
David Brownell7be2c7c2007-02-10 01:46:02 -0800535 release_resource(cmos->iomem);
536 rename_region(cmos->iomem, NULL);
537
538 if (is_valid_irq(cmos->irq))
David Brownellcd966202007-05-08 00:33:40 -0700539 free_irq(cmos->irq, cmos_rtc.rtc);
David Brownell7be2c7c2007-02-10 01:46:02 -0800540
541 rtc_device_unregister(cmos_rtc.rtc);
542
543 cmos_rtc.dev = NULL;
544 dev_set_drvdata(dev, NULL);
545}
546
547#ifdef CONFIG_PM
548
549static int cmos_suspend(struct device *dev, pm_message_t mesg)
550{
551 struct cmos_rtc *cmos = dev_get_drvdata(dev);
552 int do_wake = device_may_wakeup(dev);
David Brownellbcd9b892007-04-01 23:49:47 -0700553 unsigned char tmp;
David Brownell7be2c7c2007-02-10 01:46:02 -0800554
555 /* only the alarm might be a wakeup event source */
556 spin_lock_irq(&rtc_lock);
557 cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
558 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
David Brownellbcd9b892007-04-01 23:49:47 -0700559 unsigned char irqstat;
560
David Brownell7be2c7c2007-02-10 01:46:02 -0800561 if (do_wake)
562 tmp &= ~(RTC_PIE|RTC_UIE);
563 else
564 tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
565 CMOS_WRITE(tmp, RTC_CONTROL);
566 irqstat = CMOS_READ(RTC_INTR_FLAGS);
David Brownellbcd9b892007-04-01 23:49:47 -0700567 irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF;
568 if (is_intr(irqstat))
David Brownellab6a2d72007-05-08 00:33:30 -0700569 rtc_update_irq(cmos->rtc, 1, irqstat);
David Brownellbcd9b892007-04-01 23:49:47 -0700570 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800571 spin_unlock_irq(&rtc_lock);
572
David Brownell87ac84f2007-05-08 00:34:00 -0700573 if (tmp & RTC_AIE) {
574 cmos->enabled_wake = 1;
575 if (cmos->wake_on)
576 cmos->wake_on(dev);
577 else
578 enable_irq_wake(cmos->irq);
579 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800580
581 pr_debug("%s: suspend%s, ctrl %02x\n",
David Brownellcd966202007-05-08 00:33:40 -0700582 cmos_rtc.rtc->dev.bus_id,
David Brownell7be2c7c2007-02-10 01:46:02 -0800583 (tmp & RTC_AIE) ? ", alarm may wake" : "",
584 tmp);
585
586 return 0;
587}
588
589static int cmos_resume(struct device *dev)
590{
591 struct cmos_rtc *cmos = dev_get_drvdata(dev);
592 unsigned char tmp = cmos->suspend_ctrl;
593
David Brownell7be2c7c2007-02-10 01:46:02 -0800594 /* re-enable any irqs previously active */
595 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
596
David Brownell87ac84f2007-05-08 00:34:00 -0700597 if (cmos->enabled_wake) {
598 if (cmos->wake_off)
599 cmos->wake_off(dev);
600 else
601 disable_irq_wake(cmos->irq);
602 cmos->enabled_wake = 0;
603 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800604
605 spin_lock_irq(&rtc_lock);
606 CMOS_WRITE(tmp, RTC_CONTROL);
607 tmp = CMOS_READ(RTC_INTR_FLAGS);
David Brownellbcd9b892007-04-01 23:49:47 -0700608 tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
609 if (is_intr(tmp))
David Brownellab6a2d72007-05-08 00:33:30 -0700610 rtc_update_irq(cmos->rtc, 1, tmp);
David Brownellbcd9b892007-04-01 23:49:47 -0700611 spin_unlock_irq(&rtc_lock);
David Brownell7be2c7c2007-02-10 01:46:02 -0800612 }
613
614 pr_debug("%s: resume, ctrl %02x\n",
David Brownellcd966202007-05-08 00:33:40 -0700615 cmos_rtc.rtc->dev.bus_id,
David Brownell7be2c7c2007-02-10 01:46:02 -0800616 cmos->suspend_ctrl);
617
618
619 return 0;
620}
621
622#else
623#define cmos_suspend NULL
624#define cmos_resume NULL
625#endif
626
627/*----------------------------------------------------------------*/
628
629/* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
David Brownellbcd9b892007-04-01 23:49:47 -0700630 * the device node will always be created as a PNPACPI device.
David Brownell7be2c7c2007-02-10 01:46:02 -0800631 */
632
Marko Vrh41ac8df2007-05-08 00:34:09 -0700633#ifdef CONFIG_PNP
David Brownell7be2c7c2007-02-10 01:46:02 -0800634
635#include <linux/pnp.h>
636
637static int __devinit
638cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
639{
640 /* REVISIT paranoia argues for a shutdown notifier, since PNP
641 * drivers can't provide shutdown() methods to disable IRQs.
642 * Or better yet, fix PNP to allow those methods...
643 */
Matthew Garrett6cd8fa82007-06-01 00:46:51 -0700644 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
645 /* Some machines contain a PNP entry for the RTC, but
646 * don't define the IRQ. It should always be safe to
647 * hardcode it in these cases
648 */
649 return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8);
650 else
651 return cmos_do_probe(&pnp->dev,
652 &pnp->res.port_resource[0],
653 pnp->res.irq_resource[0].start);
David Brownell7be2c7c2007-02-10 01:46:02 -0800654}
655
656static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
657{
658 cmos_do_remove(&pnp->dev);
659}
660
661#ifdef CONFIG_PM
662
663static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
664{
665 return cmos_suspend(&pnp->dev, mesg);
666}
667
668static int cmos_pnp_resume(struct pnp_dev *pnp)
669{
670 return cmos_resume(&pnp->dev);
671}
672
673#else
674#define cmos_pnp_suspend NULL
675#define cmos_pnp_resume NULL
676#endif
677
678
679static const struct pnp_device_id rtc_ids[] = {
680 { .id = "PNP0b00", },
681 { .id = "PNP0b01", },
682 { .id = "PNP0b02", },
683 { },
684};
685MODULE_DEVICE_TABLE(pnp, rtc_ids);
686
687static struct pnp_driver cmos_pnp_driver = {
688 .name = (char *) driver_name,
689 .id_table = rtc_ids,
690 .probe = cmos_pnp_probe,
691 .remove = __exit_p(cmos_pnp_remove),
692
693 /* flag ensures resume() gets called, and stops syslog spam */
694 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
695 .suspend = cmos_pnp_suspend,
696 .resume = cmos_pnp_resume,
697};
698
699static int __init cmos_init(void)
700{
701 return pnp_register_driver(&cmos_pnp_driver);
702}
703module_init(cmos_init);
704
705static void __exit cmos_exit(void)
706{
707 pnp_unregister_driver(&cmos_pnp_driver);
708}
709module_exit(cmos_exit);
710
Marko Vrh41ac8df2007-05-08 00:34:09 -0700711#else /* no PNP */
David Brownell7be2c7c2007-02-10 01:46:02 -0800712
713/*----------------------------------------------------------------*/
714
Marko Vrh41ac8df2007-05-08 00:34:09 -0700715/* Platform setup should have set up an RTC device, when PNP is
David Brownellbcd9b892007-04-01 23:49:47 -0700716 * unavailable ... this could happen even on (older) PCs.
David Brownell7be2c7c2007-02-10 01:46:02 -0800717 */
718
719static int __init cmos_platform_probe(struct platform_device *pdev)
720{
721 return cmos_do_probe(&pdev->dev,
722 platform_get_resource(pdev, IORESOURCE_IO, 0),
723 platform_get_irq(pdev, 0));
724}
725
726static int __exit cmos_platform_remove(struct platform_device *pdev)
727{
728 cmos_do_remove(&pdev->dev);
729 return 0;
730}
731
732static void cmos_platform_shutdown(struct platform_device *pdev)
733{
734 cmos_do_shutdown();
735}
736
737static struct platform_driver cmos_platform_driver = {
738 .remove = __exit_p(cmos_platform_remove),
739 .shutdown = cmos_platform_shutdown,
740 .driver = {
741 .name = (char *) driver_name,
742 .suspend = cmos_suspend,
743 .resume = cmos_resume,
744 }
745};
746
747static int __init cmos_init(void)
748{
749 return platform_driver_probe(&cmos_platform_driver,
750 cmos_platform_probe);
751}
752module_init(cmos_init);
753
754static void __exit cmos_exit(void)
755{
756 platform_driver_unregister(&cmos_platform_driver);
757}
758module_exit(cmos_exit);
759
760
Marko Vrh41ac8df2007-05-08 00:34:09 -0700761#endif /* !PNP */
David Brownell7be2c7c2007-02-10 01:46:02 -0800762
763MODULE_AUTHOR("David Brownell");
764MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
765MODULE_LICENSE("GPL");