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