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